1

It's a newbie question. In my simple learning rock-scissors-paper game program I have single game instance class. Game instace class has several private members like total turns count, wins count, tie count etc. These class members are used only in public game instance's class method, which starting and processing a game. Should I create special private getters and setters methods for my class members following good code style? Does it better to work directly with class members in game processing method?

class GameInstance{
    unsigned int m_totalTurns;
    unsigned int m_wins;
    unsigned int m_loses;
    unsigned int m_ties;
    int getWins() const;               // is it a bad idea?  
    int getLoses() const;              // is it a bad idea?  
    int getTotalTurns() const;         // is it a bad idea?  
    int getTies() const;               // is it a bad idea?  

    // some setters-like incrementing  // is it a bad idea?
    // same private members functions  // is it a bad idea?    
    // ....                            // is it a bad idea?  

    void finishGame();

public:
    explicit GameInstance():m_totalTurns(0), m_wins(0), m_loses(0), m_ties(0){}; 
    void startGame();
};
vard
  • 2,142
  • 4
  • 30
  • 40

4 Answers4

4

You'd probably make the getters public or there is no point in having them.

With regards to any getters or setters, they are part of the interface of the class. If you don't want to externally allow getting these members directly or setting them, do not expose getters and setters.

With yours, getters might be an idea, setters looks it might be a bad idea, if you would rather have addResult() which specifies a win, tie or loss and adds one to the relevant stat.

As an alternative to individual getters you might have a "getGameStats()" which returns all 4 in one go.

If you are asking about getters rather than giving public access (rather than not giving access at all), for classes that have methods you should normally make all your data members private and use getters and if necessary, setters. It is also easier to debug code that is using them as you can insert breakpoints to the point the variables are being accessed.

In a "pure data" struct you can make members public. In fact in this case you might have a "GameStats" struct that you would return from a getGameStats() function. GameStats itself would just contain numbers and you could make these public. However the instance of GameStats within your class should be a private instance.

CashCow
  • 30,981
  • 5
  • 61
  • 92
  • Thanks, it's really helpful. And it is very nice idea about game statistics struct. – vard Nov 30 '12 at 09:56
1

I think it's a good approach. It hides implementation. It's simpler to edit. Say you have 100 places where you use getTotalTurns(). In one day you need to change logic of getter. And you will need to change one place instead of 100. Don't think about performance. Compiler will probably inline all those functions. As @CachCow said there are no real reasons to make them private(setters/getters). You can make them protected to grant access for derived classes. But most of time getters/setters are for public use.

Denis Ermolin
  • 5,530
  • 6
  • 27
  • 44
  • It doesn't hide implementation. You can see the private member. It does enable you to control access though, and can be more maintainable from the point of view of being able to put a breakpoint in the getter if something is going wrong, whilst if it's accessed direct you won't know where to put the breakpoint. – CashCow Nov 30 '12 at 09:48
  • I didn't see that getters/setters for private use. Then yes they dont really need for most variables. – Denis Ermolin Nov 30 '12 at 09:51
  • getters and setters can sometimes be protected to allow access to derived classes (whilst restricting it completely to externals). However I see no benefit from making them private. – CashCow Nov 30 '12 at 09:53
0

If you have single-thread project and main interface and interaction with user are all in GameInstance class, you have no need in accessing theese fields during game process. When game finishes you may get all off them through struct:

struct GameInfo
{
    unsigned int m_totalTurns;
    unsigned int m_wins;
    unsigned int m_loses;
    unsigned int m_ties;
};

class GameInstance
{
private:
    GameInfo gameInfo;
public:
    GameInfo GetInfo() const;
};

This approach is still safe but you don't have to write annoying number of getters.

Arsenii Fomin
  • 3,120
  • 3
  • 22
  • 42
0

There is a fairly good discussion on when / why you should use getters and setters here:
SO Getters and Setters Question

but its often worth using them even if its just for internal use, it ensures that when you access the values, you are doing it in a uniform way, making sure when you're editing them you're complying to your rules on how they should be modified.

basically it helps make your code cleaner in most situations.

Edit:
I Should point out, its more of a personal choice when it comes to using private getters and setters, so there's no real "wrong" answer, unless there's something i'm unaware of

Community
  • 1
  • 1
Daboyzuk
  • 449
  • 4
  • 14