Welcome to MSDN Blogs Sign in | Join | Help
What The Hell Does This Code Do?

Really, take your best shot:

value += ((LPWORD)&((WORD *)data)[1])[i];

It’s not all that complicated, it’s not intentionally deceptive, but it is confusing.

Such is life while working on a large project that you didn’t write. You will have to interpret and modify code without knowing how the rest of the code works. I guarentee that you will come across lines of code like this that will take you a minute or so to understand. It can be very tough to see a bug in a line that does more than one thing at a time, and it can be very risky to change a line of code that you don’t understand (hint hint: write readable code so future developers don’t suffer).

Posted: Thursday, June 14, 2007 11:10 PM by Chris Becker
Filed under: ,

Comments

nksingh said:

That's pretty horrific...  code like that should be rewritten as soon as it is reviewed, because it really doesn't need to be so complicated.

I think that line is equivalent to:

value += ((LPWORD) data)[i+1];

or, more clearly, if the second element of 'data' has a particular meaning as a struct member or something:

LPWORD wData = (LPWORD) &data[1];

value += wData[i];

# June 15, 2007 2:39 AM

Chris Becker said:

I believe you are correct, it's likely intended as a micro optimization to avoid the cost of adding 1 to i.

It makes me :(

# June 15, 2007 3:42 AM

OJ said:

Obviously the guy before you wrote that in the hope it was going to help him keep his job. Unfortunately the world's commercial source base is full of stuff like this :(

I hope for your sake it doesn't get any worse.

# June 17, 2007 6:59 PM

Zaki Mirza said:

The world of open source/non commercial software is not all that pretty either. People love to write "wicked" code there too. Look at this,

++ * -- p1;

thats from a very well respected opensource project's code. took me some seconds to figure this out, but still, i dont think its readable enough.

# June 28, 2007 9:57 AM

Chris Becker said:

Heh, someone wins "the prize"

# June 28, 2007 3:36 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker