System.IO

Chapter 3. Naming Conventions

New objects and data types continue to emerge as Microsoft Visual Basic grows in complexity. Program code often references controls as well as variables, and in complex procedures it can be difficult to fully understand everything that is occurring in the code. How do you determine whether a particular statement references a variable or a control? Without some sort of convention to differentiate variables from controls, code is more difficult to read. For example, can you tell what type of data is being manipulated in the following statement?

TotalDue.Text = LineItemTotal.Text - Discount + TotalTax 

You could argue that using Option Strict aids clarity by making you cast data to appropriate types. This is true to a point. While I'm a strict proponent (no pun intended) for using Option Strict, I don't feel that using it alleviates the need for Hungarian notation. Consider this next statement, which is the same as the previous one but rewritten to compile with Option Strict:

TotalDue.Text = CStr(CDbl(LineItemTotal.Text) - _                 CDbl(Discount) + TotalTax) 

Now, you can deduce that TotalTax is a Double because the other elements are explicitly cast to a Double. Then again, is it? With Option Strict turned on, Visual Basic .NET allows automated casts downward; in other words, Visual Basic .NET will automatically perform a cast if it can be absolutely sure of no data loss. In this example, TotalTax could be a Single, or an Integer for that matter. The fact is, there is no way to know for sure. In addition, Discount could be a Double, even though it's being explicitly cast to a Double. Is this important? It might be during a tough debugging session.

This problem isn't related just to variables either. In the same statement, can you tell if TotalDue is a text box, a label, or some other control? How about LineItemTotal? In previous editions of Visual Basic, label controls had a Caption property. Visual Basic .NET now standardizes all controls (and forms as well) so that there are no Caption properties only Text properties. This makes it even more difficult to deduce a control type simply by noting its property reference.

To make code more self-documenting (always an important goal) and to reduce the chance of programming errors, you need an easy way to distinguish variables from controls and you need to make it easy to determine the exact data type of a variable or the exact type of a control.



Practical Standards for Microsoft Visual Basic. NET
Practical Standards for Microsoft Visual Basic .NET (Pro-Developer)
ISBN: 0735613567
EAN: 2147483647
Year: 2005
Pages: 84

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