5.13 Side Effects

I l @ ve RuBoard

Unfortunately, C++ allows you to use side effects . A side effect is an operation that is performed in addition to the main operation executed by the statement. For example, the following is legal C++ code:

 size = 5;  result = ++size; 

The first statement assigns size the value of 5. The second statement:

  1. Increments size (side effect)

  2. Assigns result the value of size (main operation)

But in what order? There are three possible answers:

  1. result is assigned the value of size (5), then size is incremented.

    result is 5 and size is 6.

  2. size is incremented, then result is assigned the value of size (6).

    result is 6 and size is 6.

  3. If you don't write code like this, you don't have to worry about these sorts of questions.

The correct answer is 2: The increment occurs before the assignment. However, 3 is a much better answer. The main effects of C++ are confusing enough without having to worry about side effects.

Some programmers highly value compact code. This is a holdover from the early days of computing when storage cost a significant amount of money. It is my view that the art of programming has evolved to the point where clarity is much more valuable than compactness. (Great novels , which a lot of people enjoy reading, are not written in shorthand.)

C++ actually provides two forms of the ++ operator. One is variable ++ and the other is ++ variable . The first:

 number = 5;      result = number++; 

evaluates the expression and then increments the number, so result is 5. The second:

 number = 5;      result = ++number; 

increments first and then evaluates the expression. In this case result is 6. However, using ++ or -- in this way can lead to some surprising code:

 o = --o - o--; 

The problem with this is that it looks like someone is writing Morse code. The programmer doesn't read this statement; she decodes it. If you never use ++ or -- as part of any other statement, but always put them on a line by themselves , the difference between the two forms of these operators is not noticeable.

The prefix form ++variable is preferred over the suffix form variable++ because it allows the compiler to generate slightly simpler code.

(Actually, the code for simple integers is not more complex, but when we get into operator overloading, we'll see that it takes more code to write the suffix version of the increment operator ( variable++ ) than it does to write the prefix version ( ++variable ). (See Chapter 18.) To be constant, we always use the prefix form.)

More complex side effects can confuse even the C++ compiler. Consider the following code fragment:

 value = 1;  result = (value++ * 5) + (value++ * 3); 

This expression tells C++ to perform the following steps:

  1. Multiply value by 5 and add 1 to value .

  2. Multiply value by 3 and add 1 to value .

  3. Add the results of the two multiples together.

Steps 1 and 2 are of equal priority, unlike the previous example, so the compiler can execute them in any order it wants to. It may decide to execute step 1 first, as shown in Figure 5-2, but it may execute step 2 first, as shown in Figure 5-3.

Figure 5-2. Expression evaluation, method 1
figs/c++2_0502.gif
Figure 5-3. Expression evaluation, method 2
figs/c++2_0503.gif

Using the first method, we get a result of 11; using the second method, the result is 13. The result of this expression is ambiguous. By using the operator ++ in the middle of a larger expression, we created a problem. (This is not the only problem that ++ and -- can cause. We will get into more trouble in Chapter 10.)

To avoid trouble and keep programs simple, always put ++ and -- on a line by themselves.

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