• Post Reply Bookmark Topic Watch Topic
  • New Topic

If statements without braces  RSS feed

 
Andy Ball
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm doing some revision for my OCAJ atm, & I came across this code in a mock question which takes two int arguments & simply returns the larger.


when evaluating it I thought this would be invalid code as would always return x. But it transpires I was way off & that this actually works!
In playing around with it, it seems like the first return statement is treated as: if {// this bit } & the second return is treated as: else{ //this bit}.

What baffles me though is that you can put any amount of additional statements before the second return and it continues to work, however if you put even a single statement before the first return, the whole thing falls over.

I guess my two questions are - Am I right in my discovery above ( that statements preceeding the first return will always break it)?
& secondly is this a good way of coding? for readability, I would always do it as:



But if I'm over-egging the pudding, it'd be nice to know.

cheers.

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it is not a good way of coding. When an if block is not braced, it only encompasses the single statement.

So the code is equivalent to:

It's confusing, as you have seen, so should be avoided.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That said, I have seen coding guidelines that would allow the following:


By placing the single-line block on the same line as the if, it becomes clearer that it is intended to be a single-line block. But I wouldn't recommend this to novices.

Until you are much more familiar with coding, I would recommend to always use braces on blocks.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andy Ball wrote:however if you put even a single statement before the first return, the whole thing falls over.

I guess my two questions are - Am I right in my discovery above ( that statements preceeding the first return will always break it)?

Yes. Because if adding a statement, it looks like this to Java:



The compiler takes issue with the code after the return statement. This isn't a problem when the return is in the if statement because it only happens if the condition is true.
 
Andy Ball
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all,

I've certainly seen the 'one following statement' (on the same line as the) If statement before, but not really with a 2nd (else) return statement after it. it saves a couple of keystrokes, but looks crap (to me).

& thanks for the example Jeanne - that cleared it up nicely.



 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andy Ball wrote:I've certainly seen the 'one following statement' (on the same line as the) If statement before, but not really with a 2nd (else) return statement after it.

I doubt whether anyone else will confess, but I suspect very strongly that many of us adopt a "do as I say; not as I do" attitude when it comes to this kind of stuff.

When I'm teaching beginners, I almost always tell them to "put everything in braces"; but I freely admit to not always enclosing one-liners myself - especially when they're returns - mainly because I find braces add "visual clutter". But it's purely a personal preference; I wouldn't advocate it as the "best way".

It should be added that the one-liner "if...else" can often be substituted with the ternary (? :) operator which - as you can probably understand - I'm a big fan of.

Winston
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:I doubt whether anyone else will confess, but I suspect very strongly that many of us adopt a "do as I say; not as I do" attitude when it comes to this kind of stuff.



Err, well maybe I do occasionally do likewise. Actually now we are being honest about this, I do so more than occasionally.

Like you I believe that in certain circumstances adding braces can make the code harder to read but the ability to correctly make a judgment call on when not to use braces comes with experience so I would certainly advise all novices to initially start out using braces.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm the opposite. I find it harder to read when braces are missed, and don't like seeing one line if statements. My IDE is configured to automatically reactor all code to use next-line braces.

Strangely I have no problem using Python though (which uses whitespace indentation instead of braces to determine scope).
 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:I'm the opposite. I find it harder to read when braces are missed, and don't like seeing one line if statements. My IDE is configured to automatically reactor all code to use next-line braces.

Strangely I have no problem using Python though (which uses whitespace indentation instead of braces to determine scope).
I agree. For me, I find that the braces nicely define what is going on, and helps with readability -- especially for the poor guy who follows afterward and has to maintain it. But then again, I'm CDO (which is similar to OCD, only with the letters in the right order). I need the whitespace, the braces, everything lined up all neatly.

I have found that the 'do as i say not as I do' doesn't work. Hard to tell your kids not to drink, not to curse, etc - when one is an ex-submarine sailor. Scarey thought time -- beginning programmers learn by reading our code.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robert D. Smith wrote:I have found that the 'do as i say not as I do' doesn't work. Hard to tell your kids not to drink, not to curse, etc - when one is an ex-submarine sailor. Scarey thought time -- beginning programmers learn by reading our code.

Actually, I suspect that's the 3rd stage. At first, they learn by rote; then by trying things out for themselves - which is where the problems usually start   - and then by reading other people's code.

And since "style" is such a personal thing, and what we're talking about here isn't wildly important (the language forces you to put braces around anything but a single statement anyway), I'm not sure it's good to be too didactic about it.

And like I say: there's always "? :"...

Winston
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It may be what I seem to see from time to time. I suspect some people intentionally use poor coding style and inconsistent indentation in the hope of confusing readers. Maybe they think the exam will have such poor style, too.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!