Helper and Extension Method Guidelines
Helper and Extension Methods Guidelines Definition:
The Proper Use of Helpers Methods and Public Method Reuse Inside of Public Helper Classes
Helper and Extension Methods Guidelines Justification:
One of the most powerful aspects of Object Oriented Programming (OOP) is code reuse through encapsulated behaviours.
By created Software Entity Types that can serve more than one Client’s behaviour request with common functionality, the Code can be managed from a Single Location during the Life Cycle of the Software Entity Type.
Helper Methods allow Code Reuse..
… As Private Helper Methods, exclusively within a Single Type …
…… Or Across Many Assemblies Inside Static Public Helper Classes
Helper and Extension Methods Guidelines Standards:
There are two types of Helper methods:
Private Class Helper Methods – Located at the bottom of a Class inside of a Helper Region as Static Methods
Public Class Helper Methods – Located in Common Assemblies, documented for functionality and location, as Public Static Methods inside a Public Static Class
Helper Methods Follow all of the Standards Defined for Methods
Use Constructor State DTO objects to encapsulate long Parameter collections.
A Constructor State DTO object is one of the few times that a Nested Class object can be used.
This complies with the Open/Closed Principle as it encapsulates the parameter collection inside of an object that can be modified for future enhancements through Change Requests without modifying the Parameter code.
The functional use of the change is required inside the Method’s Code Block but the Signature Doesn’t Change.
This is Useful when a Method is Fulfilling an Interface Contract…
… There would be No Requirement to Update the Interface to Add a New Parameter Type
…… When a DTO is Used as an Encapsulated Parameter
Extension Methods are Classes that are created to give the illusion of contained Methods inside Class libraries when access to adding real Methods to those known Classes does not exist.
You can use Extension Methods to Extend an Interface
This is very useful when the many different Extension Method Classes are defined in numerous Namespaces.
This gives Developers the ability to customize the Interface Contract use with implementing Types.
Linq uses this technique to extend the IEnumerable and IQueryable Interfaces when the System.Linq Namespace is referenced.
Extension Methods are Most Useful on Primitive Types and the Reference Type of String
Use Extension Methods for these Types to add extend functionality, within the static Class and Method, when the Namespace is referenced within your calling Class:
String – Customize how the string Class is used in your Methods and Class Types
DateTime – Extend the various formatting and behaviour methods
Various Primitives like int, double and float – Create reusable simple functions that can be called within your Methods
Bool – Create Custom Functionality for Predicates
Extension Methods on instance methods can be used to decouple dependencies that may cause undesirable side effects if implemented across all method implantation
Use namespaces to limit the access to the static Extension Method Class for both instance Classes and Interface implementation contracts.
The Standards for Helper and Extension Methods
1 – Helper Method Names are Action Verbs that describe the underlying Intent of the Code
2 – Helper Methods inside of a Public Instance Class are private and static
3 – Helper Methods inside of a Public Static Class are public and static
4 – Code blocks within a Helper Method carry out a Single Responsibility and should be lean
5 – Helper Methods can consume other Helper Methods to encapsulate functionality that is not directly related to the Single Responsibility contract for the Helper Method
6 – Private Helper Methods can return Void by using Class level Properties
7 – Private Methods can return a known Type that exists within the instance Class as a private Type or return any Type referenced within the instance Class.
8 – Public Helper Classes can return any Type referenced within the calling instance Class but should avoid a void return as the knowledge of the Types acted upon will not always be known. The ref keyword can be used, primarily for Value Types but not recommended.
1 – Use Extension Methods to provide helper functionality relevant to every implementation of an Interface providing that said functionality can be written in terms of the underlying Interface as in the Linq implementation for IEnumerable.
2 – Segregate Extension Methods into logical Namespaces that can extend functionality without undue coupling. Use these Namespaces to bring in required functionality to underlying Classes through using statement references.
3 – Extension Methods that relate to basic data types, generally primitives and the string class, or Generics have a wider application and can be packaged and distributed across multiple projects
4 – Be aware that minimally useful Extension Methods tend to add “Noise” to Intellisense. Method Extensions should bring “Real” value to the Class that it is being referenced.
5 – Add an Extension Method to add functionality to a Type that you do not own and do not want to extend its functionality across all implementations of that Type
Use Helper Methods for Class Level Abstractions of Complex Method Blocks …
… Use Extension Methods to add Common Functionality to Existing Types