Introduction to Computer Science

Priciples of Good Programming

        Here are some principles of good programming - and some tips.

  1. Never use goto
  2. Use nouns as variable names. Example, "hours", "sum".
  3. Use verbs as functions names. Example "calculateFee", "getData".
  4. Never use the word "counter" for a counter. Instead name it the thing you are counting, for example, "days", "players", "elementsInList".
  5. A flag should never be called "flag". Use an adjective or a past participle, for example, "empty", "isPrime" , or "finished" as in
    while ( !finished )
  6. Names of classes should be Capitalized and should reflect real-world things, for example: Person, Page, KeyStroke.
  7. 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++ )

  8. Indent consistently.
  9. Use parenthesis for clarity.
  10. Avoid global variables like the plague.
  11. 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.
  12. When ever you have an action which can be separated into a function and the function will have a meaningful name, make a function.
  13. Your main should be read as close to sensible English as possible. This is called self-documenting code.
  14. No function, including main(), should be longer that 1 printed page.
  15. 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."
  16. 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.
  17. The compiler is king. It will tell you what is acceptable syntax. (Though not all compilers follow the same language specifications.)
  18. 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).
  19. 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.)
  20. Make comments which describe your goals not your technique.
  21. A string is a NULL ending char array
  22. The name of an array is a pointer to the first element of the array.
  23. 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