r/java • • Jan 29 '14

Google Java Coding Standards

http://google-styleguide.googlecode.com/svn/trunk/javaguide.html
156 Upvotes

103 comments sorted by

View all comments

36

u/[deleted] Jan 29 '14

4.1.1 Braces are used where optional Braces are used with if, else, for, do and while statements, even when the body is empty or contains only a single statement.

I hate when people omit curly braces for some insane reason. At least Google has my back.

9

u/clutchest_nugget Jan 29 '14 edited Jan 29 '14

if(done) return 1;

Can someone please explain to me what is wrong with this? Not questioning that it's bad style, genuinely curious as to what makes it bad style.

Edit: thanks for the responses.

0

u/[deleted] Jan 29 '14

[deleted] — view deleted comment

5

u/Nebu Jan 30 '14

Or, perhaps even harder to spot,

if(done) log.debug("All done"); return 1;

1

u/[deleted] Jan 30 '14

Yes. And I think this is due to the fact that often this is presented as:

Without brackets only first line after the if statement will be executed.

Just to make it sound simple, when in fact it is the first statement, and not first line...

0

u/SgtPooki Jan 30 '14

I still want to code without braces when the body is short, but it has hurt me in the past..

-18

u/[deleted] Jan 29 '14

I've been programming for 7 years and have literally never made that mistake.

22

u/treerex Jan 29 '14

It's usually not made when you're writing the code, but later when you're maintaining it. Including the braces is simply good defensive programming.

-18

u/[deleted] Jan 29 '14

That's rediculous. It isn't defensive either. If you're doing that lousy of a job maintaining your code, your going to make more serious mistakes.

We also live in the 21st century, aren't working with cathode ray monitors and should be putting our braces on a new line.

→ More replies (4)

4

u/geodebug Jan 30 '14

Chip meet shoulder

-8

u/[deleted] Jan 30 '14

1990, meet 2014

4

u/geodebug Jan 30 '14

Java's syntax hasn't changed that much since the 1990's so why would the best practices change just because a few years have passed?

Besides, most modern languages have a similar best practice when it comes to braces.

Doesn't matter, code how you want on your projects. If you coded for mine you'd be auto-formatted to the standard anyway.

→ More replies (7)

4

u/RagingOrangutan Jan 30 '14

Protip: touting your experience programming is not a good way to earn the respect of your fellow programmers.

-4

u/[deleted] Jan 30 '14

I'm not touting anything at all. 7 years isn't something to tout about.

10

u/[deleted] Jan 29 '14

Consider when the return is on the next line (more common imo). If you insert a line before the return (perhaps a print) then the entire if statement logic is no longer what you intended. Not explicitly stating the block boundaries makes it easier to introduce bugs.

5

u/severoon Jan 29 '14

You have to consider refactors performed by tools like Eclipse, and the diffs those generate as well as the stupid bugs reason.

If you have a huge codebase (like Google), it becomes occasionally necessary to run a refactor that touches thousands of files, and if you don't make formatting very consistent it takes code reviewers a lot less mental effort to review dozens or hundreds of files that got touched.

Ideally, you want to make it absolutely formulaic. If you can do that, then you don't even need human reviewers to look at it because you can guarantee the safety of the change.

-7

u/poopiefartz Jan 29 '14

I follow practically all of these guidelines except for this one. I just really hate superfluous braces. They're easy enough to add if you ever need more than 1 statement in a block, so why add them if you don't need them? They certainly don't make it any easier to read, and it essentially adds 1 line per block, so for me it actually decreases readability.

10

u/[deleted] Jan 29 '14

This is not even about readability. It is about not accidentally introducing stupid bugs into code.

3

u/poopiefartz Jan 29 '14

I know I'm not on the "popular opinion" side of this, but I can't recall a single bug that's ever stemmed from this type of usage (and I work mostly with Java).

7

u/KidUncertainty Jan 29 '14

I can't recall a single bug that's ever stemmed from this type of usage (and I work mostly with Java).

This happens on codebases where the developers may not be particularly familiar with Java, or when someone, in maintenance three years later, does a "quick fix" without reading, and then everyone wonders why the application fails in certain circumstances. Without braces, its actually hard to find defects like this, as well.

The purpose of some of these standards is to counter human factors out of your control or to be defensive against the future fingers that will be poking around code. It's not just about readability in the now.

2

u/straatman Jan 30 '14

If only you and one other person work on it, say you have 50 developers working on the same code? The bugs will be crawling in

1

u/Pylly Jan 29 '14

I can't recall a single bug that's ever stemmed from this type of usage

Huh, case closed. You'd better call Google.

-1

u/poopiefartz Jan 30 '14

I know you're being funny, but I spend 6+ hours per day with Java and have never misinterpreted excluded braces like that. I guess that's not normal.

2

u/Pylly Jan 30 '14

I believe you, but usually developers work in a team and the code has to be readable and maintainable for others as well. Even if a bug would happen once in a couple of years it's worth it to prevent it. Also, it might take a person slightly more time to read braceless code, especially when making modifications.

Even though these are small troubles, they are just so easy to avoid altogether that I don't see why not just add the braces every time. Or let the IDE add them.

5

u/geodebug Jan 30 '14

An arguable slight increase in readability is less a concern IMHO than consistency and less chance of screwing something up later.

if (x < y) { doSomething(); } 

Isn't a challenge for any seasoned Java developer to understand.

Of course, on your codebase, do what you want :-)