P
Pete
Hi,
Amazing that noone has mentioned performance, especially since this is
almost certainly the key reason that methods are non-virtual by default!
Calling a virtual method is slower than calling a non-virtual method.
Objects with virtual methods also require more memory. Forcing programmers
to use slower-than-necessary methods (even when there's clearly no purpose
to it being virtual) is just poor design in a programming language.
Further discussion inline:
Not really, see below:
Surely making everything virtual is the opposite of flexibility? Flexibility
is *allowing* programmers to use non-virtual methods if they want. I can
easily get the Java approach by always using virtual or override, but I
don't have to.. *that's* flexible.
With the c# approach you have the best of both worlds -- extensibility if
you want it (and hence, flexibility).
Like you implied earlier in your post.. you should use virtual methods where
you intend people to override them. Making a method virtual is as good
as -telling- the user that they can (and should) override the method. When
all methods are virtual people have no way of knowing which methods are
intended to be overridden, which can cause unforeseen problems.
I guess you could argue that making everything virtual gives extensibility
even where it wasn't intended.. but this just seems like you're working
around poor design to me.
Pete
Amazing that noone has mentioned performance, especially since this is
almost certainly the key reason that methods are non-virtual by default!
Calling a virtual method is slower than calling a non-virtual method.
Objects with virtual methods also require more memory. Forcing programmers
to use slower-than-necessary methods (even when there's clearly no purpose
to it being virtual) is just poor design in a programming language.
Further discussion inline:
There are two different perspectives on library design, and which one
is better depends upon your perspective. It's a tradeoff between
flexibility and robustness.
Not really, see below:
If you value extensibility and flexibility, you will likely prefer
the Java approach, which says that everything can be extended unless
you explicitly say that it shouldn't be. If you value robustness
more, you will likely prefer the C# approach, which says that things
can be extended only if the class designer allowed it to be extended.
Surely making everything virtual is the opposite of flexibility? Flexibility
is *allowing* programmers to use non-virtual methods if they want. I can
easily get the Java approach by always using virtual or override, but I
don't have to.. *that's* flexible.
With the c# approach you have the best of both worlds -- extensibility if
you want it (and hence, flexibility).
In addition to the default behavior, there's also a philosophical
issue related to class design. Should you leave things virtual unless
you're sure there's a good reason for them not to be virtual, or
should you make them virtual only if you know of a good reason for
them to be virtual.
Like you implied earlier in your post.. you should use virtual methods where
you intend people to override them. Making a method virtual is as good
as -telling- the user that they can (and should) override the method. When
all methods are virtual people have no way of knowing which methods are
intended to be overridden, which can cause unforeseen problems.
I guess you could argue that making everything virtual gives extensibility
even where it wasn't intended.. but this just seems like you're working
around poor design to me.
Pete