views:

43

answers:

4

In a class which has a lazy loaded property, such as:

private Collection<int> someInts;

public Collection<int> SomeInts
{
    get
    {
        if (this.someInts == null) this.someInts = new Collection<int>();
        return this.someInts;
    }
}

Is it worth also having a property such as:

public bool SomeIntsExist
{
    get { return (this.someInts != null && this.someInts.Count > 0); }
}

And then using that property.. eg:

if (thatClass.SomeIntsExist)
{
    // do something with thatClass.SomeInts collection
}

or is this premature optimisation. Its certainly easier to roll with something like below, but it will instantiate the collection needlessly:

if (thatClass.SomeInts.Count > 0)
{
    // do something with thatClass.SomeInts collection
}

Is the compiler smart enough to figure things like this out? Is there a better way?

A: 

if the lazy loaded class is a big one and the initialization would take some time.... such a boolean make sence.

bin in case of a simple .net collection it doesnt make sence in my opinion.

Jack
+1  A: 

The compiler will not figure things like that out automatically. That means, in the last case

if (thatClass.SomeInts.Count > 0) 
{ 
    // do something with thatClass.SomeInts collection 
} 

the collection will be instantiated.

So in my opinion it depends on how expensive it is to initialize the collection - in the simple case, it is not really expensive, but the wasted memory might sum up...

EFrank
+1  A: 

Even lazy initialization of a property sounds like premature optimization. There are only very few cases I can think of where delaying the creation of an empty collection helps to solve a problem (assuming your example is not oversimplified).

But when you have to delay the collection initialization then you probably should (or even have to) optimize the Exists method, too, because lazy initialization is a critical requirement.

Daniel Brückner
+1  A: 

I would say it is worth having properties like this if you are dealing with expensive data retrieval e.g. database queries.

However, there is a flaw in your code. SomeIntsExist will only ever really give you the correct answer if the property has been accessed beforehand, if the property is lazy loaded then there may indeed be integers but they just haven't been loaded yet. It should be renamed to something like IsInitialised. I know it's an example but it's probably still worth pointing out :)

James
I didnt follow your 2nd paragraph. The SomeIntsExist property will return true only if the collection has been instantiated and has items, correct? I guess you mean if it was accessed by multiple threads it could be an issue?
PaulG
@Paul: I was assuming that you would be loading your list (as you did mention lazy loaded property) when you accessed it. However, looking at your code again you are lazy initializing the property which in this particular scenario will gain you no advantages unless you explicitly need to check that the property has been initialized.
James
Thanks for clarifying, and apologies for my confusing terminology!
PaulG