Good or bad practice? Initializing objects in getter

C#Coding StyleGetter

C# Problem Overview


I have a strange habit it seems... according to my co-worker at least. We've been working on a small project together. The way I wrote the classes is (simplified example):

[Serializable()]
public class Foo
{
    public Foo()
    { }

    private Bar _bar;

    public Bar Bar
    {
        get
        {
            if (_bar == null)
                _bar = new Bar();

            return _bar;
        }
        set { _bar = value; }
    }
}

So, basically, I only initialize any field when a getter is called and the field is still null. I figured this would reduce overload by not initializing any properties that aren't used anywhere.

ETA: The reason I did this is that my class has several properties that return an instance of another class, which in turn also have properties with yet more classes, and so on. Calling the constructor for the top class would subsequently call all constructors for all these classes, when they are not always all needed.

Are there any objections against this practice, other than personal preference?

UPDATE: I have considered the many differing opinions in regards to this question and I will stand by my accepted answer. However, I have now come to a much better understanding of the concept and I'm able to decide when to use it and when not.

Cons:

  • Thread safety issues
  • Not obeying a "setter" request when the value passed is null
  • Micro-optimizations
  • Exception handling should take place in a constructor
  • Need to check for null in class' code

Pros:

  • Micro-optimizations
  • Properties never return null
  • Delay or avoid loading "heavy" objects

Most of the cons are not applicable to my current library, however I would have to test to see if the "micro-optimizations" are actually optimizing anything at all.

LAST UPDATE:

Okay, I changed my answer. My original question was whether or not this is a good habit. And I'm now convinced that it's not. Maybe I will still use it in some parts of my current code, but not unconditionally and definitely not all the time. So I'm going to lose my habit and think about it before using it. Thanks everyone!

C# Solutions


Solution 1 - C#

What you have here is a - naive - implementation of "lazy initialization".

Short answer:

Using lazy initialization unconditionally is not a good idea. It has its places but one has to take into consideration the impacts this solution has.

Background and explanation:

Concrete implementation:
Let's first look at your concrete sample and why I consider its implementation naive:

  1. It violates the Principle of Least Surprise (POLS). When a value is assigned to a property, it is expected that this value is returned. In your implementation this is not the case for null:

     foo.Bar = null;
     Assert.Null(foo.Bar); // This will fail
    
  2. It introduces quite some threading issues: Two callers of foo.Bar on different threads can potentially get two different instances of Bar and one of them will be without a connection to the Foo instance. Any changes made to that Bar instance are silently lost.
    This is another case of a violation of POLS. When only the stored value of a property is accessed it is expected to be thread-safe. While you could argue that the class simply isn't thread-safe - including the getter of your property - you would have to document this properly as that's not the normal case. Furthermore the introduction of this issue is unnecessary as we will see shortly.

In general:
It's now time to look at lazy initialization in general:
Lazy initialization is usually used to delay the construction of objects that take a long time to be constructed or that take a lot of memory once fully constructed.
That is a very valid reason for using lazy initialization.

However, such properties normally don't have setters, which gets rid of the first issue pointed out above.
Furthermore, a thread-safe implementation would be used - like Lazy<T> - to avoid the second issue.

Even when considering these two points in the implementation of a lazy property, the following points are general problems of this pattern:

  1. Construction of the object could be unsuccessful, resulting in an exception from a property getter. This is yet another violation of POLS and therefore should be avoided. Even the section on properties in the "Design Guidelines for Developing Class Libraries" explicitly states that property getters shouldn't throw exceptions:

    > Avoid throwing exceptions from property getters.

    > Property getters should be simple operations without any preconditions. If a getter might throw an exception, consider redesigning the property to be a method.

  2. Automatic optimizations by the compiler are hurt, namely inlining and branch prediction. Please see Bill K's answer for a detailed explanation.

The conclusion of these points is the following:
For each single property that is implemented lazily, you should have considered these points.
That means, that it is a per-case decision and can't be taken as a general best practice.

This pattern has its place, but it is not a general best practice when implementing classes. It should not be used unconditionally, because of the reasons stated above.


In this section I want to discuss some of the points others have brought forward as arguments for using lazy initialization unconditionally:

  1. Serialization:
    EricJ states in one comment:
    > An object that may be serialized will not have it's contructor invoked when it is deserialized (depends on the serializer, but many common ones behave like this). Putting initialization code in the constructor means that you have to provide additional support for deserialization. This pattern avoids that special coding.

    There are several problems with this argument:

    1. Most objects never will be serialized. Adding some sort of support for it when it is not needed violates YAGNI.
    2. When a class needs to support serialization there exist ways to enable it without a workaround that doesn't have anything to do with serialization at first glance.
  2. Micro-optimization: Your main argument is that you want to construct the objects only when someone actually accesses them. So you are actually talking about optimizing the memory usage.
    I don't agree with this argument for the following reasons:

    1. In most cases, a few more objects in memory have no impact whatsoever on anything. Modern computers have way enough memory. Without a case of actual problems confirmed by a profiler, this is pre-mature optimization and there are good reasons against it.

    2. I acknowledge the fact that sometimes this kind of optimization is justified. But even in these cases lazy initialization doesn't seem to be the correct solution. There are two reasons speaking against it:

      1. Lazy initialization potentially hurts performance. Maybe only marginally, but as Bill's answer showed, the impact is greater than one might think at first glance. So this approach basically trades performance versus memory.
      2. If you have a design where it is a common use case to use only parts of the class, this hints at a problem with the design itself: The class in question most likely has more than one responsibility. The solution would be to split the class into several more focused classes.

Solution 2 - C#

It is a good design choice. Strongly recommended for library code or core classes.

It is called by some "lazy initialization" or "delayed initialization" and it is generally considered by all to be a good design choice.

First, if you initialize in the declaration of class level variables or constructor, then when your object is constructed, you have the overhead of creating a resource that may never be used.

Second, the resource only gets created if needed.

Third, you avoid garbage collecting an object that was not used.

Lastly, it is easier to handle initialization exceptions that may occur in the property then exceptions that occur during initialization of class level variables or the constructor.

There are exceptions to this rule.

Regarding the performance argument of the additional check for initialization in the "get" property, it is insignificant. Initializing and disposing an object is a more significant performance hit than a simple null pointer check with a jump.

Design Guidelines for Developing Class Libraries at <http://msdn.microsoft.com/en-US/library/vstudio/ms229042.aspx>

Regarding Lazy<T>

The generic Lazy<T> class was created exactly for what the poster wants, see Lazy Initialization at <http://msdn.microsoft.com/en-us/library/dd997286(v=vs.100).aspx>;. If you have older versions of .NET, you have to use the code pattern illustrated in the question. This code pattern has become so common that Microsoft saw fit to include a class in the latest .NET libraries to make it easier to implement the pattern. In addition, if your implementation needs thread safety, then you have to add it.

Primitive Data Types and Simple Classes

Obvioulsy, you are not going to use lazy-initialization for primitive data type or simple class use like List<string>.

Before Commenting about Lazy

Lazy<T> was introduced in .NET 4.0, so please don't add yet another comment regarding this class.

Before Commenting about Micro-Optimizations

When you are building libraries, you must consider all optimizations. For instance, in the .NET classes you will see bit arrays used for Boolean class variables throughout the code to reduce memory consumption and memory fragmentation, just to name two "micro-optimizations".

Regarding User-Interfaces

You are not going to use lazy initialization for classes that are directly used by the user-interface. Last week I spent the better part of a day removing lazy loading of eight collections used in a view-model for combo-boxes. I have a LookupManager that handles lazy loading and caching of collections needed by any user-interface element.

"Setters"

I have never used a set-property ("setters") for any lazy loaded property. Therefore, you would never allow foo.Bar = null;. If you need to set Bar then I would create a method called SetBar(Bar value) and not use lazy-initialization

Collections

Class collection properties are always initialized when declared because they should never be null.

Complex Classes

Let me repeat this differently, you use lazy-initialization for complex classes. Which are usually, poorly designed classes.

Lastly

I never said to do this for all classes or in all cases. It is a bad habit.

Solution 3 - C#

Do you consider implementing such pattern using Lazy<T>?

In addition to easy creation of lazy-loaded objects, you get thread safety while the object is initialized:

As others said, you lazily-load objects if they're really resource-heavy or it takes some time to load them during object construction-time.

Solution 4 - C#

I think it depends on what you are initialising. I probably wouldn't do it for a list as the construction cost is quite small, so it can go in the constructor. But if it was a pre-populated list then I probably wouldn't until it was needed for the first time.

Basically, if the cost of construction outweighs the cost of doing an conditional check on each access then lazy create it. If not, do it in the constructor.

Solution 5 - C#

The downside that I can see is that if you want to ask if Bars is null, it would never be, and you would be creating the list there.

Solution 6 - C#

Lazy instantiation/initialization is a perfectly viable pattern. Keep in mind, though, that as a general rule consumers of your API do not expect getters and setters to take discernable time from the end user POV (or to fail).

Solution 7 - C#

I was just going to put a comment on Daniel's answer but I honestly don't think it goes far enough.

Although this is a very good pattern to use in certain situations (for instance, when the object is initialized from the database), it's a HORRIBLE habit to get into.

One of the best things about an object is that it offeres a secure, trusted environment. The very best case is if you make as many fields as possible "Final", filling them all in with the constructor. This makes your class quite bulletproof. Allowing fields to be changed through setters is a little less so, but not terrible. For instance:

class SafeClass
{
String name="";
Integer age=0;

public void setName(String newName)
{
    assert(newName != null)
    name=newName;
}// follow this pattern for age
...
public String toString() {
    String s="Safe Class has name:"+name+" and age:"+age
}

}

With your pattern, the toString method would look like this:
if(name == null)
throw new IllegalStateException("SafeClass got into an illegal state! name is null")
if(age == null)
throw new IllegalStateException("SafeClass got into an illegal state! age is null")

public String toString() {
    String s="Safe Class has name:"+name+" and age:"+age
}

Not only this, but you need null checks everywhere you might possibly use that object in your class (Outside your class is safe because of the null check in the getter, but you should be mostly using your classes members inside the class)

Also your class is perpetually in an uncertain state--for instance if you decided to make that class a hibernate class by adding a few annotations, how would you do it?

If you make any decision based on some micro-optomization without requirements and testing, it's almost certainly the wrong decision. In fact, there is a really really good chance that your pattern is actually slowing down the system even under the most ideal of circumstances because the if statement can cause a branch prediction failure on the CPU which will slow things down many many many more times than just assigning a value in the constructor unless the object you are creating is fairly complex or coming from a remote data source.

For an example of the brance prediction problem (which you are incurring repeatedly, nost just once), see the first answer to this awesome question: https://stackoverflow.com/questions/11227809/why-is-processing-a-sorted-array-faster-than-an-unsorted-array

Solution 8 - C#

Let me just add one more point to many good points made by others...

The debugger will (by default) evaluate the properties when stepping through the code, which could potentially instantiate the Bar sooner than would normally happen by just executing the code. In other words, the mere act of debugging is changing the execution of the program.

This may or may not be a problem (depending on side-effects), but is something to be aware of.

Solution 9 - C#

Are you sure Foo should be instantiating anything at all?

To me it seems smelly (though not necessarily wrong) to let Foo instantiate anything at all. Unless it is Foo's express purpose to be a factory, it should not instantiate it's own collaborators, but instead get them injected in its constructor.

If however Foo's purpose of being is to create instances of type Bar, then I don't see anything wrong with doing it lazily.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohn WillemseView Question on Stackoverflow
Solution 1 - C#Daniel HilgarthView Answer on Stackoverflow
Solution 2 - C#AMissicoView Answer on Stackoverflow
Solution 3 - C#Matías FidemraizerView Answer on Stackoverflow
Solution 4 - C#Colin MackayView Answer on Stackoverflow
Solution 5 - C#Luis TellezView Answer on Stackoverflow
Solution 6 - C#TormodView Answer on Stackoverflow
Solution 7 - C#Bill KView Answer on Stackoverflow
Solution 8 - C#Branko DimitrijevicView Answer on Stackoverflow
Solution 9 - C#KaptajnKoldView Answer on Stackoverflow