Design Summary


To recap this chapter, we will finish with some issues to bear in mind when designing methods:

  • Passing reference types by reference (ref) allows you to reassign the object. The method parameter effectively becomes a double pointer, first to the client code variable reference, then to the object itself.

  • Using enumerated lists of values as method arguments rather than integer magic numbers aids code clarity and helps prevent bugs. Enumerated lists are displayed in IntelliSense allowing the programmer to accurately select an appropriate value. This will provide some degree of safety when evaluating method parameters within the method itself.

  • Try to standardize the overloading of methods. Overloaded methods can be confusing when developing client code, as several implementations of the same method are available, each accepting a different set of arguments. This confusion is amplified when each of these methods also returns a different value type. Consider that overloaded methods coded in this manner should have been coded as separate methods as they serve different purposes. Only use overloading if the methods are semantically related. Each overload of a method should achieve the same goal based on a different set of arguments.

  • When invoking a static method, refer to the class name rather than an object instance variable; static methods belong to the class itself and may be used by other objects of a different class type, without having to instantiate an actual object.

  • Exceptions form part of the semantic contract for a class. Handle only those exceptions your method is responsible for, leaving other exceptions to propagate automatically.




C# Class Design Handbook(c) Coding Effective Classes
C# Class Design Handbook: Coding Effective Classes
ISBN: 1590592573
EAN: 2147483647
Year: N/A
Pages: 90

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