Encapsulation, Black Box

Many classes nearly every class of VCMI have all methods and data members public. I think it would be much better to encapsulate things which let’s say the user have nothing to know about. Things get easier, bugs can be avoided and development gets faster. Unauthorized access to data members or setting false and inappropriate values to class variables can be forbidden. If you need nevertheless access to private members in rare cases then you can release that members to any class with the friend keyword.

I think it would be nice to introduce/integrate encapsulation incrementelly to VCMI sources. What’s your opinion about that?

PS: As a newbie VCMI developer should I program things “open” or let’s say how the normal way with encapsulation?

Encapsulation is a risky topic… I can’t remember even one bug caused by lack of encapsulation. But don’t get me wrong, I have nothing against encapsulation as long as it’s something more than changing direct access to fields to setters/getters that check nothing. Good encapsulation could be helpful for new developers, so if you see a field that shouldn’t be public, feel free to make it private.

You’re right encapsulation is an controversial issue where there are the ones who think that it’s the most useless programmer’s tool on earth and on the other side there are the ultimate encapsulation lover’s. I think if you use that programmer’s technique wisely then it’s a way to reduce complexity. Public methods should be written on top of the class declaration, so that’s it’s clear what functionality this class does share. As you said, good encapsulation helps new developers to understand better the source code and how(!) it should be used to avoid bugs.

If encapsulation gets to that point that it’s just used for getters and setters then it’s bad. A module or a class should reveal as little as possible about it’s inner working at all. Encapsulation can also help to get things more modularized. A good class should have few relations to other classes. Minimal connectedness minimizes work during integration, testing and maintenance.

I think less encapsulation is better than over-using it. A good class hierarchy and method design is far more important. I just wanted to know what’s the reason why mostly everything in VCMI is public. Now it’s clear.