Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refactoring a data class in C++

So I have the following class which I find silly and would like to refactor.

class Data
{
  public:
    bool getVariableA();
    bool getVariableB();
    bool getVariableC();

    void setVariableA();
    void setVariableB();
    void setVariableC();

  private:
    bool A;
    bool B;
    bool C;
}

This goes on for like 100 variables and growing, most of which are boolean. If you were to refactor this class (while keeping all the data in it instead of spreading out), how would you go about it?

Issues with the current structure are at least that it's a) too much code b) pain to add stuff c) unit testing coverage always needs to be added manually when growing the class.

like image 415
OwlOCR Avatar asked Oct 18 '25 23:10

OwlOCR


1 Answers

how would you go about it?

The solution proposed by @JohnHenly is spot-on:

I would use this code:

std::vector<bool> data;
enum index { var_a, var_b, var_c, ... };

data[var_a] = true;

If possible though, consider also splitting the data by one of these criteria:

  • logical group
  • location where they are used
  • purpose
like image 175
utnapistim Avatar answered Oct 20 '25 13:10

utnapistim