Click here to Skip to main content
15,887,416 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: False selection... [modified] Pin
_Erik_18-Oct-10 2:06
_Erik_18-Oct-10 2:06 
GeneralRe: False selection... Pin
ghle19-Oct-10 1:36
ghle19-Oct-10 1:36 
GeneralRe: False selection... [modified] Pin
_Erik_19-Oct-10 2:10
_Erik_19-Oct-10 2:10 
GeneralRe: False selection... Pin
AspDotNetDev11-Oct-10 6:38
protectorAspDotNetDev11-Oct-10 6:38 
GeneralRe: False selection... Pin
OriginalGriff11-Oct-10 8:00
mveOriginalGriff11-Oct-10 8:00 
JokeRe: False selection... PinPopular
AspDotNetDev11-Oct-10 9:32
protectorAspDotNetDev11-Oct-10 9:32 
GeneralRe: False selection... Pin
ghle12-Oct-10 15:59
ghle12-Oct-10 15:59 
GeneralRe: False selection... Pin
FSANB15-Oct-10 14:49
FSANB15-Oct-10 14:49 
aspdotnetdev wrote:
I was always told to only have one return statement in a procedure, so I can see this being a horror.


I spent time last century programming in Pascal (amongst many, many other languages) and one of the "rules" which I stubbornly held onto for many years afterwards was to only have a single return from a method...

BUT I finally woke up to myself and (Thank You Martin Fowler!) realised that exiting early (using what I believe are called "guard clauses" by people who know) can really improve readability and performance. Although I do wish that (currently) Visual Studio would highlight every "return" because I still sometimes only notice the last one in a method on first read.

In my OPINION (and without knowing much about the actual subject domain) I prefer the first posted "horror" example to anything I've seen posted since. That is just subjective opinion, but it is based on coding for a long time and working with many other developers with different levels of experience. In fact it's probably mostly based on being embarrassed when returning to my own code years later and not having a clue what I was trying to do...

Thanks to LordOfAwesome for starting this thread and prompting me to post here for the first time (I think... My memory really is getting worse with age). I just also wanted to reply to one of your later comments:

lordofawesome wrote:
Nevertheless I think it is strange to return a boolean from the result of any if structure. It just seems strange to me.


I agree with you that if there was a single "if" statement which returned a boolean, that would be silly, but in this case each of the "if" blocks might either return false AND exit the method, or fall through to the next statement.

And it is that "falling through" that is very difficult to show clearly using in-line boolean operations, even with (or perhaps especially because of) the use of short-circuit boolean operators (like &&, etc.)

So in the case we're discussing, my preference would be to write more code to be absolutely clear about what I was trying to do (and let's remember that the example was from a text book, so clarity is especially important), exit early to only execute the minimum required and if the method did end up being a performance bottleneck, go back and optimise it later, which would be much easier to do based on clear existing logic.


Thanks


AB
GeneralRe: False selection... Pin
ghle19-Oct-10 1:41
ghle19-Oct-10 1:41 
GeneralRe: False selection... Pin
Simon Dufour15-Oct-10 8:55
Simon Dufour15-Oct-10 8:55 
GeneralSomething's out of round here PinPopular
PIEBALDconsult9-Oct-10 5:11
mvePIEBALDconsult9-Oct-10 5:11 
GeneralRe: Something's out of round here Pin
oggenok649-Oct-10 10:34
oggenok649-Oct-10 10:34 
GeneralRe: Something's out of round here Pin
Bigdeak10-Oct-10 23:15
Bigdeak10-Oct-10 23:15 
GeneralClever Convert. Really? PinPopular
Svetlin Panayotov8-Oct-10 3:28
Svetlin Panayotov8-Oct-10 3:28 
GeneralRe: Clever Convert. Really? Pin
GibbleCH8-Oct-10 3:35
GibbleCH8-Oct-10 3:35 
GeneralRe: Clever Convert. Really? Pin
Svetlin Panayotov8-Oct-10 3:54
Svetlin Panayotov8-Oct-10 3:54 
GeneralRe: Clever Convert. Really? Pin
Jörgen Sigvardsson9-Oct-10 21:11
Jörgen Sigvardsson9-Oct-10 21:11 
GeneralRe: Clever Convert. Really? Pin
BillW338-Oct-10 3:48
professionalBillW338-Oct-10 3:48 
GeneralRe: Clever Convert. Really? Pin
Matt Gerrans8-Oct-10 20:16
Matt Gerrans8-Oct-10 20:16 
Generalvb 6, ep. 3 PinPopular
Lutosław7-Oct-10 14:53
Lutosław7-Oct-10 14:53 
GeneralRe: vb 6, ep. 3 PinPopular
OriginalGriff7-Oct-10 21:49
mveOriginalGriff7-Oct-10 21:49 
GeneralRe: vb 6, ep. 3 Pin
Lutosław8-Oct-10 2:56
Lutosław8-Oct-10 2:56 
GeneralRe: vb 6, ep. 3 Pin
Matt Gerrans8-Oct-10 20:20
Matt Gerrans8-Oct-10 20:20 
GeneralRe: vb 6, ep. 3 Pin
Lutosław9-Oct-10 1:20
Lutosław9-Oct-10 1:20 
GeneralRe: vb 6, ep. 3 Pin
Matt Gerrans9-Oct-10 19:27
Matt Gerrans9-Oct-10 19:27 

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.