#1. Do one thing at a time

Do exactly one thing in each instruction, and put each of them in a separate line of code. This will enhance the readability of your code and make it easier to debug and possibly change in the future.

It should not be possible for an instruction to have several potential causes of failure.

If an instruction is a function call, its arguments should be trivial expressions. If its purpose is to get some result, don’t do anything complicated with it once you get it; instead, place the result in a variable and do subsequent operations with it in the following lines.

If you write code in this way you will find out that it follows your train of thought quite closely. For this reason, a future reader (whether it is another programmer or you) will have a better chance of understanding it.

Bibliography

[McConnell 2004]
Steve McConnell: Code Complete, 2nd Edition, Microsoft Press, 2004.

Chapter 31: "Layout and Style". Section 31.5: "Laying out individual statements" and its Subsection "Using Only One Statement Per Line" (pages 758-761).

Comments

Popular posts from this blog

#2. Make all type conversions explicit

Welcome to The Deep Blue C++

#13. Organize your code in classes