PostHeaderIcon Don’t Put Assignment Statements Inside Any Other Statements

Ok, good boy,let’s begin with an example:

Code like follow:

if (i = getch()) {
// Do something

The reason for this is simple: You want to do two simple things right one at a time.

Doing two things at once in a complex, and unworkable statement is not a good idea.

This hack flies in the face of some common design patterns. For example:

// Don't code like this
while ((ch = getch()) != EOF) {

Following this safety rule, good guy’s code looks like:

// Code like this

while (true) {
ch = getch();
if (ch == EOF)

Now a lot of people will point out that the first version is a lot more compact.

So what? Do you want compact code or safe code?

Do you want compact code or understandable code?

Do you want compact code or working code?

If we take off the requirement that the code works,

I can make the code much more compact.

Because most people value things like code that is safe and working,

it is a good idea to use multiple simple statements instead of a single compact one.

This show is designed to keep things simple and constant.Absolutely I know you are a clever programmer.

But it takes a very clever programmer to know when not to be clever.


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