1. What is code style and why it matters
Programming is not just about writing code but also about reading and working with code written by others. That is why it is so important to follow proper code style: if your colleagues have to spend hours trying to decipher your work, this will slow down the process.
In order to achieve clarity and consistency, every widely-used programming language has its own code style. In the long run, it helps programmers produce, support, and understand each other’s code. Following proper code style will guarantee that even a beginner won’t have trouble understanding what other developers did in a program.
In Hyperskill, we are committed to teaching our learners good coding habits from the very beginning of their professional journey. This includes following the code style of a language they are trying to learn.
Knowing how to write clear code in proper style is a great skill to add to your CV and show off to your colleagues and employers.
2. Issues that affect code style
It’s important to understand that the following code style is not a mere formality: it actually helps avoid a lot of problems. In our tasks, we distinguish four groups of style issues that affect code quality.
- Code style issue means that your code violates one of the rules recorded in the style guide for the language you’re using. It is highly recommended to fix such issues so that your code style matches that of other developers.
- Best practice issue means that your code does not follow the widely accepted recommendations and idioms of the language you’re using. Some features of the language can be used in an inefficient or obsolete way. Such errors are not critical but the fewer of them, the better.
- Error-prone issue means that your code contains a potential bug. Even if your code passes our automatic tests, it may behave incorrectly in some cases, which would be a problem in a real project. You should always fix such issues in your solutions.
- Code complexity issue means that your solution is poorly designed or overly complicated. Reducing code complexity makes it easier to understand, edit, and debug any given part of your program including functions, classes, modules, and so on. High code complexity means that some part of your program contains too many conditions, loops, logical operators; in other words, it does too many things. To fix this issue, you need to decompose your code into several smaller parts.
3. Levels of code quality
Based on these types of issues we evaluate overall code quality. It is an important indicator that shows you how comprehensible your code is for other developers. The estimation also takes into account how easy it is to test the code and make changes to it.
We use four levels to measure the quality of the solution code.
- EXCELLENT means that your code strictly follows the style guide and is easy to read and modify. You can be proud of yourself!
- GOOD means that your code is readable and relatively easy to edit, but it still contains some minor stylistic issues. If you have time, try to improve your code and make it impeccable.
- MODERATE means that your code follows the style guide only partially, so sometimes it may be challenging to understand.
- POOR means that your code is hard to read and modify, and it probably does not follow the style guide. Also, your solution might contain error prone issues.
We highly recommend you to keep improving your solution until you get to the point of GOOD, or better yet, EXCELLENT.
The level of code quality depends on the number of detected issues, their priorities, and the size of the whole program.
The larger your program is, the less impact best practice and code style issues will have on the quality of your solution. We assume that larger programs tend to contain some minor stylistic issues, which does not make them bad in general. At the same time, if a program consists of only a few lines of code, it is a great idea to make them perfect and get rid of all the issues. Note that error-prone issues should always be fixed regardless of the size of your program.
4. More about code style for different languages
To find stylistic issues in solutions, we use standard linters for the languages supported on Hyperskill. The linters are configured in accordance with the style guides of the corresponding languages and with regard to the needs of our learners.
We strictly recommend Java developers to follow the Oracle code conventions.
Here is the combination of linters we use:
- PMD to find best practice and code style issues;
- Checkstyle to find code style issues;
- Spotbugs to find error-prone issues.
Python developers should abide by the PEP8 style guide.
We use two linters to find all types of code style issues in the solutions:
The style guide for Kotlin developers is JetBrains coding conventions.
We use Detekt to find all types of code style issues in the solutions.
We use ESlint to find all types of code style issues in the solutions.
If you have the desire and enough time, you can help us improve the code quality scoring. Find out more: https://github.com/hyperskill/hyperstyle.