Solved

c# local variable lazy initialization

Posted on 2012-03-28
2
493 Views
Last Modified: 2012-04-02
can someone tell me the pros and cons of lazy initializing local variables?  my main concern about using this technique is that there is always the possibility that a variable doesn't get lazy initialized because it's being used in a whole bunch of places.  so as opposed to writing lazy initialization code in multiple places possibly, why not just initialize it the moment you declare it.
0
Comment
Question by:mmingfeilam
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 37780207
I'll have to defer for the "cons", but regarding this statement: " so as opposed to writing lazy initialization code in multiple places possibly"...

I would say that if you are duplicating your "lazy initialization" code in multiple places, then you are probably doing something wrong. Lazy initialization just means that you don't instantiate something until you need it. For local variables, I would wonder under what circumstances you were contemplating lazy initialization. If you have a lengthy function, then it may be the case that your function requires refactoring anyway. If you have a normal function, then any expensive-to-create objects which you have declared locally I would expect to have been coded in such a manner that you really wouldn't need lazy initialization--but I'm rather sleepy, so I'm sure I could be overlooking something.

From what I am reading, lazy initialization is akin to the Singleton pattern. I would inquire as to whether or not a pattern like that would suit your environment.
0
 

Author Comment

by:mmingfeilam
ID: 37784695
what i mean by lazy init is this:

MyClass var = null;

...
if(condition1)
  var = new MyClass();
else if(condition2)
...
else if(condition3)
  var = new MyClass();
else
 ...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now