No private methods
I’m heading off into the deep end here. If you want to come with me, make sure you’re wearing your SCUBA gear.
One of the first questions that come up in TDD with NUnit is “How do I test private methods?” Usually the answer looks like “Don’t do that, because XXX”, like:
· If your class has interesting private functionality that you’re inclined to test, that functionality might be interesting to a consumer of your class. Expose it. In this way, TDD is an exercise in Domain-Driven Design, where the domain is software writing, and the user is you in a few minutes.
· It’s a code smell. If your class is so complex that you need to test private methods, you should refactor to pull some of that complexity into its own class.
There are other reasons, I’m sure. If you have one, comment on it…
So I had this crazy idea. What happens if you take the second bullet to the logical extreme? What if you continue performing Extract Class until there are no more private methods? Private fields are fine, but only public methods.
It’s an idea. I don’t know if it’s a good idea or a bad idea. Even if it’s a good idea, I don’t know if I can do it well. So if I try it and the result sucks, I don’t know if it’s because the idea is bad or just because I can’t do it right.
I can try to imagine what might happen if you try this.
Obviously, you’d have a lot of small classes. You’d need to be careful to ensure that they made sense.
I think you would end up with deep nesting of classes, with built-in types at the very bottom, like:
class C
{
int i;
public void F() { }
}
class D
{
C c;
public void G() { }
}
class E
{
D d;
public void H() { }
}
How does this jive with Object Thinking’s suggestion that you shouldn’t end up with a large number of classes? (I think it does jive, but I need to think about it more.)
Is the result really easy to unit test? I hope so, but is it?
Is the code ultra-readable or a total mess?
Is refactoring this code impossible or trivial?
If you get good at this, does it change your thinking about the ideal size of a class? (The balance between many simple classes and few internally complex ones?)
I need to find a good programming problem & try this out.