|
Keeping Code Clean & Lean
When looking for new ideas I commonly
download free programs to get ideas and learn new techniques.
I am always amazed how poorly most programs
are written.
Variables written such as
$TheCostofMyWebsite.One of the most primary rules of programing
is to keep everything in lower case. If you make a case error,
you would never find it. Compare $TheCostofMyWebsite and
$TheCostoFMyWebsite when they are 200 lines apart. Good luck
finding that typo.
Another poor characteristic I see is using
single digit variables like $a or $b. You might be saving a few
cpu ticks, but when anyone is trying to read or use your program,
it takes much more brain power to decipher the code.
Using clear concise names is very importnat.I
hear from programers, "I know what it means and that is
all that is important".
OK, you know what it is TODAY! But 2 years
from now, assuming your program is of the caliber to sustain that
long of a useful duration, and you go back to make adjustments.
Will you still know what $a stands for?
Not likely.
I also see highly complicated methods to
achive simple tasks.
Most programs can be cut into a fraction of
their size with some simplification. That simplification of the
primary elements will make the developmet of more complicated
aspects much easier.
Sometimes its something simple like setting
up a date format.
I have seen sub routines 100 lines long to
set up simple date formating, when all the programer needed to do
was parse the /bin/date program with one line of code.
In those cases, it's just inexperience and
the absense of knowledge. Something we are all guilty of at some
level.
If you plan to write sucessful code, it
needs to be lean. It needs to be clean and understandable. Any
other programer should be able to read your code and understand
what it does.
In most cases, you wont be sharing. But you
will need to keep it working.
I Have written thousands of programs and
often need to update programs I have not seen for several years.
By keeping all my code in the same formatting, all my naming
makes sense and all the unfamiliar elements are commented for
future reference.
When I pull up a program that is 10 years
old, it reads just like one I wrote yesterday. I will admit my
coding is much better today and the applications are more
powerful, but I have followed the same basic rules I was taught
by programers in the past.
I know microsoft is changing the rules,
trying to trip up any UNIX potential with messy code and an O/S
that mixes up cases. But if you follow the UNIX standards, they
also work on windows. The inverse is not true.
In writing the CAPTCHA
for Perl program I wanted a simple
program that could do what more complicated programs could do in
with less code. The entire program is just a few lines and is a
stand alone application. Matching other programs that require up
to 5 perl modules.
Keep it simple!
Don't make more out of the program than it
needs to be.
I have written programs with over10,000
lines of code. When something becomes that big, nearly 300
printed pages, it is critical that you have it well organized.
It may not seem like a big deal when you
are writing quick and dirty scripts to format files and run
server aps. But when you get into millions of credit card
transactions, a line of bad code can be devistating.
At one time a misspelled variable cost me $20,000.00
in unprocessed credit card transactions.
You cannot afford to make mistakes. Start
with a good regiment now. If you grow up doing things in a sloppy
manner, it may be too hard to correct your poor methods later. It
will be like trying to quit smoking.
Bad habits are hard to break.
|