Chapter 5: Formatting [Slide]
Formatting your code show's your professional attitude and care to details
- The functionality that you create today has a good chance of changing in the next release, but the readability of your code will have a profound effect on all the changes that will ever be made
- Your style and discipline survives, even though your code does not
Small files are usually easier to understand than large files are.
The topmost parts of the source file should provide the high-level concepts and algorithms. Detail should increase as we move downward, until at the end we find the lowest level functions and details in the source file
Use blank lines to separate different concepts in your code. Eg. Properties, functions, companion object, …
Lines of code that are tightly related should appear vertically dense. Eg. Related properties like two way data bindings
Concepts that are closely related should be kept vertically close to each other.
Variables should be declared close to their usage.
Put all instance variables of a class at the top of class.
Functions that are dependent on each other should be close Caller above the callee.
Certain bits of code want to be near other bits.
Source code dependencies must be downward, details goes down. Just like newspaper that we see most important subjects abstracted out at top of page.
Use 120 characters for line width in your IDE for better readability
Put white space between separate concepts, eg. Function arguments, math statements
There is no need to use horizontal alignment for statements, because code automatic reformating will change it
Every scope of code structure, eg. File, class, method, statement block, … is indented one level to the right. Don't break indentation rule in single line lambda or anonymous blocks, I know this is tempting for make code smaller, but it makes it less readable instead.
In one line statements like while
with captured resource loop use indentation and braces to make it more visible
When you write code in a team, agree upon a coding style and follow team rules for it