Click here to Skip to main content
15,887,988 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Which code you suggest? Pin
jibalt26-Jul-13 17:30
jibalt26-Jul-13 17:30 
GeneralRe: Which code you suggest? Pin
PIEBALDconsult30-Jul-13 13:43
mvePIEBALDconsult30-Jul-13 13:43 
GeneralRe: Which code you suggest? Pin
jibalt19-Aug-13 23:25
jibalt19-Aug-13 23:25 
AnswerRe: Which code you suggest? Pin
James Lonero26-Jul-13 13:45
James Lonero26-Jul-13 13:45 
AnswerRe: Which code you suggest? Pin
jibalt26-Jul-13 17:24
jibalt26-Jul-13 17:24 
AnswerRe: Which code you suggest? Pin
jibalt26-Jul-13 17:27
jibalt26-Jul-13 17:27 
AnswerRe: Which code you suggest? Pin
BobJanova29-Jul-13 0:50
BobJanova29-Jul-13 0:50 
AnswerRe: Which code you suggest? Pin
irneb29-Jul-13 3:30
irneb29-Jul-13 3:30 
Obviously you missed a break in your 2nd method (or I hope so). If you do this, it's actually a hybrid between the 2 methods. A more "strictly" no-goto (even hidden as a break / premature return) would be as per MarkTJohnson's code samples - i.e. add a check in the loop's conditions to see if the state variable has changed.

As for those advocating premature returns, but (in the same breath) dissing goto: Such a return is very related to a goto, as is a break inside a loop. All 3 result in much the same assembly code, the only difference is return/break is safer to use than goto - since goto has more freedom to screw-up. It would be similar in stating that you should always use typed pointers, very true - but it's not as if an untyped pointer is actually "that" different, just possible to do more damage. Not to say that goto's should be used, just to point out that you're walking a fine line between a good idea and contradicting yourself.

Also I'll give you the benefit of the doubt and not try to encourage to use the Contains (or other similar stuff) instead of the entire function, as some have already pointed out. With the thinking that you used this as a sample to make a point, not a sample to take literally.

That said, I tend to find making the 2nd method work properly and efficiently becomes more code. And with more complex functions the extra code becomes exponentially more. Although I don't have trouble reading the principle itself (either one, as long as formatted so the state-set or returns are clearly highlighted, e.g. a blank line after each), I tend to try and write less code (if possible) - so I'd probably go with method 1 if no other reasons are apparent.

This particular thing (i.e. premature return vs state variable) I see as subjective in the same sense that some would like / understand recursive more than iterative loops (or visa versa). If all else is the same - i.e. no need for other stuff to happen too just before the return.

Where it does become a pain, is if your function is later extended. You might later add a further portion which should run even if the value is already found. Perhaps some side effect, like counting how many times a positive was returned through the object's life (sorry - stupid example, but it happens).
GeneralRe: Which code you suggest? Pin
jibalt19-Aug-13 23:31
jibalt19-Aug-13 23:31 
AnswerRe: Which code you suggest? Pin
Jonathan C Dickinson29-Jul-13 22:07
Jonathan C Dickinson29-Jul-13 22:07 
GeneralRe: Which code you suggest? Pin
jibalt19-Aug-13 23:34
jibalt19-Aug-13 23:34 
AnswerRe: Which code you suggest? Pin
PIEBALDconsult30-Jul-13 5:24
mvePIEBALDconsult30-Jul-13 5:24 
AnswerRe: Which code you suggest? Pin
DarkChuky CR30-Jul-13 6:48
DarkChuky CR30-Jul-13 6:48 
GeneralRe: Which code you suggest? Pin
johannesnestler1-Aug-13 4:27
johannesnestler1-Aug-13 4:27 
GeneralRe: Which code you suggest? Pin
DarkChuky CR1-Aug-13 5:44
DarkChuky CR1-Aug-13 5:44 
GeneralRe: Which code you suggest? Pin
johannesnestler1-Aug-13 23:26
johannesnestler1-Aug-13 23:26 
AnswerRe: Which code you suggest? Pin
TheGreatAndPowerfulOz30-Jul-13 10:18
TheGreatAndPowerfulOz30-Jul-13 10:18 
AnswerRe: Which code you suggest? Pin
johannesnestler1-Aug-13 4:04
johannesnestler1-Aug-13 4:04 
AnswerRe: Which code you suggest? Pin
Vasudevan Deepak Kumar2-Aug-13 8:29
Vasudevan Deepak Kumar2-Aug-13 8:29 
GeneralMisaligned elephants Pin
Matthew Faithfull22-Jul-13 2:10
Matthew Faithfull22-Jul-13 2:10 
GeneralRe: Misaligned elephants Pin
Richard Deeming22-Jul-13 11:34
mveRichard Deeming22-Jul-13 11:34 
GeneralRe: Misaligned elephants Pin
Matthew Faithfull22-Jul-13 11:54
Matthew Faithfull22-Jul-13 11:54 
GeneralRe: Misaligned elephants Pin
ExcellentOrg22-Jul-13 21:25
ExcellentOrg22-Jul-13 21:25 
GeneralRe: Misaligned elephants Pin
enhzflep23-Jul-13 19:51
enhzflep23-Jul-13 19:51 
GeneralRe: Misaligned elephants Pin
Matthew Faithfull23-Jul-13 23:03
Matthew Faithfull23-Jul-13 23:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.