NOV
21
2008

Use of casts in the Plasma code

Recently I've often been amazed by the ingenuity and the lengths that some people seem to want to go to, in order to be rude about KDE4. One example was a guy on Aaron's blog about the new system tray who claimed that Plasma had 'too many casts' especially dynamic_casts. 'Hey what? Huh?' I thought, as it was a bit off the wall.

However, we have all got the Plasma sources right there in the kdelibs trunk module, and so this morning I thought I'd do a quick survey to see if there was any truth in Ramsees' claims.

Firstly, how many dynamic_casts are there in the Plasma libs code?

mardigras rdale 57% cd kdelibs/plasma
mardigras rdale 58% find . -name "*.cpp" -exec grep dynamic_cast {} \; | wc -l
21

Which sources?

mardigras rdale 59% find . -name "*.cpp" -exec grep -l dynamic_cast {} \;
./applet.cpp
./popupapplet.cpp
./extenderitem.cpp
./extender.cpp
./corona.cpp
./tooltipmanager.cpp
./containment.cpp
./private/toolbox.cpp
./animator.cpp

So we have 21 casts in 9 different C++ source files. Aaron's blog currently has 111 comments, and so there are five times as many comments as there are casts. In fact, we probably have almost as many comments about casts as there were casts.

I counted many lines are in the Plasma .cpp and .h files including white space and comments. As there are about 40000, it means that we have one dynamic_cast for every 2000 lines of code. If you are familiar with the QGraphicsView api that Plasma uses as a canvas to draw on, you will know that it has quite a complicated class structure with multiple inheritance, and quite complicated interactions with the standard QObject/QWidget classes. I would say that it is pretty much impossible to use it without doing any casts and that we can say with quite a lot of certainty that 21 casts in Plasma is a very reasonable figure.

I counted the static_casts and found 101, which again is pretty much what you would expect given a C++ library of this size and complexity.

OK, there you have it then - I am happy to confirm that the Plasma code is well written and well designed from the point of view of cast usage.

Comments

> I counted the static_casts and found 101, which again is pretty much what you would expect
> given a C++ library of this size and complexity.

Especially that they could be false positives like:

int someint = static_cast(somedouble * 2.2);


By Pino Toscano at Fri, 11/21/2008 - 11:19

What about the usage of qobject_cast? :)


By Dominik at Fri, 11/21/2008 - 11:43

We have 25 qobject_casts in 8 source files. The guy posting on Aaron's blog didn't say whether using qobject_cast excessively is supposed to be bad design or not. But 25 of them isn't very many in the large Plasma code base.

mardigras rdale 74% find . -name "*.cpp" -exec grep qobject_cast {} \; | wc -l
25
mardigras rdale 75% find . -name "*.cpp" -exec grep -l qobject_cast {} \;
./applet.cpp
./popupapplet.cpp
./extenderitem.cpp
./extender.cpp
./view.cpp
./dialog.cpp
./private/applethandle.cpp
./private/nativetabbar.cpp

By Richard Dale at Fri, 11/21/2008 - 12:32

While casting is not particularly nice because you have uncertainty attached to it (but somebody's got to do it), it is the height of naivety to think that a project like Plasma can be undertaken without some data conversion through casting. It's a mistake that programmers stuck in an idealistic academic environment often make. You only have to look at the functionality Plasma is attempting to see that, but alas, most cannot see the wood for the trees when it comes to weighing functionality versus idealistic programming practices.


By segedunum at Fri, 11/21/2008 - 23:35