should I take out high level methods from a class?

Hi

I have a class like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
  class Something{

  private:
  
  // data structures and internal methods

  public:
  
  // low level methods for accessing and modifying data structures

  // high level analysis methods , some of which use internal methods
  };


I was wondering whether it would be better to separate out the high level methods from the class? If so, would it be best to do this as:

1. A separate class containing these high level functions as methods?

2. Just have the high level functions by themselves in a header and implementation file? Some of the high level functions rely on the internal methods so I'm not sure where these would go.

Thanks
Last edited on
not much to go on here...
but a red flag jumps out immediately: a data structure should BE a class and a class would only use such as a type most of the time. A class would not implement one alongside its true purpose.

seems like for the other question, that a class to do analysis would use inheritance so it could access the private internal methods without having to do anything odd.
When you design a class you're designing an appliance, like a microwave, and the class' public members are like a microwave's front panel. You should put there things that someone wanting to heat food might need. If one of the buttons needs access to be microwave's internals, then it should be integrated into the microwave. It should not be a plug-in. You don't want a port on the side of the microwave with wires sticking out, just so you can connect a button that really should have been on the front panel to begin with.

To give more specific advice I would need to take a look at your class and the functions you're talking about.
Last edited on
// high level analysis methods , some of which use internal [private] methods
If the analysis methods use private data/methods then they have to be part of the class.

If you make some of them public then you should ask yourself some questions like:
- Is the analysis conceptually part of the class, or does the analysis use the class?
- Would it be a big burden to do the analysis outside the class? If the analysis is all up in class Something's business then it would be better as a method:
1
2
3
4
5
6
void analyze(Something &s)
{
    s.something();
    int i = s.this() + s.that();
    if (s.cond()) s.stuff();
} // yuck 


If a new programmer wanted to find the analysis code, where would they look first?

If the analysis is a ton of code that most users of class Something won't need, then put it in a separate compilation unit. Add a comment to something.cpp saying that the analyze method is located infile xyz.cpp.

Topic archived. No new replies allowed.