Академический Документы
Профессиональный Документы
Культура Документы
VB.NET
Programming
Standards
Objectives
• Learn why programming standards are important
The use of standards can lead to reduced maintenance costs as well, due to a
consistent look and feel. This means you can move from one project to another
very easily, even one someone else wrote, and immediately read and
understand the code. Programming standards are used to help programmers
create a consistent structure, coding style and names of variables within an
application. Standards help a programmer create code that is unambiguous,
easy to read, and easy to maintain by other developers.
Computer Setup
It's important that you configure your development system appropriately. If
you're working with multiple developers, this is especially important, as you
want all computers to be configured similarly.
We've found that it's crucial for developers to be able to rebuild machines, from
scratch, with a minimum of effort. This means that it's imperative that data be
stored separately from the operating system, so you can "blow away" the
operating system at any time without losing data.
We suggest the following strategy, which has worked well for us:
• Create a partition just for the operating system. This partition should be
at least 10 to 12 gigabytes. This partition will be your C: drive. Make sure
you use an efficient file system, such as NTFS. The FAT and FAT32 file
systems are both inefficient and un-securable.
• Once Windows is installed and running, rename your CD-ROM (or DVD,
or other optical drive used for installations) to a "high" drive letter (we use
X:), so that other installed drives won't move the drive letter for the
optical drive later on.
• Create two more partitions: the first, (D:) will be used for your data,
projects, and applications. You should use NTFS for this partition, as
well. The other partition (E:) should be the same size as your C: drive,
and must be formatted using FAT or FAT32. You'll use this drive to
create an image of your C: drive.
• Install all your standard applications. Make sure that any personal data
files (such as an Outlook PST file) are stored on the D: drive. (We
suggest that you also set your My Documents folder to point to a folder
on the D: drive.)
• Once you have all your standard applications installed, you can create
an image copy of your C: drive. Using a tool that creates exact images of
drives (such as Ghost or DriveImage), create an image of your C: drive
and store it on the E: drive.
Your machine is now ready to be used. Should you ever need to restore your C:
drive to a working configuration, you can simply restore the saved image, which
takes minutes rather than the original hours you invested in creating and
installing your C: drive.
You may also want to select the Always ignore changes and continue
debugging option. Without this change, you'll either need to respond to an alert
each time you make changes during debugging, or even worse, restart
debugging immediately after making a change.
Option Strict
In VB.NET, Option Strict is Off by default -- this is a poor decision, in our eyes,
since it allows the same sort of code you might have written in VB6, including
embarrassing type conversions. You can easily set Option Strict On in VB.NET
for all new projects you create by going into Tools | Options | Projects | VB
Defaults and setting the Option Strict to On within Visual Studio .NET.
Naming Conventions
While you're developing new code, you'll spend a large portion of your time
naming things. You'll need to propose names for variables, procedures, and
more. The following sections propose standards for providing names, allowing
for consistency in your code.
Naming Variables
Although it's subject of much debate, we believe that you should adopt a strict
and consistent naming convention for variables in your applications. Because
variables describe the behavior of your code, it's useful to be able to glance at a
variable and determine its type (and perhaps its use) just given its name. We've
adopted a commonly used convention, prefixing variables with a two- or three-
letter prefix indicating the data type of the variable.
Taking into account our own coding styles, we suggest that you consider
adopting these same rules when naming variables:
• Make all variable names mixed case. That is, each word or
abbreviation within the variable name should be capitalized.
Scope Prefixes
A variable's scope defines the locations within your application that a particular
variable may be inspected or modified. For example, a local variable is one
Prefix Description
Local variables should not have a prefix. This will distinguish them as being
local and not have a scope outside of the current procedure:
• Parameters to procedures
• Enumerations
Basically, we suggest that you don't use Hungarian naming on any name that's
publicly viewable, in any IntelliSense list.
Control Prefix
Label lbl
TextBox txt
Button btn
LinkButton lnk
ImageButton img
HyperLink hyp
DropDownList ddl
ListBox lst
DataGrid grd
DataList dlst
Repeater rep
CheckBox chk
CheckBoxList cbl
RadioButtonList rbl
RadioButton rdo
Image img
Panel pnl
PlaceHolder plc
Calendar cal
AdRotator ad
Table tbl
RequiredFieldValidator reqv
CompareValidator cmpv
RangeValidator rngv
Control Prefix
RegularExpressionValid rexpv
ator
CustomValidator custv
ValidationSummary vsum
Xml xml
Literal lit
CrystalReportViewer crv
Table 3. Web Form control prefixes
Label lbl
LinkLabel lnk
Button btn
TextBox txt
MainMenu mnu
Checkbox chk
RadioButton rdo
GroupBox grp
PictureBox pic
Panel pnl
DataGrid grd
ListBox lst
CheckedListBox clst
ComboBox cbo
ListView lvw
TreeView tvw
TabControl tab
DataTimePicker dtp
MonthCalendar cal
HScrollBar hscr
VScrollBar vscr
Timer tim
Splitter spl
DomainUpDown dup
NumericUpDown nup
TrackBar trk
ProgressBar prg
RichTextBox rtxt
ImageList ilst
HelpProvider hlp
ToolTip tip
ContextMenu cmnu
ToolBar tbar
StatusBar sbar
NotifyIcon nic
OpenFileDialog ofd
SaveFileDialog sfd
FontDialog fd
ColorDialog cd
PrintDialog pd
PrintPreviewDialog ppd
PrintPreviewControl ppc
ErrorProvider errp
PrintDocument pdoc
PageSetupDialog psd
CrystalReportViewe crv
r
Table 4. Prefixes to use with WinForm controls
If you take care to name your controls with these common prefixes, then you'll
be able to identify the type of a control reference in your code without having to
refer back to the page or form.
Menus Name
File mnuFile
File|New mnuFNew
File|Open mnuFOpen
Edit mnuEdit
Edit|Copy mnuECopy
Edit|Paste mnuEPaste
Window mnuWindow
Table 5. Menu file naming conventions
When you use this convention, all members of a particular menu group are
listed next to each other in the object drop-down list boxes (in the code window
and property window). In addition, the menu control names clearly document
the menu items to which they are attached.
For derivative controls, such as an enhanced list box, extend the prefixes above
so that there is no confusion over which control is really being used. A lower-
case abbreviation for the manufacturer could be added to the prefix.
DataSet ds
DataTable dt
DataView dv
DataRow drw
Connection* cnn
Command* cmd
DataAdapter* da
CommandBuilder* bld
DataReader* dr
Table 6. Prefixes for ADO.NET classes
Of course, if you need two or more of the same data type in the same scope,
you can use the prefixes listed in Table 6 as part of variable names. Example:
Coding Conventions
In addition to other naming conventions, we suggest carefully naming data
structures and procedures. We also suggest specific conventions for
indentation and commenting.
Interfaces
Begin interface names with "I". For example:
Interface ICustomers
…
End Interface
Enumerations
Enumerations should have a descriptive name, followed by the suffix Enum. For
example:
Constants
Constant names should be UPPER_CASE with underscores (“_”) between
words. This will help distinguish your constants from any other type that you
might use in your application. Be sure to supply a data type for your constants
—of course, with Option Strict on, you won't be able to leave out the data type.
If you do not type your constants, they will be of the type Object. Your constant
declarations might look like this:
For example:
Try
…
Catch exp As Exception
…
End Try
If you create your own classes inheriting from the Exception object, make sure
you follow the same convention used by Microsoft: name the class with a
meaningful name, followed by Exception. For example, your exception class
Naming Procedures
Naming procedures is another area in which careful planning will help make
your applications more understandable, and more maintainable. Selecting a
standard within your development organization will make it easier for all your
developers to work together.
We've gathered a list of suggestions. We've found that these suggestions make
our development tasks go more smoothly. When naming procedures, we follow
these rules, in general:
• Follow the noun with the action that will be performed on the object
represented by the noun.
Here are some examples of procedure names that follow these conventions:
• FormShow
• FormInit
• StateLoad
• EmployeeLoad
As you can see, these examples place the noun, or object, first. This convention
mirrors the way that in object-oriented languages, the object name is placed
first, then the method or action you wish to perform on that object. Objects are
always nouns, and actions are verbs. Therefore, we suggest that you follow this
coding style when creating names for your procedures.
Below is a sample XML document that you might place at the top of each public
function, sub and even property that needs to be described in your classes. By
keeping a standard format, you could easily write a utility to parse these
comments in your files and write out a typical XML document.
'''************************************************************
'''<VBComments class="" name="" type="Function/Sub">
'''<summary>
'''SUMMARY HERE
'''</summary>
'''<example>
'''EXAMPLE HERE
'''</example>
'''<param name="" type=""> DESCRIPTION HERE </param>
'''<returns> RETURN VALUE TYPE HERE </returns>
'''</VBComments>
'''************************************************************
• Variables, controls, and routines should be named clearly enough that in-
line commenting is only needed for complex or non-intuitive
implementation details.
Indentation
Consistent code indentation can mean the difference between easily readable
code and a hopeless nightmare. Happily, indentation has become a non-issue
in Visual Studio .NET. If you allow Visual Studio .NET to perform its smart
indenting, it will take care of the indentation for you. We strongly suggest that
you allow Visual Studio .NET to handle the indentation chores. The standard
indentation depth is 4 characters, and we don't see any reason to change it to a
larger value. For the purposes of fitting more code into the limited space in this
book, we've used a value of two. The good news is that it doesn't matter: If you
simply select an entire procedure, then press Shift+Tab, Visual Studio .NET will
reformat the code for your particular tab setting, making it easy to adapt code
formatted for a different tab size to your own preferences.
Summary
Programming standards are imperative for multi-developer organizations, and
will make the job of even a single developer much easier. Since much of your
development time is spent in maintenance mode, you'll want to adopt standards
that make it easier for you to maintain and manage your projects. In this
chapter, we suggested some easily adopted standards that can by adapted for
your own needs, including:
• Computer setup
• Naming conventions