Naming Subs and Functions

Like humans, pets, and hurricanes, every Sub and Function procedure must have a name. Although it is perfectly acceptable to name your dog Hairball Harris, it's usually not a good idea to use such a freewheeling attitude when naming procedures. When naming procedures, you must follow a few rules:

i You can use letters, numbers, and some punctuation characters, but the first character must be a letter.

i You can't use any spaces or periods in the name.

i VBA does not distinguish between uppercase and lowercase letters.

i You can't embed any of the following characters in a name: #, $, %, &, @,

i If you write a Function procedure for use in a formula, make sure the name does not look like a cell address (for example, AC12).

i Names can be no longer than 255 characters. (Of course, you would never make a procedure name this long.)

Ideally, a procedure's name should describe the routine's purpose. A good practice is to create a name by combining a verb and a noun — for example, ProcessData, PrintReport, Sort_Array, or CheckFilename.

Some programmers prefer using sentencelike names that provide a complete description of the procedure. Some examples include WriteReportToTextFile and Get_Print_Options_and_Print_Report. The use of such lengthy names has its pros and cons. On the one hand, such names are descriptive and unambiguous. On the other hand, they take longer to type. Everyone develops a naming style, but the main objectives should be to make the names descriptive and to avoid meaningless names such as DoIt, Update, and Fix.

0 0

Post a comment