Datasheet
Chapter 1: A Quick Introduction to Programming
28
❑ Are you writing a block of code that you think you might need again in some other part of the
script, or in another script? Move it to its own procedure.
❑ Are you writing some code that you think someone else might find useful? Move it.
This isn’t a science and there are no hard and fast rules for code — after all, only you know what you
want it to do. Only you know if parts are going to be reused later. Only you know how complex
something will turn out. However, always keep an eye out for possible modularization.
Use the “Hungarian” Variable Naming Convention
You might hear programmers (especially C++ programmers) mention this quite a bit. While this is a bit
out of scope of this introductory discussion, it is still worth mentioning nonetheless. The Hungarian
naming convention involves giving variable names a prefix that indicates what the scope and data type
of the variable are intended to be. So as not to confuse matters, the Hungarian convention was not used
in this chapter, but you will find that most programmers prefer this convention. Properly used, it makes
your programs much clearer and easier to write and read.
See Chapter 3 for more on Hungarian notation variable prefixes. The standard prefixes for scope and
data types are in Appendix B .
Don’t Use One Variable for More Than One Job
This is a big no-no and a common mistake of both beginner and experienced programmers alike (but the
fact that experienced programmers might have a bad habit does not make it any less bad). Each variable
in your script should have just one purpose.
It might be very tempting to just declare a bunch of generic variables with fuzzy names at the beginning
of your script, and then use them for multiple purposes throughout your script — but don’t do it. This is
one of the best ways to introduce very strange, hard to track down bugs into your scripts. Giving a vari-
able a good name that clearly defines its purpose will help prevent you from using it for multiple pur-
poses. The moral here is that while reusing variables might seem like a total timesaver, it isn’t and can
lead to hours of frustration and wasted time looking for the problem.
Always Lay Out Your Code Properly
Always remember that good code layout adds greatly to readability later. Don’t be tempted to save time
early on by writing messy, hard to follow code because as sure as day turns to night, you will suffer if
you do.
Without reading a single word, you should be able to look at the indentations of the lines to see which
ones are subordinate to others. Keep related code together by keeping them on consecutive lines. Also,
don’t be frightened of white space in your code. Separate blocks of unrelated code by putting a blank
line between them. Even though the script engine will let you, avoid putting multiple statements on the
same line. Also, remember to use the line continuation character (
_ ) to break long lines into multiple
shorter lines.
The importance of a clean layout that visually suggests the logic of the underlying code cannot be
overemphasized.
c01.indd 28c01.indd 28 8/27/07 7:45:27 PM8/27/07 7:45:27 PM