Function and Variable Names

[ LiB ]

Function and Variable Names

Naming your variables correctly can help solve a lot of problems in programs. Every once in a while you might come across the problem of not knowing what a variable does. You will have to backtrack and follow your program from the beginning. A way to solve this problem, however, is to give your variables very easy-to-understand names. This can help reduce, if not eliminate, forgetting what a variable does.

Names

When declaring and defining a variable, make sure you come up with a name that easily describes what the variable does. For example, when writing the Hello World program, I could have easily named the variable anything. I could have chosen names like

 i$       row$       howareyou$       _123$       hellostr$ 

but there is a reason I didn't. For most of them, they don't make sense. For example, what does howareyou have to do with a string? (Unless of course, I am asking how the user is feeling.) You may be wondering, though, why I didn't pick hellostr$ . In this program, it would have been fine; however, in most programs, the contents of a variable change. Because they usually do change, creating a variable that tells exactly what is inside the variable, rather than what kind of data it contains, can create the exact same problem it is supposed to fix. If you changed the program so that hellostr$ was equivalent to "Today is my birthday," the hellostr$ no longer makes sense in the context, and you might have to change all of the variable names in the program.

Naming Format

The format of your variable names is largely up to you. There are no rules set in stone as to how to name your functions and variables. The only requirement is that your format stays consistent throughout the program.

Here are some different ways to format the same variable.

 hellostr$       Hello_Str$       helloStr$       HelloStr$       Hellostr$ 

As you can see, these variables are all the same. However, each name is slightly changed.

The first variable is my choice for regular variables. I keep it simple: both words are in lowercase. Some people use two words separated by an underscore (a key achieved by pressing Shift+Dash). Others use the two words in different capitalization patterns.

Functions can also be named in similar ways. For example:

 PrintString       printstring        Print_String       printString       PrintString       Printstring 

I usually choose the first method for functions: two joined words that are both capitalized. Once again, feel free to pick whichever you like, but make sure you stick with it.

Some other naming formats you might like to vary are constants, global variables, and array names.

I usually uppercase all the letters in a constant, like this:

 Const CONSTANT = 1 

My global variables are usually the same as regular variables, like this:

 globalvar = 10 

A lot of people prefer to add a g_ to the beginning of global variables. I choose not to, but feel free to try it.

For arrays, I keep it simple. I use one word if possible, and I keep it lowercase.

 Dim array(numofelements) 

[ LiB ]


Game Programming for Teens
Game Programming for Teens
ISBN: 1598635182
EAN: 2147483647
Year: 2004
Pages: 94
Authors: Maneesh Sethi

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net