Monday, September 25, 2006

I have written an article on Visual Studio Code Snippets - how to use them, and how to create your own. I have provided there a couple of snippets that I use on daily basis. You can also check Mads Kristensen's blog where he provides a snippet for "safe events". Today I have thought of yet another useful snippet...

There often happens that you need to have some kind of property that does some calculation yet you do not want it to be a method. Maybe it is because of the ease of use or some other reason? But as far as best practice tells you, the property is expected not to perform any kind of lenghty operation. So in order to be able to make a property that does some time consuming task the result of which can be then quickly retrieved any time, you do a what is called a Lazy Loading or Lazy Initialization - one of the most useful patterns.

Suppose that we have a tree structure of some operations. Each operation is either an atomic operation or a composite operation consisting of sub operations - a simple composite pattern. Each atomic operation has a property that shows the number of arguments the operation requires to execute. So to count a total number of arguments required by the tree of operations you would have to recursively sum the number of parameters from all sub operations. And that is the operation that can take some time to perform given a large tree of operations. So there is a a choice of making exposing this functionality as a method or doing the Lazy Initialization.

If you go the "lazy" way, you will end up with something like this:

If using 2.0 version of the .NET Framework, you can do better and use the Nullable type for the field. And that is exactly the code snippet I provide. With it you can create such a lazy initialized property very quickly. Just type: "propn" and what you get is:

Just fille the green places.

I have added the snippet to my collection of Snippets.zip (2.23 KB). Feel free to use it.

kick it on DotNetKicks.com

Tuesday, October 20, 2009 9:58:22 PM (Central European Standard Time, UTC+01:00)
Excuse me. If we fall, we don't need self-recrimination or blame or anger - we need a reawakening of our intention and a willingness to recommit, to be whole-hearted once again.
I am from Zaire and learning to read in English, please tell me right I wrote the following sentence: "The conditions academic for the colony shall be found to be a population to the credit resolution interacting the cities or excessive credentials."

Regards ;) Anselme.
Tuesday, January 26, 2010 5:50:32 AM (Central European Standard Time, UTC+01:00)
How are you. I like coincidences. They make me wonder about destiny, and whether free will is an illusion or just a matter of perspective. They let me speculate on the idea of some master plan that, from time to time, we're allowed to see out of the corner of our eye. Help me! Help to find sites on the: Cortland tennis ball machine. I found only this - http://www.mamadspizza.com/Members/TennisBallMachine/tennis-ball-machines-for-sale. Tennis ball machine, the calories' control with lal masjid serves many. They very use unlimited coins and 1970s, tennis ball machine. With love ;-), Juliet from Pakistan.
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview