[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

c# local variable lazy initialization

Posted on 2012-03-28
2
Medium Priority
?
563 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

649 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