Hornets' Nest Tomfoolery
Yet again, this old argument has been started.
Although some of the people who read his blog (like these) are prime tits, Rory's put forward about the only lucid and sensible argument I've heard on the matter. Live and let live, just 'cos it's good for you, doesn't make it good for me, just because it's 'harder' doesn't make it more worth doing. More importantly, just because it looks more complicated, doesn't a) make it so, or b) make it better.
That's all I've got to say on that.
When I Paul Kimmel's excellent VB.NET unleashed, I sat and pondered just what on earth anyone would use MyClass for (as in MyClass.New()) Why not just use MyBase?. I now know. Whilst putting together a load of overloaded constructors I discovered its use. I'll explain:
One of the classes I've built, BlogEntry, uses an internal ArrayList to store Comments. Arraylists are great, but they do need initializing before you can use them (I'm always catching myself out with that if I have an empty arralist). Rather than
If Not MyArraylist is Nothing Then...or anything like that every time it's referred to, I just created an empty ArrayList in the deault constructor.
Then I got on to more constructors (I got up early this morning and had a boit of a constructor fest!) and found myself writing MyArrayList = new ArrayList over and over again.
That. THAT was when I remembered the wise words about the MyClass keyword.
Now, the default, unparameterized constructor has all the initialization bits and bobs that need to be done, and all the overloads just add on the bits that are specific to them. I thought that was kinda clever. I think I shall make an effort to use it more often.
No comments:
Post a Comment