C: alternative to goto?

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

Re: C: alternative to goto?

Post by nathandelane on Fri Jul 25, 2008 9:44 am
([msg=8244]see Re: C: alternative to goto?[/msg])

Okay I see the point of many people here stating basically that, "goto isn't really that bad, and...". When programming in Assembly language, especially pre-i586[1] Assembly (pre 64-bit also) the only reasonable way to create a loop is with jmp or jump statements, which in effect are goto statements for Assembly. This is still a little bit of a misnomer however, because jmp statements are also used for function calls, and to grab macros in some cases when inlining them isn't as effective (I know somebody's going to say that macros are always inlined by the compiler, but you can tell the compiler not to). The other important thing to note here is that Assembly language is a completely top-down stack-based language, meaning that execution starts at the top and goes straight down, that's just how machines works - so to combat that and introduce reusable code where it needs to be dynamic, jump statements are used. This keeps the code small, whereas unraveling a loop would make code faster (two different optimizations that are possible in linking). However jump statements are rarely EVER just the unconditional jmp (except with function calls), which is what you're speaking of when you call a goto - goto statements are unconditional jumps to some part of your program. More common are je (jump if equal), jnz (jump is not zero), jne (jump if not equal).

Ultimately the use of goto statements in code is up to the programmer, especially if goto is part of the language (Java for example does NOT implement goto in the language API, where break and continue are implemented instead as methods of getting out of heavily nested loops and control structures. These two methods are also implemented in many languages). If the programmer, however has the problem of either maintaining or scaling their code because of gotos, then a rewrite is necessary. Loops and functions are nearly always scalable when well written.

Here are two documents that anybody should read if they have questions about goto statements:

In general goto statements are not a part of object-oriented programming, because they break the object paradigm. They are however a part of structured programming, which generally the way one programs using C89 (ISO Standard C). But learning to use functions and loops provides for more readable code and higher reuse, as in the example code I supplied above. In general also, though goto statements may be usable, the number of encumbering labels may be more to blame for the unreadability of programs utilizing gotos heavily.

Thanks. I hope that we've all learned something in this thread, and that we continue to learn, because that's the only way we're all going to get better.


Errata:
[1] There is a LOOP opcode in most Assembly language implementations. It takes the form

Code: Select all
LOOP      label

Where label is similar to a goto label. link


Examples:
Goto example
Code: Select all
label:
... /* Some code */
goto label;


Loop equivalent
Code: Select all
while(condition)
{
... /* Some code */
}

or
Code: Select all
for(init; condition; function)
{
... /* Some code */
}

or
Code: Select all
do
{
... /* Some code */
} while(condition);
Me, Nathandelane, Highly influential to Hackerdom, Premature Optimization=http://c2.com/cgi/wiki?PrematureOptimization
User avatar
nathandelane
Poster
Poster
 
Posts: 204
Joined: Thu Jun 26, 2008 11:26 am
Location: Utah
Blog: View Blog (0)


Previous

Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests

cron