Automate naming conventions.

As any repeating task that could be automated to save time and prevent human mistakes, adhering to coding conventions could be considered as one of those tasks.


If you haven’t automated yet your coding conventions because you thought that it was difficult to do, then this post will change your mind.

It’s known to be a good practice following some sort of consistency throughout your code. Regardless whether your are working on a personal project or on a team project, consistency helps readability, maintainability and error preventions.

One of the most basic coding practices which help consistency is adhering to naming conventions.

However, it may be tedious to remember and apply naming conventions, especially when you have to face elaborate coding standards to pass your pull request review. Coding standards for field naming may look somehow like:

  • Non-public, non-static field names start with m,
  • Static field names start with s,

(example taken from

So, how do we deal with them in a smart way?

The answer is: let android studio do it for you!

Naturally you need to instruct android studio so that it can automatically apply those conventions and also automagically generate code compliant with your conventions.

Go to File -> Other Settings -> Default Settings…

Screen Shot 2015-12-13 at 19.12.37

Then browse to Editor -> Code Style -> Java

Screen Shot 2015-12-13 at 19.17.09.png

In this window you can set plenty of code styles rules for your java code. You can change for example how “Tabs and Indents” can format your files, where to leave “Spaces” (before parentheses, around operators…), where and how to enforce “Braces” and so on…

But for the sake of example, in this post I am going to focus on the “Code Generation” tab. Here is where we can instruct android studio on field naming conventions described above.

Individuate the “Naming” box, and write “m” in the cell “Name prefix” on the row “Field”, and “s” on the row “Static field” as shown in the following image.

Screen Shot 2015-12-13 at 19.40.18.png

When done, “Apply” the new settings and let’s try to generate some code to verify them.

Create a new Java Class and start writing a field member of type String. Leave a space and then hit “ctrl+space” to trigger the auto-completion feature. You will notice that the field variable generated will be automatically prefixed with “m”.

If you do the same by starting with the static keyword, then the automatically generated variable will be prefixed with “s”.

However that’s not all and maybe it is not even that important because at the end you want to use more meaningful names for your variables. But what is more important is that the auto-generation feature for getter and setter method will remember your prefix and will not include it in the generated methods.

Indeed in our example the methods that will be generated will be getString() and setString(), and not getmString() and setmString() as it would happen without the settings done before.

Screen Shot 2015-12-13 at 20.02.42

As we have configured those rules, you can configure many more coding conventions by simply playing with the other options of the code style window of android studio. I leave to you the fun! 😉


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s