Extreme Advice 1: Make all Methods virtual!

“Make all Methods virtual!” This was actually a sign on the door of the Head of Engineering in a company i used to work. I thought at the time this was a pretty extreme advice. It was actually targeted at the C++ programmers in our group but can be applied to C# as well. The background was that this guy used to be a Java programmer. In Java you have the opposite situation: Every method is virtual by default! You have to say explicitly that a method shall be final (non-virtual).

Now I came to the conclusion that this is a very good advice, especially if you are working on a framework. I had recently various situations where we had to change our framework simply because we needed to override a method. This is a big thing because you have to redistribute the framework, do tests etc.

The only downside of making everything virtual is a slight diminished performance (as the compiler needs to put hooks into your code to provide a possibility to override things). There is an excellent discussion about this on artima (http://www.artima.com/intv/nonvirtual.html) with Anders Hejlsberg. I definitely would argue, that it is worth following this “extreme advice”.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>