PostHeaderIcon Eliminate Side Effects

Hi,everybody,long time I have not update this site.just because a software I have to finish.

Now, It’s done. let’s begin our tutorial again.

The soft I developed is something  about MFC, combine with ADO、MSXML、SOCKET and THREAD,

I’ll introduce them later~

Now, let’s begin about today’s topic:Eliminate Side Effects

what is Eliminate Side Effects?

In C++,It let’s you use operators like ++ and — to make your code very compact.

It also can be used to create code that generates ambiguous results, as well as being unreadable.

For example, is the result of the following code:

i = 0;
// Bad code
array[i++] = i;

There are two sub-expressions involving i. These are:

1.i++
2.i

The compiler is free to execute them in any order it wants to.

So what compiler you are using and even what compilation flags are used can affect the result.
There is no reason for trying to keep everything on one line.

You computer has lots of storage and a few extra lines won’t hurt things.

A simple, working program is always better than a short, compact, and broken one.

So avoid side effects and put ++ and — on lines by themselves.

 If we rewrite the previous example as:

i++;
array[i] = i;

The order of the operations is clear not only to the compiler but to anyone reading the code.

So,clever boy,write statements that perform one operation only.

Don’t use ++ and — except as standalone statements.

16,979 views

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Copyright © 2010 - C++ Technology. All Rights Reserved.

Powered by Jerry | Free Space Provided by connove.com