O'Reilly logo

Practical C++ Programming, 2nd Edition by Steve Oualline

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 30. Programming Adages

Second thoughts are ever wiser.

Euripides

This chapter consists of guidelines and rules for practical programming.

General

  • Comment, comment, comment. Put a lot of comments in your program. They tell other programmers what you did, and they also tell you what you did.

  • Use the "KISS" principle (Keep It Simple, Stupid). Clear and simple is better than complex and wonderful.

  • Avoid side effects. Use ++ and -- on lines by themselves.

  • Never put an assignment inside a conditional. Never put an assignment inside any other statement.

  • Know the difference between = and = =. Using = for = = is a very common mistake and is difficult to find.

  • Never do "nothing" silently:

    // Don't program like this
    for (index = 0; data[index] < key; ++index); 
    // Did you see the semicolon at the end of the last line?

    Always put in a comment:

    for (index = 0; data[index] < key; ++index) 
            /* Do nothing */;
  • Practice coding. Practice is crucial for people involved in almost every other profession that requires a significant level of skill and creativity (e.g., artists, athletes). Help others learn to program. It's good practice for you to go over what you already know, or think you know.

Design

  • If you come to a choice between a relatively "quick hack" or a somewhat more involved but more flexible solution, always go for the more flexible solution. You're more likely to reuse it or learn from it. You're also more likely to be thankful later on when requirements shift a little and your code is ready ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required