Search⌘ K
AI Features

Code Quality Best Practices

Explore essential code quality best practices to write efficient and readable programs. Learn how limiting line length, breaking down functions, and organizing code improves clarity and maintainability. This lesson helps you develop skills for creating clean and manageable code that is easier to understand and debug.

Code quality best practices

As stated earlier, what we focus on here is the quality of the code, not the quality of the user experience when using our applications. When writing code, there are some things we can keep in mind to make our code better, quality-wise.

We’ll take a look at some best practices and talk about why it’s a good idea to use them.

Limiting line length

Long lines are never a good idea. Look at any newspaper and think about why the text hardly ever runs on one line across the full width of the page:

 A newspaper uses columns to limit the line length—photo by Wan Chen on Unsplash
A newspaper uses columns to limit the line length—photo by Wan Chen on Unsplash

A good rule of thumb is that if the line is wider than what can fit on the screen, then it’s too wide. We should use common sense and divide the code into several lines if needed, but do so in a way that makes sense.

Take a look at the following screenshot. The code shown here is just one single statement and could have been written on a single line, but that line would have been very long and hard to read. Instead, it has been broken up in separate lines, and the line breaks occur in a natural location so that the code is easier to read:

C++
var time = [
[seconds % 10, document.getElementsByClassName("s-2")],
[(seconds - seconds % 10) / 10 % 10, document.getElementsByClassName("s-1")],
[minutes % 10, document.getElementsByClassName("m-2")],
[(minutes - minutes % 10) / 10 % 10, document.getElementsByClassName("m-1")],
[hours % 10, document.getElementsByClassName("h-2")],
[(hours - hours % 10) / 10 % 10, document.getElementsByClassName("h-1")]
];

Some programming editors will assist you in determining the maximum length of your code lines by showing a line to ...