In my experience object-oriented programming concept constructor-destructor rule combined with the idea of ownership has reduced typical memory corruption bugs of C to a point where it has become almost non-existent problem, yet very common source of bugs to C programmers. C++ has also other useful concepts which are extremely simple such as the scope of a class.
In C++ and other object-oriented languages the concept of inheritance is an essential feature, but it's proven to be difficult to use properly. Rather than using inheritance as it was designed I've been using it sparingly and probably also wrong way in the first place. For a long time I was wondering what is wrong with inheritance and I think I've found a good explanation.
The main problem with inheritance is something that many roguelike programmers find in what we call a game engine. It's an idea where data is handled by a program core called engine and the content of game is in the data entirely or as much as possible. We have years of knowledge about engine programming which in mathematical term tends towards infinity. This means you spend ridiculous amount of time trying to write a roguelike engine which turns out to be extremely difficult.
The engine vs. direct approach to game programming is analogous to inheritance and class hierarchy, because both are examples of generic vs. specific case. Even if you write a generic engine you still need to solve the other problem, because each game is specific. I believe this is the root of the problem. You have an idea to write generic class library and/or program using lot of inheritance to make sure no similar code is duplicated, but the result is more or less generic. You still have to create the actual game which often include extremely specific content.
This is the reason why it's easier to create specific classes to solve some problem, because it's more direct style even from the outside it appears to be OOP with classes and all that. The generic solution is as difficult to program as is a game engine that can handle all specific cases of a game. It can be possible, but I think it requires lot of experience and planning, unless you have a talent for using inheritance which I guess few people have, according to my internet research on comp.lang.c++ and other forums.
When you look at the big picture I think object-oriented programming is still difficult to handle after 50+ years of its invention and that's why some people don't like it or they write inefficient code which sometimes can reach strange meta-levels not really required to produce computer programs. OOP can be a trap for people who think they are clever and who refuse to understand the concepts and admit that there is possibly something from which they have only limited knowledge.
No comments:
Post a Comment