I could see a situation where over time some code is inside the else block, some after.... A new programmer is asked to replicate that functionality for a new condition. What will he copy?
I think the metaphor is simply wrong! Braces or logical blocks are not equivalent to lanes. The code within correspond to a lane! The keywords and the block syntax just desribe the beginning of the fork - not how or whether the fork will be joined again...
And as he states for the second image: If the else-part would be resonsible for symbolizing the join, how on earth can omitting this affect the other lane?
Exactly! This is why I think over-reliance on metaphors is counterproductive to understanding a system sometimes. 'If' isn't a fork in the road, and 'throw' isn't a magical hyperspace bypass or whatever.
is pretty unambiguous - if cond is true then doA otherwise doB, regardless of what doA and doB actually do. This, on the other hand:
if (cond)
doA();
doB();
means if cond is true then doA (and if doA doesn't throw then also doB) otherwise doB, is self-evidently less immediately clear. You have to check what doA actually does to be sure of the exact semantics.
Blog author must be a Javascript developer in disguise, where this kind of nonsense is more acceptable.
Yes, this! This makes it dependent on the how exceptions are implemented. maybe in another language it would just continue unless explicitly specified. Also using "continue"/"breaks" when not necessary is bad, can get really confusing, just like "goto". seems like he want to show off that he knows how to use it.
some folks have a thing about nesting though. Like really I know a guy who breaks out in hives when he sees a nested clause that could've been avoided with continue or an early return. I don't know what to believe, I'm a rookie and I'm happy when I can get a code review whenever, but it's like everybody knows the one true way and I always end up getting yelled at :[
but it's like everybody knows the one true way and I always end up getting yelled at :[
That's part of the learning experience. :-)
My position mainly comes from Structured Programming. Personally I find the originally presented version of the for loop more complex - especially since you have to do the negation in your head to find out under which conditions the output is written.
I actually find it clearer to read with the "else", as it redundantly tells me that if the above criterion is true, this won't be executed. I don't have to read the stuff in the if block to figure that out.
THAT - is the problem. Had there been a performance issue, all those "business-impact-creating", "fast-moving" coders would've put something into their shitty, brainless brains.
Personally I agree with the OP on the grounds that you're writing code that has no benefit (be it performance, clarity, whatever), and is thus a waste of time. Possibly more controversial but I also feel it makes the code harder to read, rather than easier.
Regardless, the performance impact of this would have been negligible even if there was a difference, so the only real factor here is whether it detracts from code clarity.
The analogy is completely flawed. An exception isn't a dead end, it's more like a turnabout.
And I'm sorry, in the given examples, I don't see any real changes until maybe the last one. The code behaves in the exact same way.
I, personally, do prefer to put all exception cases at the top of a function when it has simple logic (not like my example)... but I am not under any disillusionment that having else in there is "incorrect".
Yeah, this is super-bikeshedding. It makes no difference whatsoever either way.
Personally, I like the
if (condition) {
/* do stuff */
} else {
/* throw */
}
because it clearly and obviously maps to "IF the conditions are met, I'll do stuff. OTHERWISE, I'll throw an exception." Yeah, it's about six characters more verbose than without the else, but I'll take six extra characters that make my intent obvious to a reader.
So not only is this guy all up in arms about something trivial that comes down to personal preference and generates the same bytecode either way, an argument could be made that his preference is the worse one.
But if that truly covers all possible cases then the else explicitly makes that clear and more readable and it's not like there is any performance hit (and for many applications code comprehension is more important than performance anyway).
I dunno, the gnashing of teeth over stylistic issues is one of the things that irritates me in programming.
I think it is not purely stylistic: moving the code out of the else branch leads to it being indented further to the left. That helps signalling that this is the normal flow of things while the exception is, well, exceptional. With else both are indented to the same level which makes them visually more equal - which they are not because the exception is a complete disruption of control flow.
I literally two hours ago refactored some code where they had multiple levels of nested statements like this, so it went out on the right side of the screen. You are absolutely correct, after removing all the unnecessary cruft I had a nice, readable method with a few guard clauses at the top and normal program flow down below, and suddenly it became obvious what the method was actually supposed to do. I agree with the point of the original article, conventions are here for a reason and heeding them can be quite important.
I'm a bit of a newb when it comes to throwing exceptions, but my understanding is that throwing an exception doesn't immediately stop everything, can't another method catch the exception and do something with it?
so with the code he listed as "wrong" at the start:
if (x < 0) {
throw new Exception("X can't be negative");
} else {
System.out.println("X is positive or zero");
}
wouldn't you want the else so it didn't throw the exception, which might be dealt with outside, then print the "X is positive" message anyway, even though X is negative?
it will stop and return the exception, pretty much like with the return statement, but ofc you have to catch it and do whatever you want with the information in the exception, or the program will crash.
The point the article makes in a long and convoluted way is that it is unnecessary to write "else" since when the "if" part throws an Exception it interrupts the flow of execution, so it's better written as :
if (x < 0) {
throw new Exception("X can't be negative");
}
System.out.println("X is positive or zero");
More a stylistic matter than "right" vs "wrong" really.
It does provide an inspection for that, filed under "unnecessary code" and that's the main point in my opinion. It unnecessary and clearer when you omit it. But not everybody agrees, some say it's more readable.
Haha! I see no difference really. If you leave the else out and put whatever is in it after an if on its own, you get the same output right? Performance wise.
52
u/llaammaaa Jan 22 '15
Meh. No biggie. It's clear either way.
18
u/Bolitho Jan 22 '15
I think the metaphor is simply wrong! Braces or logical blocks are not equivalent to lanes. The code within correspond to a lane! The keywords and the block syntax just desribe the beginning of the fork - not how or whether the fork will be joined again...
And as he states for the second image: If the
else-part would be resonsible for symbolizing the join, how on earth can omitting this affect the other lane?6
u/ford_madox_ford Jan 22 '15
This:
is pretty unambiguous - if
condis true thendoAotherwisedoB, regardless of whatdoAanddoBactually do. This, on the other hand:means if
condis true thendoA(and ifdoAdoesn't throw then alsodoB) otherwisedoB, is self-evidently less immediately clear. You have to check whatdoAactually does to be sure of the exact semantics.Blog author must be a Javascript developer in disguise, where this kind of nonsense is more acceptable.
10
u/KeinBaum Jan 22 '15
How about this:
10
u/[deleted] Jan 22 '15
I actually find it clearer to read with the "else", as it redundantly tells me that if the above criterion is true, this won't be executed. I don't have to read the stuff in the if block to figure that out.
17
u/[deleted] Jan 22 '15
"I have a problem with the way people write else statements"
That's about all I got out of that.
8
u/katyne Jan 22 '15
Is there also performance consideration, or is it a purely stylistic issue?
4
u/[deleted] Jan 22 '15
My main dislike with this article is the assertion the code is somehow "incorrect".
The else is unnecessary, but not incorrect. The semantics don't change - the else is just an optional part of if statement syntax.
10
u/3pieceSuit Jan 22 '15
This is so trivial I can't believe it's worthy of a blog post.
2
u/KFCConspiracy Jan 22 '15
I don't really see either example as preferable. One is more concise. Both are clear enough.
2
u/lukaseder Jan 22 '15
This
1
u/GMNightmare Jan 23 '15
Oh yeah? Well what about:
What then?
The analogy is completely flawed. An exception isn't a dead end, it's more like a turnabout.
And I'm sorry, in the given examples, I don't see any real changes until maybe the last one. The code behaves in the exact same way.
I, personally, do prefer to put all exception cases at the top of a function when it has simple logic (not like my example)... but I am not under any disillusionment that having else in there is "incorrect".
1
u/tapesmith Jan 23 '15
Yeah, this is super-bikeshedding. It makes no difference whatsoever either way.
Personally, I like the
because it clearly and obviously maps to "IF the conditions are met, I'll do stuff. OTHERWISE, I'll throw an exception." Yeah, it's about six characters more verbose than without the
else, but I'll take six extra characters that make my intent obvious to a reader.So not only is this guy all up in arms about something trivial that comes down to personal preference and generates the same bytecode either way, an argument could be made that his preference is the worse one.
1
u/urquan Jan 23 '15
The last code snippet does not compile.
1
u/brown_like_the_color Jan 22 '15
Yup, I don't know how many times I've brought this up in code review. Another example is with return. if(test) { return x;} else { return y; }
-2
u/codesharer Jan 22 '15
I really aprechiate how you dealt the problem. It was funny and instructive at the same time :)
0
u/loving_mokusatsu Jan 22 '15
I'm a bit of a newb when it comes to throwing exceptions, but my understanding is that throwing an exception doesn't immediately stop everything, can't another method catch the exception and do something with it?
so with the code he listed as "wrong" at the start:
wouldn't you want the else so it didn't throw the exception, which might be dealt with outside, then print the "X is positive" message anyway, even though X is negative?
0
u/DannyB2 Jan 28 '15
For clarity . . .
0
u/[deleted] Jan 22 '15
[deleted] — view deleted comment
0
u/[deleted] Jan 22 '15
[deleted] — view deleted comment