Chapter 23. Programming Adages
Second thoughts are ever wiser.
General
Comment, comment, comment. Put a lot of comments in your program. They tell other programmers what you did. 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.Use the prefix version of
++
and--
(++x
,--x
) instead of the postfix version (x++
,x--
). This adage does nothing for you in C, but will serve you well when you move to C++.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 or statement.
for (index = 0; data[index] < key; ++index) continue;
Design
When designing your program, keep in mind “The Law of Least Astonishment,” which states that your program should behave in a way that least astonishes the user.
Make the user interface as simple and consistent as possible.
Give the user as much help as you can.
Clearly identify all error messages with the word “error,” and try to give the user some idea of how to correct his problem.
Declarations
Put one variable declaration per line, and comment them.
Make variable-names long enough to be ...
Get Practical C Programming, 3rd 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.