3.7 Simplicity

I l @ ve RuBoard

Your program should be simple. Here are some general rules of thumb:

  • A single function should not be longer than one or two pages. (See Chapter 9.) If it gets longer, it can probably be split into two simpler functions. This rule comes about because the human mind can hold only so much in short- term memory: three pages is about the maximum for a single sitting.

  • Avoid complex logic such as multiple nested if s. The more complex your code, the more indentation levels you will need. About the time you start running into the right margin, you should think about splitting your code into multiple procedures and thus decreasing the level of complexity.

  • Did you ever read a sentence, like this one, where the author went on and on, stringing together sentence after sentence with the word "and," and didn't seem to understand the fact that several shorter sentences would do the job much better, and didn't it bother you?

    C++ statements should not go on forever. Long statements should be avoided. If an equation or formula looks like it is going to be longer than one or two lines, you probably should split it into two shorter equations.

  • Split large single code files into multiple smaller ones. (See Chapter 23 for more information about programming with multiple files.) In general I like to keep my files smaller than 1,500 lines. That way they aren't too difficult to edit and print.

  • When using classes (see Chapter 13), put one class per module.

  • Finally, the most important rule: make your program as simple and easy to understand as possible, even if it means breaking some of the rules. The goal is clarity, and the rules given in this chapter are designed to help you accomplish that goal. If the rules get in the way, get rid of them. I have seen a program with a single statement that spanned more than 20 pages. However, because of the specialized nature of the program, this statement was simple and easy to understand.

I l @ ve RuBoard


Practical C++ Programming
Practical C Programming, 3rd Edition
ISBN: 1565923065
EAN: 2147483647
Year: 2003
Pages: 364

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net