|
Agreed! That’s what GOTO is for.
Time is the differentiation of eternity devised by man to measure the passage of human events.
- Manly P. Hall
Mark
Just another cog in the wheel
|
|
|
|
|
|
Obviously it should have been:
if (!condition)
{
}
else
{
return;
}
- I would love to change the world, but they won’t give me the source code.
|
|
|
|
|
Whehe, that's evil
Bastard Programmer from Hell
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Got it before I did!
|
|
|
|
|
I would never do it the same, but I can understand it as a hint and that is ok.
Of course
if (condition) return;
would be more appropriate
On the other hand: Is it really worth to discuss such little things?
modified 5-Apr-21 21:01pm.
|
|
|
|
|
Member 15092515 wrote: On the other hand: Is it really worth to discuss such little things? You're absolutely correct. Let's discuss this, instead:
if (condition)
{
return;
}
else
{
// Do something else
}
Ravings en masse^ |
---|
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein | "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010 |
|
|
|
|
|
Ah ok I see you have a problem with 'if ... else'
[Edit]
Is see in your profile "In real life, a research chemist."
Yep, better stop programming. This because programming follow strict rules while chemistry has only exceptions 
modified 5-Apr-21 21:01pm.
|
|
|
|
|
You're asking if it's worth having a pointless whinge about something others do that's outside of one's control?
What a silly question!
cheers
Chris Maunder
|
|
|
|
|
when you refactor some code and are too afraid to remove else clauses.
I'd rather be phishing!
|
|
|
|
|
I like single exit points... That code would drive me nuts too...
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
Bonus points to JSOP!
I actually like having two exit points at most
1st at the very top if your initial sanity checks on input parameters fail. Get that out of the qay early
2nd at the very end of the routine.
cheers
Chris Maunder
|
|
|
|
|
That's what I like too.
If the function is something easy... just one return at the end.
If the function is something normal size... what you said.
If the function is big... I do a second function to "check all needed stuff" at the beginning
and then what you said.
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
I take a similar approach but I don't limit myself to just one return there. I let each sanity check have its own return statement because I find easier to deal with when debugging. Following the input sanitization I try to not have any returns unless a value is returned and then only at the bottom.
"They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"
|
|
|
|
|
Rick York wrote: I let each sanity check have its own return statement
Full disclosure: I do the same if it makes it cumbersome / ugly otherwise. But one test/return section at the top.
cheers
Chris Maunder
|
|
|
|
|
No religion in the lounge!
|
|
|
|
|
hehe 
modified 5-Apr-21 21:01pm.
|
|
|
|
|
It all depends on which measuring stick is used...
- # exits in a function
- # lines of code
- # lines of comments
- # paths in the code
- readability,
- maintainability, modifiability
- only use positive condition tests (! using ! and certainly ! !!)
And of course if you (think you) are paid by lines of code produced there are many other "solutions"
|
|
|
|
|
Visual Studio would show you that the else branch in unnecessary.
|
|
|
|
|
Hmm, to trying to please people who want a single exit point and clear intent when processing is completed:
if (condition)
{
goto returnStatement;
}
:returnStatement
return;

|
|
|
|
|
That's so, so wrong on so many levels...
cheers
Chris Maunder
|
|
|
|
|
My career best:
function doNothing(something) {
return something;
}
Nothing succeeds like a budgie without teeth.
|
|
|
|
|
Just to annoy everyone
switch (condition)
{
case true: return;
default:
}
|
|
|
|
|
if(the_code_below_can_not_be_executed) return;
|
|
|
|
|
Looks like a copy/paste error where the code wasn't cleaned up after the paste.
|
|
|
|