• Post Reply Bookmark Topic Watch Topic
  • New Topic

omit else-clause when returning value in if-clause ?  RSS feed

 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What code would be better:


this




or that:




I guess, it makes no difference if I omit the else-clause, as 'doThat' comes, if test is not returning false.

Which code would be better?
 
Marshal
Posts: 58439
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both look bad; in fact the code you quote won't compile because the else has no return statement. I don't like two returns (though I know this is a controversial opinion), so try thisIn the snippet you quoted, it would appear you can simply write return !test; at the end of the method.
 
Bartender
Posts: 1776
45
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think he meant:



does it compile ?(since "do" is a reserved keyword)


I think this can be optimised to :



or


 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I had cut the code down to view the essence:


What about this:








The point is: the statements after 'if'-block are excecuted, if 'test' is not true, so it makes no difference, if I use a 'else' or not.

Why should I use a else in this case?
 
Campbell Ritchie
Marshal
Posts: 58439
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you say, the else is redundant, and I thought one compiler error was enough and I could keep quiet about "do".

What about this?

return !test;
 
salvin francis
Bartender
Posts: 1776
45
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote: I could keep quiet about "do".


 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean different.

Look at that:




In this case I ommit else:



The point is: the statements after 'if'-block are excecuted, if 'test' is not true, so it makes no difference, if I use a 'else' or not.

Why should I use a else in this case?
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nimo..
Seems like a personal choice.

I prefer the latter version for it's explicitness and readability, as below(code sample modified to not become a play on logic than a matter of style(which is what Nimo intended I guess ))



If it's only about return values based on a test and no further processing, then how about this one ...

- Hamy
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes you are right. that was my intention thanks.
 
Campbell Ritchie
Marshal
Posts: 58439
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hemant Murthy wrote: . . . == true . . .

Never, never write == true or == false. You can get all sorts of nasty errors if you write = my mistake, and it is poor style. you should simply writeIf you need it to be false, use the bang/"not" operator (!).
 
Hemant Murthy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah. That's right. Looking back now, that == looks silly for bools
Will remember not to forget that
- Hamy
 
Campbell Ritchie
Marshal
Posts: 58439
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hemant Murthy wrote: . . . that == looks silly . . .

Make sure to forget not to remember it. Don't fail to forget not to do it. And don't forget to remember it.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!