When we develop Android apps, we prefer readability over ease of writing. There are a lot of things that affect code readability like names of variables and functions, language syntax, use of pure functions, etc. But readability can be also increased by good code alignment. And this doesn’t only mean how many tabs or spaces you use. Aligning the same parts of code in columns can help you see what is different. Or it can help you ignore what is the same.
For example when you are creating data classes with more fields. It is more readable when you put field names in one column and data types in another.
Or if you declare a function with more parameters. It is easier to read parameter names and their data types.
Another example is something repetitive as setting string values to TextViews.
If you align the same code in columns you can easily see the mistake now (shift TextView is set with wrong string :) ), because you can visually ignore the same parts and focus on what is important.
We use code alignment like this a lot and it can be quite time consuming. So we decided to create a plugin for Android Studio called GoodFormatter.
It’s limited to 50 lines of code because it’s quite heavy to process long strings and align them. We are working on version 2.0 which will use PSI tree data structure and therefore allow us to optimize the alignment process.
But there are drawbacks too. So to sum things up let’s look at pros and cons.