Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using {} for single statements

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34178
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On page 91 on "Clean Code", Uncle Bob lists out his preferred formatting rules. I noticed he doesn't use {} if there is only one statement. I'm curious what the advantage of this is.

I've heard and seen the disadvantage - that someone will add a second statement without remembering to add the {}.

This isn't really about agile, but it is about the book being promoted this week. It can be moved to Java in General if the forum moderator prefers.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64683
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used to always use the {} for even single line statements. But lately, I've found myself putting short single-liners on the same line as the conditional.

For example, I'll do:

but I won't do:
If it's not on the same line, it gets the {}'s.

I'm not sure if that makes sense or if I'm just getting lazy/demented.
 
Matthew Taylor
Rancher
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
but I won't do:
If it's not on the same line, it gets the {}'s.


I second that. Even as a Groovy programmer, where the syntax tends to be more terse, I will never omit the {}'s over a statement stretched multiple lines. It just makes things more confusing.
[ September 22, 2008: Message edited by: Matthew Taylor ]
 
Alaa Nassef
Ranch Hand
Posts: 471
Hibernate Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree to that too. For me I prefer having the condition in one line, and the statement between {} on another. I believe it's more readable this way.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeanne Boyarsky:
On page 91 on "Clean Code", Uncle Bob lists out his preferred formatting rules. I noticed he doesn't use {} if there is only one statement. I'm curious what the advantage of this is.

I've heard and seen the disadvantage - that someone will add a second statement without remembering to add the {}.


To me, there is another disadvantage: I need a more complex regular expression in my head to be able to parse the code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic