Skip to content

Instantly share code, notes, and snippets.

@mahmoudhossam
Last active December 18, 2015 17:09
Show Gist options
  • Save mahmoudhossam/5816826 to your computer and use it in GitHub Desktop.
Save mahmoudhossam/5816826 to your computer and use it in GitHub Desktop.
How I think Java code should be written and why.

This is in response to this article.

Braces

In the 21st century where 2k monitors exist, saving a line of code here or there really isnt a valid argument. Especially when source files can most of the time fit all on one screen. Code folding also exists in many good IDEs.

2k monitors do exist, but not everybody has them. I have a single 20" monitor with a resolution of 1600*900.

Saving a line of code here and there does count for something when you're looking at a ~9000 line monstrosity like this.

Properly commented code usually results in files that can't possibly fit on one screen, no matter how large.

I prefer not having to scroll endlessly to find what I'm looking for in a file, especially when I don't want to fire up my IDE just to navigate an unfamiliar code base.

Code folding is also okay, but you're assuming I have the code in my IDE already, which isn't always the case. I browse code on github all the time.

I actually found out that to contribute to Android, you have to use the first style, according to their code style guidelines.

Also, Oracle(Sun) has their own style guide and they seem prefer the first style, shown here

It separates code easier

This is subjective at best.

It clearly shows which trailing brace belong to the control block. Especially when there are many nested blocks.

If there are many nested blocks, the second style is going to be a disaster with all the scrolling, no thanks.

If you really want to know which trailing space belongs where, enable the brace matching feature in your favorite IDE, or if you're not using one, the indentation will give it away.

When you want to test the code without the control block, you can comment out just the control block and execute as the compiler ignores braces. With braces on same line, you then have to also comment out the matching brace.

While this seems to be a convenience, I don't think it's worth going against the convention and having to deal with other annoyances this style introduces.

What annoys me even more is when people do this:

if (var == true){

//code

}

Oh yes, this does annoy me too :)

Indentation

I prefer 4 spaces for indentation, and I use a single tab for that.

Annotations

I actually prefer to have each annotation on its own line, it makes it easier for me to know what annotation goes where, and not make the declaration get lost between all the annotations.

It's simply too much code for a single line to handle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment