Skip to content

Keep it Simple Stupid.

June 19, 2008

In my youth, many was the time that my father, the engineer, would look over my shoulder at some project I was doing for scouts or on the computer or whatnot and say those 4 wise words. 

In refactoring some code at work, I ran into these lines and felt my skin crawl

if (agency != null)

                return false;

            else

                return true;

 

I thought everyone knew comparasions in C# return boolean values themselves.  He should have just written it like this to get the same effect.

return agency == null;

 

Or to get to the guts, This:

00000061  cmp         dword ptr [ebp-40h],0

00000065  jne         0000006F

00000067  nop             

00000068  mov         esi,1

0000006d  jmp         00000071

0000006f  xor         esi,esi

00000071  mov         eax,esi

00000073  and         eax,0FFh

00000078  mov         ebx,eax

0000007a  nop             

0000007b  jmp         0000007D

 

Or this:

00000061  cmp         dword ptr [ebp-40h],0

00000065  sete        al  

00000068  movzx       eax,al

0000006b  mov         ebx,eax

0000006d  nop             

0000006e  jmp         00000070

 

Half the code, less memory and processing.  It adds up.  There is something to be said for elegance too.

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: