Sponsored Link •
Bill Venners: One guideline in Effective C++ is, "Strive for interfaces that are minimal and complete." How do you know when an interface is complete? How do you determine what to leave in and what to leave out?
Scott Meyers: A class is a manifestation of some concept. You have something in mind. You want to let people talk about that something. You have some idea of the operations that make sense. You have some idea of what people might reasonably want to do.
The class is complete when people can do everything that you as a designer can envision they might reasonably want to do, though they don't necessarily have to be able to do it in a convenient fashion. If you want to make things convenient, add some non-member functions that wrap a bunch of calls to member functions. The class is complete, however, when clients can do everything you envision people might reasonably want to do.
Bill Venners: But what if I can envision 1000 things?
Scott Meyers: And they're all reasonable?
Bill Venners: Yeah. Or, maybe they aren't but I think they are. How do I know if 500 of those things really aren't necessary? How do I decide what is necessary?
Scott Meyers: Necessary is easier than reasonable. A member function is unnecessary if it can be implemented in terms of the other member functions. You can provide unlimited functionality. I don't care about that. The question is whether or not it goes inside the class. If you have a member function that can be implemented in terms of other member functions, it is not necessary.
A minimal interface throws out all the unnecessary functions. The unnecessary functions might simply become non-member functions. In that case, clients can still use the functions, they just don't call them with member function syntax.
It's difficult for me to envision a class that has even 100 member functions,
none of which are redundant. For example, one of the more embarrassing
classes in the standard C++ library is the
String class. The
String class was designed by committee, and it shows.
String has about 113 member function names, including
overloading. I say member function names because many of them
are member function templates, which means the number of member
functions is literally unlimited. You could easily trim that down to 25 names,
move everything else outside the class, and not lose any functionality. That's
just a matter of a design that went completely crazy.
If you can come up with more than say 25 or 30 member functions, that strongly suggests you have probably merged more than one concept into a single class. You should probably think about splitting that class into pieces.