Code Quality Best Practices
Learn about common best practices in coding.
We'll cover the following...
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 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:
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 ...