Page 1 of 2
goto poll;
Posted: Mon Oct 29, 2007 4:50 pm
by piranha
What do you think of the goto keyword?
Good/Bad etc...
I don't use it, not because of a general rule, just because other ways look cleaner and are easier to read and deal with.
-JL
Posted: Mon Oct 29, 2007 6:41 pm
by Colonel Kernel
If there were an option in the poll that says "I use it as a last resort", that's what I would have picked. It can be handy for centralizing last-ditch cleanup code in C if you have no other choice (RAII in C++ is a much better way to do it). I wouldn't use it for any other purpose though.
Posted: Mon Oct 29, 2007 8:04 pm
by AndrewAPrice
You have to be careful too. You can jump from a member in one class to a member in another, which really messes up the stack.
EDIT: My lecturer also said it's an instant fail if he finds a goto in one of our assignments.
Posted: Mon Oct 29, 2007 8:19 pm
by piranha
EDIT: My lecturer also said it's an instant fail if he finds a goto in one of our assignments.
Well, I hope he knows about the jump command for the CPU.......it's freakin everywhere.
I bet he does, but yeah....
And it's true, goto isn't great.
Posted: Tue Oct 30, 2007 1:37 am
by Combuster
Being a basic programmer, the goto keyword is the only way to do exception handling, so yes, I use it on a regular basis. (and then there are those languages without function calls
)
That said, I probably would never use it in C though.
Posted: Tue Oct 30, 2007 2:24 am
by Solar
I *do* use it in C occassionally, but only as a last resort for in-function exception handling. (For example, during construction of nested memory structures on the heap - if the second or third allocation fails, you have to clean up what you already allocated.) I could write my way around using goto, but sometimes the workaround would be even uglier.
I didn't vote any of the above because none of the options quite fit.
Posted: Wed Oct 31, 2007 10:01 am
by 01000101
The only time I use the goto: is when I am handling interrupts and do last minute error checking.
Posted: Fri Nov 02, 2007 11:33 pm
by iammisc
I use it when i don't want to write code over and over like when i have a function that needs to lock/unlock mutexes but returns multiple times. Instead of retyping the unlocking and return sequence I just goto the label with the return statements under it. For anything else, it's pretty useless.
Posted: Fri Nov 02, 2007 11:47 pm
by FutureDomain
I never use goto in C#. It's there, but I find it's better to use structured programming and exception handling. There are a couple circumstances where it might be the best option, but I've never hit those yet.
Posted: Sat Nov 03, 2007 3:13 am
by XCHG
Isn't "goto" in C the equivalent of a simple JMP instruction? I don't see any problems in using that. You have to use it. If you don't, people will die
Posted: Sat Nov 03, 2007 7:00 am
by AndrewAPrice
XCHG wrote:Isn't "goto" in C the equivalent of a simple JMP instruction? I don't see any problems in using that.
It is still considered by many as bad programming practice. I take it as you haven't used C or C++ before? Well, it is a simple jump instruction, which may work fine in some situations (e.g. for a to jump over a block of code, or back to the beginning of a loop), but when you start jumping between functions, classes, and between scope within the same function, you can begin to screw up the stack and memory location of local variables. This is not to say you couldn't work out a way to do this in a safe manner.
XCHG wrote:You have to use it. If you don't, people will die
Of course you have to use the JMP instruction to divert program control to another section in memory, and goto isn't the only way to do this. C loops, conditions, and function calls removes the necessity of using goto. I don't even have a single "goto" in my OS which is 99%+ C++ (I do use jmp in assembly, but only occasionally, like to jump over my multiboot header and to jump to my main c++ function). This is only my opinion, and does not mean that you can't/shouldn't use goto just because I say so.
I could provide useful in some situations, like if you loaded a program and you wanted to jump to an address in memory. In my OS, I load the EIP into the thread's structure, which gets placed into memory on a context switch, avoiding the need for goto. If I had to jump to an address, I would create a function pointer to the address and then call the function.
Posted: Sat Nov 03, 2007 11:56 am
by niteice
Posted: Sat Nov 03, 2007 12:33 pm
by Brynet-Inc
The moral of the story.. If you use goto's you'll be attacked and killed by prehistoric reptiles?
Posted: Sat Nov 03, 2007 2:03 pm
by SpooK
XCHG wrote:Isn't "goto" in C the equivalent of a simple JMP instruction? I don't see any problems in using that. You have to use it. If you don't, people will die
Yes, and for people that let the compiler do most/all of the work... in there lies the problem of diverting program control in such a manner.
Posted: Mon Nov 05, 2007 10:46 pm
by babernat
I try hard not to use it. But to my everlasting shame I have used it in hobby code. Every time I have used it it has been because I did not truly understand the problem and the resulting solution...
http://www.cs.utexas.edu/users/EWD/ewd02xx/EWD215.PDF