Click here to Skip to main content
15,887,683 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
MarkTJohnson11-Feb-14 4:11
professionalMarkTJohnson11-Feb-14 4:11 
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 
We had this discussion quite recently.

In the example you posted, the second is objectively worse because it doesn't leave the loop at the first opportunity. If you are going to go for a single return point, you must put a break in there.

C# provides a simple way to do this general action of 'find the first that matches a condition': the Linq extension method Contains. If this code is C# 3.5 or later then you should use that: return values.Contains(s => s == "ABC"). Note that you can use this for any construction of this type.

If you're in an environment where you can't do that, I'd pick the first. As soon as you have control structures you can't assume top-to-bottom flow anyway, and you have to track execution paths to follow the code, so there's no good argument for not using return any more.
AnswerRe: Which code you suggest? Pin
irneb29-Jul-13 3:30
irneb29-Jul-13 3:30 
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 

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.