goto a no-go?

Chris wendlec at tcd.ie
Tue Oct 1 04:22:11 PDT 2013


Just a short question. Usually goto statements are frowned upon 
as being bad programming style (in textbooks at least). D has it 
(thankfully) and I've used it, albeit sparingly. Sometimes goto 
is simply the best and most efficient solution within a code 
block (to avoid code duplication, unnecessary checks or 
redirecting to yet another function blah blah). Is it ok or even 
necessary to use goto in D? Or does the compiler recognize 
_obvious_ cases and generate code accordingly? For example would 
it turn something like this

// ...
if (word.length == 1) {
  // format output
  return output;
} else if (word.length > 1) {
   // do some additional processing
   // format output
   return output;
}

into

// ...
if (word.length == 1) goto FormatOutput;

// if word.length > 1, some extra work has to be done
// initialize some variables, parse, do some processing etc.

FormatOutput:
  // .....

return output;



More information about the Digitalmars-d mailing list