wood burning stoves 2.0*
The moose likes Agile and Other Processes and the fly likes using {} for single statements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Agile and Other Processes
Bookmark "using {} for single statements" Watch "using {} for single statements" New topic
Author

using {} for single statements

Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30116
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60780
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Matthew Taylor
Rancher

Joined: Jun 13, 2004
Posts: 110
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 ]

Grails Consultant
http://dangertree.net
Alaa Nassef
Ranch Hand

Joined: Jan 28, 2008
Posts: 460
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.


Visit my blog: http://jnassef.blogspot.com/
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
 
subject: using {} for single statements
 
Similar Threads
Problem with PDF generation
Rest based post for receiving and reponding with json
CellTable and Pager: shows all data instead of limited number of items per page
HttpSessionAttributeListener and HttpSessionBindingListener
char assignments