The Artima Developer Community
Sponsored Link

Designing Contracts and Interfaces
A Conversation with Scott Meyers, Part II
by Bill Venners
December 23, 2002

<<  Page 4 of 4

Advertisement

The Minimal Complete Sweet Spot

Bill Venners: These two things, minimal and complete, seem to be in tension. In your Effective C++ justification of minimal interfaces, you illustrate that multiple methods that do the same exact thing, such as think, ponder, and ruminate, are confusing. But striving for a minimal interface also seems to discourage the adding of gratuitous bells and whistles.

Scott Meyers: Yes.

Bill Venners: I guess I'm going back to the question, how do I know if the client really needs a particular method? How do you find that sweet spot that is both minimal and complete?

Scott Meyers: Finding the sweet spot is harder. If you follow my design guideline, most classes will probably be harder to use than you would like. Many convenience functions that people want won�t be there. If you want to have a nicely rounded class interface that does hit the sweet spot, you will almost certainly have some ancillary member functions. The ancillary functions will let people do things they could have done by making a bunch of calls through the public interface. Instead, they just call a single function that wraps the bunch of calls into a single function name.

Bill Venners: But that's not the minimal and complete sweet spot. That's the almost minimal, a little more than complete, but user- friendly spot.

Scott Meyers: Minimal and complete applies to the member functions. In that particular item, I'm talking about what you make member functions. I advocate that you determine what should be a member function by shooting for the minimal complete set.

However, you will probably want to offer clients a greater degree of functionality. I will call this greater degree of functionality, which includes all the member functions plus whatever non-member functions you provide for convenience, the interface to the class, because that is the set of functionality you are offering them. When you're shooting for the sweet spot, you really want a class that has a convenient interface.

Bill Venners: I see. Where are those non-member functions? Are they global functions that take this type of object as parameters?

Scott Meyers: Exactly. They are non-member functions. I actually wrote an article for CUJ (C/C++ Users Journal) a couple years ago. I suggest putting the non-member functions in the same namespace as the class, so they are also not global. I suggest using the namespace to package the non-member functions with the classes to which they are associated.

Talk Back!

Have an opinion about contracts, private data, or minimal and complete interfaces? Discuss this article in the News & Ideas Forum topic, Designing Contracts and Interfaces

Resources

Effective C++, Second Edition, by Scott Meyers is at Amazon.com at:
http://www.amazon.com/exec/obidos/ASIN/0201924889/

More Effective C++, by Scott Meyers is at Amazon.com at:
http://www.amazon.com/exec/obidos/ASIN/020163371X/

Effective STL, by Scott Meyers is at Amazon.com at:
http://www.amazon.com/exec/obidos/ASIN/0201749629/

Effective C++ CD, by Scott Meyers is at Amazon.com at:
http://www.amazon.com/exec/obidos/ASIN/0201710919/

Scott Meyer's website contains links to many publications, presentations, books, and other information from Scott:
http://www.aristeia.com/

Images of Persephone, the best dog in the world:
http://www.aristeia.com/Persephone/index_frames.html

<<  Page 4 of 4


Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us