views:

106

answers:

5

Assume I have a class a:

class a
{
public:
  void load_data( );
private:
  void check_data( );
  void work_data( );
  void analyze_data( );
}

Those functions all do something with the class or one of its members.

However this function:

bool validate_something( myType myData )
{
     if ( myData.blah > 0 && myData.blah < 100 )
     {
        return true;
     }
     return false;
}
  • Is related to the class and will only be called by it, so it won't be needed anywhere else

  • Doesn't do anything with the class or its members - just a small "utility" function

Where to put validate_something? Inside or outside the class?

A: 

If this method is a utility that's only being used by the class, I would make it a private method of the class. This way, it's encapsulated and not "polluting" your public API.

Reed Copsey
+3  A: 

Make it a private static class member. I used to tend to make these non-class members and put them in an nameless namespace in the implementation file, but it seems that they almost always do end up needing to be class members (or need to be moved elsewhere - perhaps to a validation library, in your example), as the code changes, so now I almost always make them static members.

Please notice the use of "almost" as a qualifier throughout this answer :-)

anon
Why would a function *need* to be a static member as opposed to being in a nameless namespace in the impl file? Or do you just do this to save work when it does need to get state data?
John Dibling
And why do you make them `static` members rather than simply member functions?
John Dibling
@John It's my experience that helper functions in the implementation file almost always end up needing to be migrated to the class. This is pure expediency.
anon
@Neil: That's what I figured. Just making sure you didn't know something I didn't :)
John Dibling
@Neil: this seems very "oracly" to me. I would say it's unnecessary for the method to have access to the class private parts and it's thus better than it does not.
Matthieu M.
@Matthiue I'm offering an heuristic, not a treatise on OO programming. And what is "oracly"?
anon
A: 

Assuming that myType is also a struct/class, why don't you make validate_something a method of myType?

Patrick
A: 

As I understand, it's not a method. Just put it's definition in the cpp file, above the definitions of the methods that use it. If You have all your method definitions inline in Your class You have to put the prototype of the function in the header above the class definition, but the definition can still be in a cpp file.

Maciej Hehl
+6  A: 

If a function is

  • not required outside of the class, and,
  • doesn't need to access class members (or perhaps makes sense as a free function with members as parameters)

then I tend to make it a free function inside a private namespace in the implementation file. I prefer not to use private functions because they expose implementation detail in the header file (and haven't yet encountered Neil's problems) and I prefer to avoid recompiling for implementation changes.

Adam Bowen