Template talk:Computer Programming/Control/5

Beg to differ
Beg to differ with the idea that a return can be replaced by a goto to the end of the routine and be twice as readable: it is twice as much work for the code reader/modifier. Many years of reading other folks code has shown that you cannot credulously take names at their word. Dangerous assumption that a label EndOfRoutine is ACTUALLY at the end of the routine. Programmers who think they know better or are just lazy may sneak a line or two of essential code in after this label. Thus you must verify that this label is actually at the end of the routine, with no other statements after it, before it is known to have the same function as the return. Return is clean and safe, can only mean one thing. (Alas for the wonderful DEC F77 labeled return, which jumped to the label of choice in the calling routine!) Allows you to check off and exit from non-allowed conditions, keep your left margin narrow and the indentations and logic easy to follow.

search for returns vs. search for label
I probably have analysed just as many code from other programmers and the problem is that you always have to fight lazy programmers. It is just a matter where you currently look.

Imagine a large function - one with a large case statemen which can't be broken down. How will you analze it? From my experience you first look at the beginning, than skipp the middle case and look at the end.

With that top down approch you are likely to see the label first - and then start looking for the fitting gotos.

But even if you analyse bottom up (middle to outside) the goto will at least mean a knudge "Something unstructured is happening here - beware."

With a goto you have a double chance to see the early exit - with a return only one. The most dangerous goto/return is the one you have not spotted.

Of corse: I usually avoid both - only one in a hundred functions I write has more then one return.

--Krischik 06:15, 28 Jun 2005 (UTC)