Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should virtual functions not be used excessively?

I just read that we should not use virtual function excessively. People felt that less virtual functions tends to have fewer bugs and reduces maintenance.

What kind of bugs and disadvantages can appear due to virtual functions?

I'm interested in context of C++ or Java.


One reason I can think of is virtual function may be slower than normal functions due to v-table lookup.

like image 596
GG. Avatar asked Jun 16 '10 04:06

GG.


People also ask

Can virtual functions be overloaded?

It is not possible for these functions to get overloaded.

When should you use virtual functions?

You use virtual functions when you want to override a certain behavior (read method) for your derived class rather than the one implemented for the base class and you want to do so at run-time through a pointer to the base class.

Why are virtual functions slower?

Virtual functions are slow when you have a cache miss looking them up. As we'll see through benchmarks, they can be very slow. They can also be very fast when used carefully — to the point where it's impossible to measure the overhead.

What happens if we don't use virtual function in inheritance?

If you don't use virtual functions, you don't understand OOP yet. Because the virtual function is intimately bound with the concept of type, and type is at the core of object-oriented programming, there is no analog to the virtual function in a traditional procedural language.


2 Answers

I dont know where you read that, but imho this is not about performance at all.

Maybe its more about "prefer composition about inheritance" and problems which can occur if your classes/methods are not final (im talking mostly java here) but not really designed for reuse. There are many things which can go really wrong:

  • Maybe you use virtual methods in your constructor - once theyre overridden, your base class calls the overridden method, which may use ressources initialized in the subclass constructor - which runs later (NPE rises).

  • Imagine an add and an addAll method in a list class. addAll calls add many times and both are virtual. Somebody may override them to count how many items have been added at all. If you dont document that addAll calls add, the developer may (and will) override both add and addAll (and add some counter++ stuff to them). But now, if you youse addAll, each item is count twice (add and addAll) which leads to incorrect results and hard to find bugs.

To sum this up, if you dont design your class for being extended (provide hooks, document some of the important implementation things), you shouldnt allow inheritance at all because this can lead to mean bugs. Also its easy to remove a final modifier (and maybe redesign it for reuseability) from one of your classes if needed, but its impossible to make a non-final class (where subclassing lead to errors) final because others may have subclassed it already.

Maybe it was really about performance, then im at least off topic. But if it wasnt, there you have some good reasons not to make your classes extendable if you dont really need it.

More information about stuff like that in Blochs Effective Java (this particular post was written a few days after I read item 16 ("prefer composition over inheritance") and 17 ("design and document for inheritance or else prohibit it") - amazing book.

like image 162
atamanroman Avatar answered Sep 21 '22 05:09

atamanroman


You've posted some blanket statements that I would think most pragmatic programmers would shrug off as being misinformed or misinterpreted. But, there do exist anti-virtual zealots, and their code can be just as bad for performance and maintenance.

In Java, everything is virtual by default. Saying you shouldn't use virtual functions excessively is pretty strong.

In C++, you must declare a function virtual, but it's perfectly acceptable to use them when appropriate.

I just read that we should not use virtual function excessively.

It's hard to define "excessively"... certainly "use virtual functions when appropriate" is good advice.

People felt that less virtual functions tends to have fewer bugs and reduces maintenance. I'm not able to get what kind of bugs and disadvantages can appear due to virtual functions.

Poorly designed code is hard to maintain. Period.

If you're a library maintainer, debugging code buried in a tall class hierarchy, it can be difficult to trace where code is actually being executed, without the benefit of a powerful IDE, it's often hard to tell just which class overrides the behavior. It can lead to a lot of jumping around between files tracing inheritance trees.

So, there are some rules of thumb, all with exceptions:

  • Keep your hierarchies shallow. Tall trees make for confusing classes.
  • In c++, if your class has virtual functions, use a virtual destructor (if not, it's probably a bug)
  • As with any hierarchy, keep to a 'is-a' relationship between derived and base classes.
  • You have to be aware, that a virtual function may not be called at all... so don't add implicit expectations.
  • There's a hard-to-argue case to be made that virtual functions are slower. It's dynamically bound, so it's often the case. Whether it matters in most of the cases that its cited is certainly debatable. Profile and optimize instead :)
  • In C++, don't use virtual when it's not needed. There's semantic meaning involved in marking a function virtual - don't abuse it. Let the reader know that "yes, this may be overridden!".
  • Prefer pure virtual interfaces to a hierarchy that mixes implementation. It's cleaner and much easier to understand.

The reality of the situation is that virtual functions are incredibly useful, and these shades of doubt are unlikely coming from balanced sources - virtual functions have been widely used for a very long time. More newer languages are adopting them as the default than otherwise.

like image 42
Stephen Avatar answered Sep 21 '22 05:09

Stephen