0

As I start to code more and more, for example in C++, I tend to also want to learn more about the accepted standards for code.

I'm trying to code a chess engine right now, but I'm unsure how to treat each 'module' of the program (which is separated into different files).

For example, it makes sense to make a class for bitboards as bitboard.h and bitboard.cpp, as this might be required later on. But, it doesn't make sense to have a class for the move generator, because I won't ever need more than one move generator.

But I still want to abide by the principle of least privilege, and not give users access to more functions than necessary. But, I'm not sure if you can do this without a class? I've looked at namespace but I'm not sure how it'd apply in this case.

How do professional programmers handle this sort of thing? I've looked at the source for the Stockfish open source chess engine, and it has both header files and cpp files, but the header files simply seem to have a whole bunch of variables.

  • 1
    I just looked at 3 header files randomly and they contained classes, enums, templates, namespaces... Also only needing one generator doesn't argue against (or for) making a class for it. – Mat Jul 12 '15 at 13:04
  • @Mat I did make a class for it. But looking at other engines (like Stockfish), they aren't implemented as classes. For example, movegen.h in Stockfish only contains a class for position, not for movegen itself. – Shreyas Jul 12 '15 at 13:09
  • Is this some sort of networked, multiuser application where you have to worry about cheating? When you say you want to abide by the principle of least privilege, are you talking about the actual objects themselves? – Steven Burnap Jul 12 '15 at 15:19
  • @StevenBurnap No, it's not something I have to worry about users cheating at. For example, somebody else may in the future use the movegen in their own chess engine, but there are only a few functions that actually need to be public (the remaining being internally used). – Shreyas Jul 12 '15 at 15:51
2

The Principle of Least Privilege is about security, not software engineering and should not be confused with Encapsulation. They are two very different things.

The point of the Principle of Least Privilege is to give a user (or application) the bare minimum of permissions it needs. So user A may get a different set of permissions than user B. This is to make it hard for potentially malicious users to gain access to potentially dangerous features as it requires that they have explicit permission.

Encapsulation is about making it clear about what parts of your class are meant for external user and what parts are meant to be ignored by the caller. The purpose here is not about malicious users, but rather, about making sure users don't accidentally write code that depends on things that may change in the future. It also pushes class authors to create clear interfaces.

Inherent in the Principle of Least Privilege is that different users have different permissions. There is no notion of that in all in Object Orient software. All users of your classes have the same access. Also implicit in the Principle of Least Privilege is that you are forcing users into a restricted subset. By contrast, Encapsulation can be easily broken in many Object Oriented languages. (In some, like Python, encapsulation is entirely handled with coding standards.)

If you try to design classes with an eye to restricting users to a minimum (as with the Principle of Least Privilege) you are likely going to end up with classes that are limited and hard to use. Instead, you should look at it from the perspective of "what are the users going to want to do with this class". The public interface is everything you can think of that users will want to do. Then make everything else private. That's Encapsulation.

In essence, when designing classes, you want to operate with an eye to giving users the most privilege you can safely give them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.