Introduction to Computer Science
Priciples of Good Programming
Here are some principles of good programming - and some tips.
- Never use goto
- Use nouns as variable names. Example, "hours", "sum".
- Use verbs as functions names. Example "calculateFee", "getData".
- Never use the word "counter" for a counter. Instead name it the thing you are counting, for example, "days", "players", "elementsInList".
- A flag should never be called "flag". Use an adjective
or a past participle, for example, "empty", "isPrime" , or
"finished" as in
while ( !finished )
- Names of classes should be Capitalized and should reflect real-world things, for example: Person, Page, KeyStroke.
- Never use "Magic Numbers". A "magic number" is a number other than
zero or 1 that you need in the program to have things work.
If you need such a number, use a const, a #DEFINE, or, in Java, a final to give the
number a meaningful name. Use all capital letters for names of constants.
For example, if you want a a loop
to run a maximum of 100 times, write the following:
final int MAX = 100 ;
for ( i = 0 ; i < MAX ; i++ )
{
}
- Indent consistently.
- Use parenthesis for clarity.
- Avoid global variables like the plague.
- Use meaningful names for variables and functions. If you can't think of a
meaningful name for
a function, probably your function is doing too many different things.
- When ever you have an action which can be separated into a function
and the function will have a meaningful name, make a function.
- Your main should be read as close to sensible English as possible.
This is called self-documenting code.
- No function, including main(), should be longer that 1 printed page.
- When trying to solve a problem, think of an average case somewhere in the
middle and think of how you might solve it. Then, see if you solution works
for the first or smallest case and for the last or largest case. These two
cases are called the extreme cases. You may need to modify your solution
a bit so that it will work for the extremes. Hopefully you can find a
solution that
solves both the normal middle case and also works for the extremes without
resorting to using if's and else's to deal with the extremes independantly.
Remember, "Seldom is the solution that works, the best solution."
- Ideally, methods should return a boolean value of "true" on success and
"false" on failure. Any other return values can be given to the calling method
by means of a reference in the parameter list. In some cases this is not true.
For example, recursive methods sometimes need to return a meaningful value.
Also, in simple math methods a return value is the simplest approach.
- The compiler is king. It will tell you what is acceptable syntax. (Though
not all compilers follow the same language specifications.)
- Follow the principle of least access. If you don't need access to some
data, then deny yourself access to it (use "private" members in classes and const in
function parameters).
- Parameters of functions must usefully affect the behavior of the
function. (Information that is constant should be supplied internally. Don't
write a function that must take teh value 1 for example as its first parameter.)
- Make comments which describe your goals not your technique.
Definitions:
- A string is a NULL ending char array
- The name of an array is a pointer to the first element of the array.
- Rule of least access (privilege): If you don't need access to funtion/attribute,
then deny yourself access to it.
Behaviors are public in general, attributes are private.
© Nachum Danzig November 2003-2009