Formatted Code

Part of what makes something easy to read—whether it's a book, a Web site, or computer code—is its layout. Formatting your code for readability doesn't just make things easier on your eyes; it makes writing and debugging your code easier, too. As with naming conventions, there are some practices that have been standardized for Visual Basic:

• Indenting, typically 4 spaces, is used to help differentiate the "contents" of a construct from the statements that define the construct. For example, the contents of a sub are indented from the Sub and End Sub statements. Similarly, the contents of a decision structure are indented from, for example, the If.. .Then and End If statements.

• Grouping declarations of variables and constants at the beginning of modules and procedures makes them easier to find.

• Using blank lines before and after procedures, variable declarations, and decision and loop structures makes them easier to find and read.

• Using multiple, indented lines for If.. .Then statements not only makes it easier to differentiate the condition from the action taken when the condition is met, but simplifies the work necessary to add additional actions.

For example, the line

If ActiveCell.Task.PercentComplete = 100 Then MsgBox "Task complete!"

from the discussion of If.. .Then statements could have been written as

If ActiveCell.Task.PercentComplete = 100 Then

MsgBox "Task complete!" End If

Although Visual Basic supports placing multiple statements on one line, each separated from the others with a colon (:), doing so makes your code much harder to read.

• Breaking long lines into multiple lines with an underscore (_) prevents excessive horizontal scrolling. The line continuation character can be used only after an operator (such as the &, +, And, and Or operators) or between arguments of a procedure. For example:

ViewEditCombination Name:="Check Resources View", Create:=True, _ TopView:="Gantt Chart", BottomView:="Resource Sheet"

If blnThis = True And blnThat = True And blnThose = False And _ blnOthers = True Then MsgBox "Hey!"

Tip When using the line continuation character, it's customary to indent the continued lines by two characters.

• When breaking long lines of quoted text into multiple lines, the & operator and the line continuation character must be used outside of the quotation marks or they will be considered as part of the text:

MsgBox "This is a very long line of text and would scroll " & _ "horizontally for quite a ways, which is why it was broken " & _ "into several lines of code."

There are some people who would say that in this age of cheap computer memory and stunningly fast processors, efficient code isn't as important as it used to be. Perhaps they're right in a strict sense, but the idea of efficient code is more than just code that doesn't require much memory and uses as few processor cycles as possible. Efficient code also means using the best data type for the job, using object references wisely, and writing "smart" code.

Visual Basic is high level and compiled, which means that what you write is converted by a compiler into machine code for use by the computer. Modern compilers are amazingly good at optimizing the compiled code, but you are (in theory, at least) giving up some level of efficiency by not writing machine code directly. The tradeoff is that it is much, much faster and easier to write in a compiled language.

Visual Basic is also what's known as a high-level language, which means that it doesn't support methods for working with memory and the computer's processor that are available in a low-level language such as C.

Writing efficient code is as much an art as a science, and a full discussion of it is far beyond the scope of this book. There are, however, some simple things you can do to make your code more efficient:

• When working with objects, use early-binding as much as possible.

As discussed earlier in this chapter, early-binding means declaring object variables of a specific class such as Project, Resource, Form, or CommandBar. Object variables that are early-bound run faster than those that are late-bound, or that are declared simply As Object.

• When you're done working with an object variable, get rid of it. When you're finished with an object variable, set it equal to the keyword Nothing. This clears the object reference and frees up memory. For example:

Dim objAssign As Assignment

Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook

Post a comment