|
|
|
How to Write Reliable Code
|
|
2 points
by Tim1776
5619 days ago
|
|
I've learned the most important key to making reliable software, a method that has served me well for over 20 years. Follow this simple rule: Write a comment for each line of code, making it a complete sentence to express a complete thought. You should aim to write for two computers in parallel -- one is the CPU, and the other is between your ears. Why does this technique work to make software more reliable? Line-by-line comments serve as a kind of logical checksum for the code, such that if the code and its comment differ in function, then you know immediately that work needs to be done in that area. Often, a coding bug will be revealed as you are writing the comment that goes along with the code. More broadly, line-by-line comments serve to hold the intellectual context and keep your mind connected to reality. |
|
Secondly, a major problem with line-by-line documentation is that you will soon end up in a situation where the documentation does not agree with the code. A bug was fixed, but the comment wasn't updated or vice versa.
Additionally, what makes for good comments in complex systems is not "what" a specific line of code does. Good programmers can read the code they are paid to write in. A good comment identifies "why". Why is this code necessary, why is it being solved in this specific way, why should I, as a developer of this system, care about this code.