Chapter 30. Programming Adages
Second thoughts are ever wiser.
This chapter consists of guidelines and rules for practical programming.
General
Comment, comment, comment. Put a lot of comments in your program. They tell other programmers what you did, and they also tell you what you did.
Use the “KISS” principle (Keep It Simple, Stupid). Clear and simple is better than complex and wonderful.
Avoid side effects. Use
++
and--
on lines by themselves.Never put an assignment inside a conditional. Never put an assignment inside any other statement.
Know the difference between = and = =. Using = for = = is a very common mistake and is difficult to find.
Never do “nothing” silently:
// Don't program like this for (index = 0; data[index] < key; ++index); // Did you see the semicolon at the end of the last line?
Always put in a comment:
for (index = 0; data[index] < key; ++index) /* Do nothing */;
Practice coding. Practice is crucial for people involved in almost every other profession that requires a significant level of skill and creativity (e.g., artists, athletes). Help others learn to program. It’s good practice for you to go over what you already know, or think you know.
Design
If you come to a choice between a relatively “quick hack” or a somewhat more involved but more flexible solution, always go for the more flexible solution. You’re more likely to reuse it or learn from it. You’re also more likely to be thankful later on when requirements shift a little and your code is ready ...
Get Practical C++ Programming, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.