The more code I write, the more I want it to be cleaner and easier to understand. One of the ways you can achieve clearer more declarative code is to use a technique I have been fond of for some time now - lazy initialization.
Martin Fowler defines Lazy Load as a pattern in his classic book Patterns of Enterprise Application Architecture (PEAA). The main premise behind the lazy load pattern is that you have a holder for an object or data that is not filled with the contents until the code makes a request to the property or object. Thus the code is "lazy" because the work is not done unless the procedure explicitly requires it. No wasted data access or initialziation here.
Fowler breaks the pattern down into four main areas:
- Lazy Initialization
- Virtual Proxy
- Value Holder
- Ghost
I will leave it up to Mr. Fowler to explain each in detail, but for now let's focus on lazy initialization. Here is how he describes it:
The basic idea is that every access to the field checks first to see if it's null. If so, it calculates the value of the field before returning the field. To make this work you have to ensure that the field is self-encapsulated, meaning all access to the field, even from within the class, is done through a getting method.
Most of the examples given for lazy load in PEAA are in the context of data access, but there are other great uses for this technique as well. In my recent talk I gave on Implementing the Ajax.NET Framework, one of the developers in the crowd had a bit of difficulity understanding how I was using lazy initialization in JavaScript. The reasoning behind the implementation is to keep the JavaScript code free of document.getElementById() calls all over the place. Using lazy initialization you can just call the method that accesses the controls and never have to worry about when the process of finding the element in the DOM occurs. Here is an example:
JavaScript
window.onload = Window_Load;
function Window_Load()
{
txtTitle().value = "Title";
}
var mTitle = null;
function txtTitle()
{
if(mTitle == null)
{
mTitle = document.getElementById("txtTitle");
}
return mTitle;
}
On the server side you might have an entity class that needs hydration from the database. Again instead of writing numerous build-up calls into an initialization method you can create a private property that will do the work for you. With the initialization logic encapsulated into the property you are now free to use the property at any time without worrying about when the member variable is initialized.
VB.NET
Private mCustomer As Customers = Nothing
Private ReadOnly Property Customer As Customers
Get
If Me.mCustomer is Nothing Then
Me.mCustomer = DirectCast(ObjectBroker.GetObject(GetType(Customers),primaryKey),Customers)
End If
return Me.mCustomer
End Get
End Property
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.txtCustomer.Name = Me.Customer.Name
End Sub
C#
private Customers mCustomer = null;
private Customers Customer
{
get
{
if(this.mCustomer == null)
{
this.mCustomer = (Customers)ObjectBroker.GetObject(typeof(Customers),primaryKey);
}
return this.mCustomer;
}
}
private void Page_Load(Object sender, System.EventArgs e)
{
this.txtCustomer.Name = this.Customer.Name;
}
This is great in event-driven applications when do don't know which event will fire first.