Seven Deadly Sins of Programming - #3
I'm sure this is going to be a contentious one. I don't have a good story about this one, so I'm just going to go right to the sin.
Sin #3 - Overuse of Virtual
There are two schools of thought when it comes to virtual. They are:
- Always use virtual
- Only use virtual when absolutely necessary
The first group's argument is that original designer of a class is a poor judge of how the class might ultimately be used. Making all methods virtual allows the users of the class to easily extend/modify the behavior. Non-virtual methods make more work for the user.
(or that's the argument I've heard - please comment if there's something I missed...)
The argument is true. Virtual does make extensibility easier. The problem with virtual is robustness.
The original designer of a class has a good idea for what the class is about, and the implementation is designed (either explicitly through the tests written with TDD, or more implicitly through the mind of the designer) to support that idea (or contract, if you prefer).
In other words, the designer has an explicit idea about what extension points he wants to support in the class. If virtual is only on those points, then the designer has (or should have) done sufficient testing on those points, and it's pretty likely that users who extend through those points get the behavior they want. And since only a few methods are virtual, the fact that a specific method is virtual is an important clue to the user that that method *is* an expected extension point.
If virtual is on every method, the user can extend in a lot of ways, but it's very unlikely that the designer thought of all those ways - or all combinations of those ways - which means that such a user extension is going into uncharted territory. Maybe it doesn't work when you try it. Maybe it works now, but breaks with the first update when the object behavior changes.
I prefer the "you can only do these 2 things, but I promise that they work" classes to the "you can do any of these 12 things, but they may or may not work" classes.
Comments
Anonymous
July 18, 2006
PingBack from http://microsoft.wagalulu.com/2006/07/18/seven-deadly-sins-of-programming-3/Anonymous
July 18, 2006
> original designer of a class is a poor judge of how the class might ultimately be used.
Ouch, that sounds like a problem waiting to happen. I suppose they would also advocate "never use sealed".Anonymous
July 18, 2006
>> original designer of a class is a poor judge of how the class might ultimately be used.
>Ouch, that sounds like a problem waiting to happen. I suppose they would also advocate "never use sealed".
Do you have any stories of a bad situation caused by a missing sealed or a virtual too many?Anonymous
July 18, 2006
Anders Hejlsberg had quite a bit to say about virtual @http://www.artima.com/intv/nonvirtual.html
This is said in the context of C#/.Net vs. Java, but most of the remarks are interesting anyways.
And I'd agree with you Eric, the "2 safe operations" approach is better than the "12 unsafe operations".Anonymous
July 19, 2006
I agree in principle, Eric. But using the Microsoft Enterprise "Library" as an example, I think one has to recognize that overzealous use of private and sealed limits the usefulness of a library. I'd lean more toward allowing extension with its inherent risks rather than limiting it.Anonymous
July 24, 2006
PingBack from http://technote.thedeveloperside.com/?p=56Anonymous
July 24, 2006
The comment has been removedAnonymous
August 04, 2006
So, the time has come for the worst sin.
Just to recap - and so there is one post that lists them all...Anonymous
August 05, 2006
PingBack from http://www.magerquark.de/blog/archive/374Anonymous
August 07, 2006
I'm going to stick with eric on this... if you're object is designed and TESTED for extension of 2 methods... then those are the only two methods that should be extended...
You aren't going to put trailer hitches on miatas. Just because you can hook up a trailer doesn't mean it will haul it.Anonymous
August 07, 2006
PingBack from http://pyre.third-bit.com/blog/archives/598.htmlAnonymous
August 09, 2006
So, the time has come for the worst sin.
Just to recap - and so there is one post that lists them all...Anonymous
August 13, 2006
The comment has been removedAnonymous
October 07, 2006
PingBack from http://www.philwallach.com/?p=65Anonymous
May 22, 2008
PingBack from http://www.gmbsg.com/stories/?p=66Anonymous
July 07, 2008
I was responding in comments, but it doesn't allow me to use links, so here's the long version: Judah,