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?
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 🙂