Suggestions for better programs
-
Keep your programs simple. The less complex, the fewer bugs.
-
You can learn a lot from a compiler. If you don't understand a feature
or keyword, write a test program to try it out. See what happens when
you use the feature. Use the debugger to watch a program
execute.
-
Learn the editor of the environment you are using. Learn the tips and
tricks and keyboard combinations. The time you spend in learning
how to make the editor work will save you time in the long run.
-
Check the operator precedence chart when you are writing a complex
expression. Make sure the operators are done in the order you
expect; use parentheses to force the order that you want.
-
Programmers usually write a condition like y == 5 with the variable
name on the left and the constant on the right. If you get the habit of reversing the
condition, like 5 == y, if you accidently write = instead of ==,
the compiler will catch 5 = y because that isn't a legal expression.
-
Use your text editor to search for all occurrences of = in your program and check that
you used the operator you meant to use.
-
Programs should always validate the correctness of input values to prevent
incorrect data from affecting a program's calculations. Echo-print the input data
to verify each value is where it belongs. This is crucial because an
input failure in C++ does NOT produce an error message or terminate the
program.
-
Avoid using variables of the same name for different purposes in a program.
Although the compiler handles it, it can be confusing.