Skip to content

Good design ghosts

April 24, 2006 by Artem

I can recall several cases, when a seemingly clean'n'simple class appeared to be a huge and difficult to debug entity with loads of underlying asynchronous logic. The simple and small public interface hides the implementation details and according to the good Object-Oriented style all the underlying details are private. Or is it a good style to hide the implementation details into many private methods?

As it often happens, the problem comes from a good idea overused to the extent, where it inhibits another good idea. The encapsulation makes it easy to use your classes or systems externally, but it should not make you forget the internal structure of the class. As Michael Feathers tells

It often means that there's some other abstraction hidden away inside the class that might be useful to pull out.

The class with a loads of private methods looks like a ghost of a good design over an old dirty dangerous grave. It is good to hide the internals of your system from the external clients, but don't forget about the next level and keep the sublevels clean as well.

Comments

There's private and then there's private

April 25, 2006 by Anonymous, 5 years 41 weeks ago
Comment id: 4

I've seen way too much use of "private" and "final" with the purpose of enforcing developers to follow "the way", effectively protecting the codebase from the stupid developers. In many cases, moving those private methods into being public methods on a separate class is a huge improvement in terms of testability and code clarity. And, believe it or not, the stupid developers are not going to start using a method in malicious ways just because it's now public in its new home.

Refactoring might be the key

April 26, 2006 by Artem, 5 years 41 weeks ago
Comment id: 5

I have a good example right now. I am building the application over the API with a lot of small interfaces. The bridge to this API serves a small and concrete purpose, but nevertheless has to [privately] implement a lot of these small interfaces. Clear and simple class purpose brought small amount of nice public methods and a reasonable of private implementation complexity.

What I am going to do is whenever the basic and totally unoptimized implementation is ready, I'll start refactoring this big class into several smaller chunks, each to have mostly publicly inherited interfaces. The key is not to be afraid of refactoring and to be prepared to it (have unit/system tests in place and delay the optimization not to harm the code clarity).

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <b> <i> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <br> <blockquote>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.

Best of AgileSoftwareDevelopment.com