A Few Programming Disciplines
updated
10/16/02
The following Visual Basic programming guidelines should be
followed. They are listed in no particular order.
- Name all controls, forms, etc. before writing any
code.
- Do not use Global variables. If you use variables
global in a form or class, make sure they are private. Globals lead to
problems with
coupling.
- Do not use DIM to declare class level variables, use
only Private or Public, Public only rarely.
- Naming variables: Start with lowercase, use
uppercase for extra words in the variable name (or use '_' between words).
Example: maxWidgets, max_widgets.
- Declare only a single variable in each declaration.
- Always use Option Explicit. This will require all
variables to be declared.
- Always use Option Strict. This
will require explicit type conversions.
- Don't use text boxes in calculations or as the target of a
calculation. Copy the values out of text boxes into variables, copy a single
variable value into a text box.
- Don't do math operations in a output statement. Store the
value of the operation into a variable and output the variable.
- Break your program up into reasonably sized modules (forms,
classes, .bas modules). Break your modules up into reasonably sized subs and
functions. See #12 about cohesion.
- Name your methods using the same rules as variables. (See
#4)
- Modules and methods should do one thing well. This
is called
cohesion.
- If a method returns a single value (or object) use a
function.
- To communicate between methods use parameters. Use ByVal to
pass values in, use ByRef when values are passed back. Use ByRef carefully
and sparingly. (The rule changes with increased use of
classes.)ddddddddd
- Method bodies should have a maximum of 10 to 15
lines of code.
- Format code well. Indent the body of modules. Indent the
body of control structures. Use single blank lines between logical sections
of your code.
' This sub does something interesting
'
Private Sub foo(ByVal in As integer)
Dim i As integer
If i = in Then
i = i * in
Else
i = i / in
End If For i = 1 To in
picture1.print i
Next iEnd Sub
Your program should have a brief comment in the main form
that describes what the program does. The comment should include how the
program does what it does.
Each module should have a brief comment at the top of the module describing
what it does and how.
Each method should have a brief comment describing the method.
If the methods are well written, the comment can be quite brief. If the
method is not well written, rewrite it.
If a variable is used only inside a method, declare it
local to the method. (Some code incorrectly uses formal parameters to
declare variables that are used in the method and never used by the caller.)
When choosing an iteration structure: Use For when
the number of iterations is known in advance. Otherwise: Use a
DO/LOOP with a test at the bottom if the loop body must be executed at least
once, use a test at the top if no executions are a possibility. (Choose
While/Until by which reads best. Some folks use While at the top and Until
at the bottom.)
Never change the value of the For Loop control variable
inside the loop's body. (If tempted use a DO/LOOP.)
When choosing between IF/ELSE and Select/Case, use the
structure that provides the most clarity and simplicity to your code.
Always include a Case Else in your Select structures. Use
the Case Else for error checking, always have a Case for every option your
program may support.
When using Boolean variables in your program don't compare
them to True/False in IF or DO structures. (Using good variable names will
help avoid this.)
Never use magic numbers in your program. Always declare
constants. Use an "As Type" in your constant declarations. (Const
MAX As Integer = 100) Use all uppercase for your constant names:
MAX_WIDGETS.
If you ever feel the need to violate the rules above,
justify it in your comments.