« The Flawed Theory Behind Unit Testing | Main | Beyond Technical Debt »

June 27, 2008

Comments

Rafael de F. Ferreira

[I think TypePad swallowed my comment, so here it goes again:]

In Java, nested classes seem a little out-of-place, but in some languages they are a fundamental organizing principle. Beta pioneered the technique, Scala uses it to a great deal -- check out the cake pattern (http://is.gd/HEU) -- and Gilad Bracha's Newspeak appears to be adept as well.

Jeremy D. Miller

I use inner classes quite a bit for Fluent Interface development. It's awfully handy in that regard, but if the inner classes reach any type of size they get pulled out into their own class.

Ferruccio

I think the same can be said of nested and lambda functions. When they're small they can greatly improve the readability of the code by increasing locality of reference. But they can quickly grow to a size that makes it hard to distinguish them from their encapsulating function

Ron Jeffries

Seems to me that "X Considered Harmful" ought to be considered more like "Considering the harmful effects of X" rather than "X sucks in every way."

But I suppose it isn't considered that way.

Bugs me sometimes that people choose to take affront at a phrase rather than read and /consider/ the material as a whole.

Ricky Clarkson

I recognised my own shunning of nested classes as an arbitrary limit, like when I used to use an 80 char maximum width. So I thought for a moment about why I find nested classes unreadable, and dropped my indentation from being 8 columns wide to being 1 space.

Now I can happily place multiple classes inside an outer class, pretty much pretending that the outer class is a package with its members being top-level.

James Carr

I find nested classes okay if they are very small (It's not uncommon to find one line implementations of Closure or Predicate inside my classes). Unexpected outer scope creep, like you said, can be an issue though.

The usual approach I take is to put the class in a separate package and have the classes that would have been nested marked as default rather than public so that they aren't visible to the outside world.

Sachman Bhatti

Nested classes are useful in situations such as:
- it's a private class that is used only by the parent class
- it's a nested static class used to tell you more about the methods (as opposed to more overloads or methods on the parent class).

An example of the 2nd type, I recently wrote some code for a PermissionsEngine. I could have put more overloads/methods that say ForCurrentUser() but instead you can do:
PermissionsEngine.AssertPermissions(...,params string roleIds);
or...
PermissionsEngine.CurrentUser.AssertPermissions(...)

This makes the library easier to understand, and code easier to read when the engine is used.

George Harrison

If classes can't nest how will they have children? I remember one time when I was in Prague reading an article on nesting classes, I found it quite stimulating. Let me pass on a few interesting points:
- Classes often nest in the same place every year.
- You can often substitute a class that is the child of another in recipes, this is called "tasty"
- Classes sometimes share the same mate for years (see bullet 1, they also will use the same nest)

But that said, let me address your points individually:
1. No.
2. Maybe, but have you tried looking in trees?
3. Isn't this the same as 1?

So in conclusion, please let classes nest, or else our children's children may well never see classes.

Michael Feathers

George: Thank you for your thoughtful response, however I notice that classes don't really need nests to reproduce. When the male gamete is not involved, they reproduce by burgeoning, or budding.

Peter Ritchie

I think in the general case, you are correct--nested classes are rarely appropriate. Nested classes are like friend classes.

There are times when nested classes make sense. When private state information needs to be given out as opaque data only to be given back to the same class, nested classes to abstract that information make sense. Not implementing something like that as nested classes means implementation details are leaking out of your class. While I don't consider "polluting" a viable reason for nested classes; if those implementation details are made public through a autonomous class, you have to accept that you no longer have the same freedom to change those implementation details.

George Dinwiddie

One place where I've found using nested classes useful (though hardly essential) is for factory classes. When the factory class is nested inside the class it instantiates, it can have access to methods that are not intended for other clients. Also, it makes sense (sometimes) to keep these together when the two classes are tightly coupled, anyway.

That said, it's been several years since I've used this technique.

David Nelson

Partial classes in .NET make 1 irrelevant. 3 is true of any member, not just nested classes. 2 is legitimate, but it is the ability to access private scope that make nested classes uniquely suited to solve certain classes of problems (like iterators). They certainly can be abused, like every other programming feature ever invented, but I have never felt a need to avoid them.

alex

Right now dealing with another often seen side effect of nested classes: duplication hidden underneath an extra layer of (un)visibility.

The comments to this entry are closed.