• Post Reply Bookmark Topic Watch Topic
  • New Topic

if then return else return  RSS feed

 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question of style and good practice. What's better:



...or...



I think it's the second but I'm not sure.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think that? What criteria are you using for your decision?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:I think it's the second but I'm not sure.

Personally, I prefer

return some_condition ? "something" : "something else";

to both; but I'm an old "ternary" nerd.

Winston
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ternary for me as well in this case, but still interested in why the OP chose the second form given the two choices.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Why do you think that? What criteria are you using for your decision?


I think it's the second because a) that puts a return at the end of the method, and b) it allows for more code to be put between the if and the return.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a) Why is this superior?

b) You can put such code in the else. Why would it be superior?

(Not picking on you -- drawing out your thoughts)

My question: which do you think would be clearer to you when yu come back to this code in 6 months? Which would be clearer to a novice programmer that you bequeath this code to? Why?
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know the Sun Code Conventions are old, but they leave no doubt that Winston is correct (See §10.5.2).
Both the forms in the original post are poor style because they use the keyword return twice, as we suggest here. The fact that there is no semantic difference to the code if you include else or omit it means there is a potential for confusion. As Winston implies, you should learn all about the ?: operator. Note that the () in the link I posted are redundant.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:a) Why is this superior?

I come from the old BASIC days when it was nice to "foot" your subroutines with a return. Habit? Looks nice? Reminds people this method returns something?

b) You can put such code in the else. Why would it be superior?

I personally don't like great big then or else clauses. Again, maybe old BASIC habits.

(Not picking on you -- drawing out your thoughts)

I respect your opinions.

My question: which do you think would be clearer to you when yu come back to this code in 6 months? Which would be clearer to a novice programmer that you bequeath this code to? Why?

I would definitely choose the second form for myself. I think a novice programmer might get tripped up by the first form because he/she might miss the return in the else and not realise that you can't put code after the else.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote: . . . it allows for more code to be put between the if and the return.
So why do you want multiple paths of execution and multiple exit points from your method? If you want additional code after the if, it can be fitted into an else just as well, with no semantic difference.Remember Böhm and Jacopini. Every program can be created from iteration, selection and sequence. I believe Alan Turing had the same concept 30 years previously, only he used recursion instead of iteration. It is not difficult to prove that recursion and iteration are semantically equivalent to each other.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:So why do you want multiple paths of execution and multiple exit points from your method?

I'll chime in here again: because it makes sense.

Just as an example (although it's doesn't strictly deal with return): We are told we should check parameters to prevent avoidable errors like NullPointerExceptions; so how is:semantically different to Knute's? In the above case, I would find it odd to see the "body" of the method in an else block.

I also think that the "one point of return" rule can be applied excessively.
Doesn't:seem more intuitive than:?

@Knute: Within reason, I have no problem with multiple 'return's, and I also subscribe to the "shortest path" principle; so if there is some obvious case where a method can exit early, I will generally use your preferred approach - particularly if the only thing it has to do in such a case is to return.
However, there are situations where that isn't the case. For example, the second method above could be re-written as:which actually runs slightly faster (in fact, I believe that's how it's done in many of Java's Collection classes).

And there it does make sense to me to include the else block.

My 2¢.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!