Read the following sentences and notice the differences. Not the text but the style differences.

IAm sO clever,that-sometimesI don 't undErstand, a sinGle?word, oF what IAm-

saying-

.

or

I'm so clever that sometimes I don’t understand a single word of what I'm saying.

Which one was easier to read and understand?

I bet the second one!

How do I know?! Because consistent, correct punctuation makes written sentences understandable. A common well-defined system, aka. grammar makes sure that everyone who literate could easily read and understand a sentence.

This applies to the code we write. If the style of the code is consistent then it is faster to read and easier to understand. Since we(developers) read a lot of code through the day, this makes or brakes our day. This is the reason why we have code style wars. When one party thinks you have to put the starting braces in new lines, while the others think it should be the closing character in the line.

Does it really matter where the starting braces are?

From the code point of view, it does not! From the program performance point of view, it does not! But from the reader(programmer) point of view, it really does!

Our brain works by matching patterns to everything that we see. When you open a class’ source code your brain tries to make sense of it by matching it to something similar you saw in the past. And from that exact past experience it knows what to focus on. And your conscious self only get the important information to deal with. Without pattern matching every time you you would be forced to think through the whole deal, which is really time and energy consuming.

In the day to day life, this translates to the following scenario. When you see an if statement you know where it ends by searching for a line with only a closing brace } in it. And without conscious effort, you can just skip the part(if the condition not applies) and continue reading the code. The spared mental effort then could be spend on the things that really matter.

I hope now we can agree, that a common coding style spare a lot of effort during code reading and makes your everyday life easier.

But in a team, there are a lot more people than just you. It is not enough if you write code in one style but all of you have to write in the same style. And this is where Checkstyle could help you!

Checkstyle, the tool, will automatically check your code if it adheres to the coding style of your team or not. Of course you have to have a common code style. So before introducing this tool, first agree on a style with your teammates. Think about where you want to put the braces, how many spaces you want to use for indentation. Do you want blank lines between variable declarations or not, etc. Believe me that 30 minutes meeting will pay you back in even in the short run.

The easiest way to do this is to open Android Studio/Preferences/Code style and set all of the things there. When you are finished just “Save as” the style and save it into your version control system from where everyone can import it.

If you really want to make your life super easy just set up auto-format for the “save” action. In this way, every time you save the code it will be formatted correctly.

If you don’t want to bother to create one for yourself you can use Square’s code style:

Now you have a code style that you agreed on. The next step is to put the following lines to your build.gradle file:

In the above snippet I referenced a checkstyle.xml(here is mine) and a suppressions.xml(here is mine) to fully control the tool.

With this, every time you run the check task the code style will be checked too.

If you want to see more examples check out butterknife’s repository or glide’s repository.

Closing thoughts

There are so many things worth fighting over in a code review, but code style is not one of them! Spare 30 minutes to set it up for your taste and let it guard the code for you!

Stop the war! Make your coworker’s and of course your own life easier!