Click here to Skip to main content
15,884,298 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: How to be popular among your colleagues Pin
Mycroft Holmes11-Dec-08 21:58
professionalMycroft Holmes11-Dec-08 21:58 
GeneralRe: How to be popular among your colleagues Pin
QuiJohn12-Dec-08 3:18
QuiJohn12-Dec-08 3:18 
GeneralRe: How to be popular among your colleagues Pin
Krirk26-Jan-09 23:09
Krirk26-Jan-09 23:09 
JokeRe: How to be popular among your colleagues Pin
notmasteryet27-Jan-09 5:55
notmasteryet27-Jan-09 5:55 
GeneralDo not trust a computer... PinPopular
notmasteryet11-Dec-08 12:51
notmasteryet11-Dec-08 12:51 
GeneralRe: Do not trust a computer... Pin
Lutosław12-Dec-08 9:57
Lutosław12-Dec-08 9:57 
GeneralAvoid return statement in the middle - horror or not? Pin
Georgi Atanasov5-Dec-08 12:01
Georgi Atanasov5-Dec-08 12:01 
GeneralRe: Avoid return statement in the middle - horror or not? PinPopular
Robert.C.Cartaino5-Dec-08 14:32
Robert.C.Cartaino5-Dec-08 14:32 
You forgot to execute DoSomething() in your second example. That aside...

I don't necessarily dislike "return in the middle." But I expect early returns to be an error condition (i.e. invalid parameter, resource not obtained, etc) to keep the method from continuing. In your example, the early return seems to be the normal, successful condition and that makes me uncomfortable.

So looking at your code samples... Definitely not the first example. If you ever want to add more logic around DoOtherThing(), you've essentially "cut-and-pasted" your code all over the place (a big no-no).

There are a lot of conditions where DoOtherThing() would be executed. According to your logic, if each process (A, B, and C) passes, ask the user if they want to DoSomething(). If user says "no" or any of the processes fail, then DoOtherThing().

To make my intentions clear, I would try and write the logic exactly as I would describe it (of course, the variables would be more "English-Like" if I knew their purpose). Maybe something like this:
// assume for the moment, we don't need to DoSomething()
bool DoSomethingNeeded = false;

if (FlagA && FlagB && FlagC)
{
    // if all processes passed, ask the user if we need to DoSomething()
    DoSomthingNeeded = PromptUser();
}

if (DoSomethingNeeded)
{
    DoSomething();
}
else
{
    DoOtherThing();
}
return;

GeneralRe: Avoid return statement in the middle - horror or not? Pin
John M. Drescher11-Dec-08 8:24
John M. Drescher11-Dec-08 8:24 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
PIEBALDconsult5-Dec-08 15:07
mvePIEBALDconsult5-Dec-08 15:07 
General[Message Deleted] Pin
Robert.C.Cartaino6-Dec-08 5:47
Robert.C.Cartaino6-Dec-08 5:47 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
harold aptroot6-Dec-08 6:48
harold aptroot6-Dec-08 6:48 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
Robert.C.Cartaino6-Dec-08 11:52
Robert.C.Cartaino6-Dec-08 11:52 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
harold aptroot6-Dec-08 12:10
harold aptroot6-Dec-08 12:10 
GeneralRe: Avoid return statement in the middle - horror or not? PinPopular
PIEBALDconsult6-Dec-08 13:44
mvePIEBALDconsult6-Dec-08 13:44 
GeneralRe: Avoid return statement in the middle - horror or not? PinPopular
Graham Bradshaw6-Dec-08 7:43
Graham Bradshaw6-Dec-08 7:43 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
PIEBALDconsult6-Dec-08 11:16
mvePIEBALDconsult6-Dec-08 11:16 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
Mladen Janković9-Dec-08 5:25
Mladen Janković9-Dec-08 5:25 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
PIEBALDconsult9-Dec-08 6:53
mvePIEBALDconsult9-Dec-08 6:53 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
johannesnestler23-Dec-08 1:56
johannesnestler23-Dec-08 1:56 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
ehuysamer5-Dec-08 22:25
ehuysamer5-Dec-08 22:25 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
Tristan Rhodes5-Dec-08 23:39
Tristan Rhodes5-Dec-08 23:39 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
cokkiy6-Dec-08 1:00
cokkiy6-Dec-08 1:00 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
Robert.C.Cartaino6-Dec-08 6:05
Robert.C.Cartaino6-Dec-08 6:05 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
riced6-Dec-08 8:30
riced6-Dec-08 8:30 

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.