Posts

Showing posts with the label if

#3. The condition in an "if" statement should be a simple boolean expression with no side effects

The expression inside an  if statement should be boolean, simple, and have no side effects. In C++, every statement is an expression of a certain type. It may be an ordinary type, such as  bool ,  int or a user-defined type such as  Customer ), or a special type called  void which basically means "no type". In C++ the expression inside the pair of brackets will be understood as a boolean expression. Some people carry the habit from their C days and pass an as  int an argument to the  if . Other people even use a pointer, so that the  if will check whether it is NULL and will evaluate to true in that case. Don't do it - it makes the code less readable and a step closer to being error prone. Guideline #2 advised you to avoid implicit type conversions. This is a very good place to start applying it. The  if  statement requires a bool , so why pass it anything else? Don't overcomplicate things by putting several actions tog...