Johnny Le wrote:

> There is a discussion right now in CF-Jobs-Talk called "Indian Code".  It says
 > "I mentioned to the client that the code looked like it was done by a 
first
> month CF programmer. You know the type, pound signs everywhere, lots of CFIFs,
> etc. Didn't say the code sucked or anything, just that it looked amaturish."
> I tend to agree with this statement.  Somehow beginners tends to use more 
> CFIFs
> than the experts.  Have you attend any of Hal Helm's or some of the experts'
> presentations?  Their code seems to have a lot less CFIFs and more CFSET.  
> They
> just seem to organize things better.  I don't want to turn this into a big
> discussion here.  If you have some tricks or tips to help me code better, make
> good/better code, I'll greatly appreciate it.

Avoid special cases.

That's all it is.

That's the only reason why code with lots of conditional statements
look's amateurish. For some reason, rather than trying to find the
underlying logical flow of data, amateurs tend to treat things as a
set of special cases. One of my favourite exampes is from the book
"Writing Solid Code". It's in C, but you should be able to understand
it. It's a little simplified, but it gets the point across.

The code is meant to control a type of checkbox. The checkbox can be
either two-state or three-state. Two-state ones store their states as
0 and 1, whereas the three-state ones use 2, 3, and 4.

Here's the (reasonable) amateur attempt:

unsigned GetNextCheckboxState(unsigned crnt)
{
     assert(crnt >= 0 && crnt <= 4);

     /* Deal with two-state. */
     if (crnt == 0)
         return 1;
     if (crnt == 1)
         return 0;

     /* Deal with three-state. */
     if (crnt == 4)
         return 2;
     /* Or if the value's 2 or 3 */
     return crnt + 1;
}

But this handles a lot of special cases, making it error-prone.
Experienced developers learn to get rid of special cases, and try
to find the underlying general case. For the above problem, here are
a few ways of solving it.

Wraparound as the only special cases:

     if (crnt == 1)
         return 0;
     if (crnt == 4)
         return 2;
     return crnt + 1;

Data-driven, use a static array storing the next values:

     static unsigned nextVals = { 1, 0, 3, 4, 2 };
     return nextVals[crnt];

This last one is the best: no special cases and it's logic simple. All
it does is look up an array mapping a value onto the one that follows
it.

Buy some good books. Code Complete and Writing Solid Code are excellent.
So are Programming Pearls and The Pragmatic Programmer. These are books
every developer should have.

K.


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Logware (www.logware.us): a new and convenient web-based time tracking 
application. Start tracking and documenting hours spent on a project or with a 
client with Logware today. Try it for free with a 15 day trial account.
http://www.houseoffusion.com/banners/view.cfm?bannerid=67

Message: http://www.houseoffusion.com/lists.cfm/link=i:4:192524
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/4
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54

Reply via email to