Click here to Skip to main content
15,902,634 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: Avoid return statement in the middle - horror or not? Pin
PIEBALDconsult10-Dec-08 8:33
mvePIEBALDconsult10-Dec-08 8:33 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
Manish K. Agarwal10-Dec-08 21:32
Manish K. Agarwal10-Dec-08 21:32 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
PIEBALDconsult11-Dec-08 6:15
mvePIEBALDconsult11-Dec-08 6:15 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
Thomas Weller11-Dec-08 19:56
Thomas Weller11-Dec-08 19:56 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
PIEBALDconsult12-Dec-08 3:18
mvePIEBALDconsult12-Dec-08 3:18 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
Marcello Faga11-Dec-08 1:30
Marcello Faga11-Dec-08 1:30 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
riced11-Dec-08 14:04
riced11-Dec-08 14:04 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
Thomas Weller11-Dec-08 19:49
Thomas Weller11-Dec-08 19:49 
While having many return statements might be questionable, I think readability is much more important. So I would always allow for multiple return statements to avoid deep nesting. Deep nesting makes the code going out of the right side of the display and it is also harder to understand. Over time, this can become a big maintainability issue.

In your (first) example, we can do without a single return. We can be explicit and well structured at the same time like so:

bool needToDoSomething = false;

if (FlagA && FlagB && FlagC)
{
    needToDoSomething = PromptUser();
}

if (needToDoSomething)
{
    DoSomething();
}
else
{
    DoOtherThing();
}

You should always write your code such that a potential reader can quickly understand your original intention without the need to scroll in any direction.

Another issue arises with the code above when it comes to unit testing and code coverage: We cannot setup code coverage for every single condition in if (FlagA && FlagB && FlagC) - we can do this only for the whole line. If we want to be accurate with this and setup an individual test case for every single condition, we can only do this by using a waterfall-like coding style:

bool needToDoSomething = false;

if (!needToDoSomething) 
    needToDoSomething |= FlagA;
if (!needToDoSomething) 
    needToDoSomething |= FlagB;
if (!needToDoSomething) 
    needToDoSomething |= FlagC;

if (needToDoSomething) 
    needToDoSomething = PromptUser();

if (needToDoSomething)
{
    DoSomething();
}
else
{
    DoOtherThing();
}


Probably not the most elegant solution and surely not the shortest one, but it is easy to read/understand and it has a much better testability than the first example. And this in my view is much more important than any other argument.

Regards
Thomas

www.thomas-weller.de

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Programmer - an organism that turns coffee into software.


GeneralRe: Avoid return statement in the middle - horror or not? Pin
PIEBALDconsult12-Dec-08 3:34
mvePIEBALDconsult12-Dec-08 3:34 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
Rob Grainger16-Dec-08 3:10
Rob Grainger16-Dec-08 3:10 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
coolM18-Dec-08 8:04
coolM18-Dec-08 8:04 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
PIEBALDconsult18-Dec-08 8:41
mvePIEBALDconsult18-Dec-08 8:41 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
coolM18-Dec-08 10:57
coolM18-Dec-08 10:57 
JokeRe: Avoid return statement in the middle - horror or not? Pin
notmasteryet18-Dec-08 12:29
notmasteryet18-Dec-08 12:29 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
PIEBALDconsult18-Dec-08 15:47
mvePIEBALDconsult18-Dec-08 15:47 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
Megidolaon22-Feb-09 23:43
Megidolaon22-Feb-09 23:43 
GeneralC and Machine Code horror Pin
Timothy Baldwin2-Dec-08 11:12
Timothy Baldwin2-Dec-08 11:12 
GeneralRe: C and Machine Code horror Pin
Paul Conrad2-Dec-08 11:16
professionalPaul Conrad2-Dec-08 11:16 
GeneralRe: C and Machine Code horror Pin
PIEBALDconsult2-Dec-08 13:39
mvePIEBALDconsult2-Dec-08 13:39 
GeneralRe: C and Machine Code horror Pin
supercat95-Dec-08 6:19
supercat95-Dec-08 6:19 
GeneralRe: C and Machine Code horror Pin
Timothy Baldwin8-Dec-08 8:30
Timothy Baldwin8-Dec-08 8:30 
GeneralRe: C and Machine Code horror Pin
supercat98-Dec-08 19:30
supercat98-Dec-08 19:30 
GeneralRe: C and Machine Code horror Pin
Lutosław5-Dec-08 12:11
Lutosław5-Dec-08 12:11 
GeneralC# bool datatype horror [modified] Pin
Shyam Bharath24-Nov-08 5:00
Shyam Bharath24-Nov-08 5:00 
GeneralRe: C# bool datatype horror Pin
PIEBALDconsult24-Nov-08 5:23
mvePIEBALDconsult24-Nov-08 5:23 

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.