Ramon Leon explains succinctly why I like lazy initialization for variables instead of direct variable access:
On thing to always remember when using lazy accessors, is that one can’t ever access the instance variable directly from inside the class. You need to always use the accessor method. I’ve waffled back and forth between direct variable access and lazy initialization several times, but I’ve finally chosen lazy initialization as my preferred method because it allows one to grow a program better by allowing existing objects to be upgraded on the fly without worrying about uninitialized instance variables. I also enjoy being lazy.
In the two main apps I support - this server and BottomFeeder, I use on the fly updating. On the fly updating is way, way simpler when you use lazy initialization. Over time, I've come to the conclusion that on the fly updating is a highly desirable trait in an application, so I like lazy initialization. I suppose it depends on what kind of code you write, and for for what purpose - but for what I do, I agree with Ramon.
Technorati Tags:
development