Techniques to Employ
for Good Design
- Keep main( ) small and readable by writing functions that are either
modules or are for hiding details.
main() should be a skeleton of the program
- Write modules whenever possible.
- Do not allow functions that are modules to be dependent upon
#defines. Pass values to these functions rather than using symbolic
constant names within the function's code. This makes a function
more general and promotes reuse.
- See the changes made to GetRandomNumber( ) as an example.
-
Lecture 4's GetRandomNumber( )
relies upon #defines. (BAD!!!)
- GetRandomNumber() in the Cards example below has the necessary values
passed in as arguments and is now independent code (GOOD), which is
essential for it to be a module.
- Remove any large section of code that accomplishes one purpose from
main() and place it in an appropriately-named function for hiding
details.
- Replace any numbers (constants) that "magically appear" in main( ) and
in any "hiding-details" functions with symbolic constants (meaningfully
named #defines) for readability.
- "
magic number" is the disparaging phrase used by computer
programmers for "a special constant used for some specific
purpose". It's magic because its value or presence is
inexplicable without some additional knowledge.