Conventions

It is a fact that programmers can spend more time reading code written by others than creating it from scratch. That is why it is so important to write clearly and concisely. If you have a team of developers, every line of code should be uniform. In this case, it will be easy to read.

Code style conventions can help you standardize and maintain well-readable code. They are more like recommendations than a set of strict rules. If developers follow them, their code becomes steady and consistent.

Every major programming language has those conventions, and Kotlin is no different. You can find them in the Official Documentation. We follow them in all our examples and exercises. Try to stick to them, too. There is no need to learn all conventions at once. You can open them from time to time after learning new concepts. We will also supply you with some important parts of these conventions in our topics.

First steps

Let’s learn a few code conventions for Kotlin:

  1. Use 4 spaces for indentation instead of one tab. Depending on the operating system and different IDEs, one tab does not always correspond to four spaces;
  2. Omit semicolon ( ; ). Kotlin doesn’t need that;
  3. Put opening curly brace at the end of the line;
  4. Put closing curly brace at the beginning of the next line.

For example, the program below follows those rules:

fun main() { // opening curly brace
    println("Hi") // this statement is offset by 4 spaces and has no `;` at the end
} // closing brace

Conclusion

Congratulations, these were your first conventions! Carry on, and you’ll always end up with wonderful code. Remember that good code is like a good joke — it needs no explanation.

Leave a Reply