Programming with Class
|
Get the Most From OOP
Use these 10 top techniques to maximize the benefits of object-oriented programming.
by Deborah Kurata
Reprinted with permission from Visual Basic Programmer's Journal, June 2001, Volume 11, Issue 6, Copyright 2001, Fawcette Technical Publications, Palo Alto, CA, USA. To subscribe, call 1-800-848-5523, 650-833-7100, visit www.vbpj.com, or visit The Development Exchange.
| Taking a step back and examining your application is useful from time to time. You can think about whether the application does what it's supposed to do, whether you're following best practices, and whether better ways exist to achieve your goals. This column presents 10 top object-oriented programming techniques to help with your evaluation.
1. I Do Declare Be sure to use the correct scope. In most cases, you want the object variable to be private. If you need the object only in a particular routine, you can declare it private to that routine using Dim. If you need it throughout the class or form module, you declare it at the top of that class or form using Private. By convention, you prefix variables declared at the module level with an "m_" for module-level or member variables. If you need to access the object variable from outside the class, don't make the variable Public. Rather, define a Property Get procedure to expose the object variable. This prevents other code from using the variable in any other way than you intend. Always use the class name and not "As Object" in the declaration because using "As Object" makes the object variable late-bound. This means Visual Basic doesn't know the type of object you will use until run time, so it can't give you auto list members (the dropdown list of the object's properties and methods). More importantly, at run time you take a significant performance hit over early-bound objects. Finally, don't use the "New" keyword on the declaration. Using the New keyword creates the object automatically any time you reference it. This has a slight performance impact because each property and method call on the object must check to determine whether the object is created. Using the New keyword can cause debugging nightmares because the application re-creates the object automatically after it's destroyed if you reference the object again inadvertently. Understanding this is even more important as you prepare to move to Microsoft .NET because the New keyword has a different meaning. In Microsoft .NET, the New keyword allows you to create an object when you declare it: This statement creates a new Customer object. 2. Clean Up After Yourself Use this code to release an object: 3. Encapsulate Encapsulation is sometimes referred to as information hiding because you hide the object's data within the class. If other classes need access to the data, you can expose the data using property procedures (property statements in Microsoft .NET). This action gives the class control over how users can view or update the data and prevents other objects from corrupting that data. Encapsulation simplifies the interactions between objects. An object can use another object without knowing all its data or how its data is maintained. For example, a Customer object might have name and address properties, as well as a phone number, credit history, and other related fields. If an Invoice object wants to use a Customer object, it can request the name and address for the invoice without needing to know where the Customer object obtained that data or what the other data elements are. In my previous column, I demonstrated how to encapsulate the Document Object Model (DOM) for Extensible Markup Language (XML) in a wrapper class. This allows you to interact more easily with an XML file without needing to know the DOM's details. 4. Watch Your State Some OOP literature suggests that an object without properties is not an object. Ignoring this semantic issue, you can create classes with methods and no properties. You often want to do this when working with COM+ or Web development. An Active Server Page can execute a method on a component to perform specific processing, then release the component without concern for managing object state. 5. Pass Parameters For example, the PlaceOrder method of a stateless Customer object needs the customer information passed to it as parameters. The PlaceOrder method then uses this information to perform the necessary processing. As you define your methods' parameters, be sure to make them extensible. For example, this line of code is not extensible: You must pass in the three parameters to call this method. If you decide later you also want the phone number on the order, you must change the function signature, breaking compatibility and every developer's code that uses this method. A better technique: Pass the parameters in a container. You can use a recordset, variable array, or XML string to pass in parameters more generically. This tip is less necessary as you move to Microsoft .NET because you can do function overloading, whereby you can have the same function with two different sets of parameters: 6. Use XML The best thing about XML is that it's platform- and vendor-independent. XML is neither a Microsoft technology nor a Sun technology. Rather, the World Wide Web Consortium (W3C) controls this standard. XML is the upcoming data access standard in Microsoft .NET, so using it now will give you a head start. 7. Define Interfaces For example, you can develop a mailing list label-printing component and define the standard interface the component requires. In this example, that interface includes name and address information. Any class that implements this interface can then use the component without making any changes to it. A customer class that implements the label-printing interface can use the component to print customer mailing labels. An employee class that implements the label-printing interface can use the component to print paycheck mailing labels and so on.
8. Inherit Base Functionality For example, you can define base functionality for a Customer object that allows you to retrieve and save data and calculate a discount. You can then define a government Customer object that inherits all the customer functionality but overrides the discount calculation to give a better discount to government customers. You can't do this one now, but it's in Microsoft VB.NET, so you will be able to do it soon. For now, you might consider where inheritance makes sense in your application to prepare for this feature. 9. Model It However, a common mistake is to take that domain model and attempt to code from it. Rather, you need to morph that domain model into an implementation model that defines how you should build the classes. You need to consider several factors when morphing the domain model into an implementation model (see the sidebar, "Work With Object Models"). 10. Know It, Live It Deborah Kurata is a cofounder of InStep Technologies Inc., a consulting company that specializes in designing and developing Web and Windows applications (www.insteptech.com). She's the author of Doing Objects in Visual Basic 6 (Sams), which focuses on a pragmatic approach to object-oriented design and development. Reach her by e-mail at deborahk@insteptech.com. |
