6

I'm find I have several places that having public static inner classes designed that extend "helper" classes makes my code a lot more type safe and, in my opinion, readable. For example, imagine I have a "SearchCriteria" class. There are a lot of commonalities for the different things I search for (a search term and then a group of search term types, a date range, etc.) By extending it in a static inner class, I tightly couple the extension and the searchable class with the specific differences. This seems like a bad idea in theory (Tight Coupling Bad!) but the extension is specific to this searchable class (One Class, One Purpose).

My question is, in your experience, has the use of static inner classes (or whatever your language equivelent is) made your code more readable/maintainable or has this ended up biting you in the EOF?

Also, I'm not sure if this is community wiki material or not.

amaidment
  • 6,942
  • 5
  • 52
  • 88
Drew
  • 15,158
  • 17
  • 68
  • 77
  • Is it a factual question? Can there be a right answer? If not, it's a discussion topic, better done as a community wiki. – S.Lott Mar 23 '09 at 19:11
  • Which language(s) are you talking about? MostOO languages have no such concept as static inner class. –  Mar 23 '09 at 19:20
  • S.Lott: Fair enough. Community Wiki-ized then. Neil Butterworth: I guess the better choice of wording would be nested classes as opposed to static inner? And This is originally Java. – Drew Mar 23 '09 at 19:25
  • Well, not a few don't have nested classes either - Smalltalk for one –  Mar 23 '09 at 19:45
  • Neil: Fair enough. However, the two OO languages I've used most extensively, Java and C++, both had nested classes of some variety. So I figured this was a more general question than just Java. How should I have better tagged/worded this question? – Drew Mar 23 '09 at 19:56
  • Although this question is not limited to Java, it may be helpful to reference the [Java tutorial](http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html) on this. Also, in Java, what is described here is a 'static nested class', whereas 'inner class' refers to a non-static nested class. This [SO question](http://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class) explains the difference. – amaidment Aug 30 '13 at 08:35

4 Answers4

3

Sounds perfectly reasonable to me. By making it an inner class, you're making it easy to find and an obvious candidate for review when the searchable class changes.

Tight coupling is only bad when you couple things that don't really belong together just because one of them happens to call the other one. For classes that collaborate closely, e.g. when, as in your case, one of them exists to support the other, then it's called "cohesion", and it's a good thing.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
1

The only caveat with using inner classes is making sure you're not repeating yourself all over the place - as in - make sure, when you define an inner class, you're not going to need to use that functionality anywhere else, and, that that functionality is necessarily coupled with the outer class. You don't want to end up with a whole bunch of inner classes that all implement the exact same setOrderyByNameDesc() method.

rojoca
  • 11,040
  • 4
  • 45
  • 46
1

Note that the class is not the unit of reuse. Therefore, some coupling between classes is normal and expected. The unit of reuse is usually a collection of related classes.

In Python, we have a variety of structures.

  1. Packages. They contain modules. These are essentially directories with a little bit of Python machinery thrown in.

  2. Modules. They contain classes (and functions). These are files; and can contain any number of closely-related classes. Often, the "inner class" business is handled at this level.

  3. Classes. These can contain inner class definitions as well as method functions. Sometimes (not very often) inner classes may actually be used. This is rare, since the module-level coupling among classes is usually perfectly clear.

S.Lott
  • 384,516
  • 81
  • 508
  • 779
-2

The point in "loose coupling" is to keep the two classes separate so that if there are code changes in your "SearchCriteria" class nothing would have to be change in the other classes. I think that the static inner classes you are talking about could potentially make maintaining code a nightmare. One change in SearchCriteria could send you searching through all of the static classes to figure out which ones are now broken because of the update. Personally, I would stay away from any such inner classes unless it is really needed for some reason.

beyerss
  • 1,302
  • 3
  • 21
  • 38