Few Short Classes Per Method

And you thought that FewShortMethodsPerClass is the real deal ? No, let me tell you the real deal: FewShortClassesPerMethod.

The idea is that the code is then completely modular and perfectly encapsulated. Clients won't be able to see the classes of the objects they get, not even in the CodeBrowser?. They just see an interface that's implemented, well, nowhere !!! That was the whole point. Even if they have a very modern environment, smarter clients will be able to detect where the class is implemented, and even in debugger they will see something like MainClass$1InsideMethod$X and some such mumbo jumbo, they won't be able to grab a hold of them, they can't instantiate. Completely sealed !!!. Just like Wirth intended them to be when he asked us to separate interface from implementation.

So this keeps you clients honest, but keeps you also honest. You cannot inherit that damn class, you just leave it there (inheritance is for weak souls, going for it like flies to the jar with honey). you cannot instantiate it in another method. More importantly, it makes almost trivial for you to have your new objects referentially transparent. Your referenced variables will be on a stack (the locals in the method), and the stacks disappears, it's almost like LinearLogic. And you can really feel the outer class acts like a module in ML. The real objects, are sealed and closed inside the methods, as they should be.

Extra points for having static methods only. Then the classes inside the static methods do not share state through the enclosing instance.


A modest example: the final static method of some class (we don't even care, because that's the idea, constructs the union of two ordered sets. Nobody from outside has any hold on the implementation. Although there's some imperative state creeping through the backdoor, due to some of my bad programming habits.

 /**
  * represents the union of two ordered sets
  * @author ccozianu
  */
 public static OSet Union(final OSet s0, final OSet s1) { 
class _union extends OSet {
public CloneableEnum? getEnum() {
class UnionENUM implements CloneableEnum? { 
CloneableEnum? e0_=null, e1_=null; 
CloneableEnum? e0() { return e0_!=null? e0_ : (e0_=s0.getEnum());}
CloneableEnum? e1() { return e1_!=null? e1_ : (e1_=s1.getEnum());}

// buffer Comparable[] holder = { null, null };

public CloneableEnum? clone_() { UnionENUM result= new UnionENUM(); result.holder= (Comparable[]) holder.clone(); result.e0_= e0_!=null ? e0_.clone_() :null; result.e1_= e1_!=null ? e1_.clone_() :null; return result; } public boolean hasMoreElements() { fillBuffer(); return holder[0] != ENDTAG || holder[1]!= ENDTAG; } private void fillBuffer() { if (holder[0]== null) pop0(); if (holder[1]== null) pop1(); } private void pop0() { holder[0]= (Comparable)e0().nextElement(); } private void pop1() { holder[1]= (Comparable)e1().nextElement(); } private Object take0() { Object result= holder[0]; holder[0]=null; return result;} private Object take1() { Object result= holder[1]; holder[1]=null; return result;} public Object nextElement() { fillBuffer(); if (holder[0]==ENDTAG) if (holder[1]==ENDTAG) { return ENDTAG; } else { return take1(); } else if (holder[1]==ENDTAG) { return take0();} else { int c=holder[0].compareTo(holder[1]); if (c<0)return take0(); else if (c==0) {// eliminate the duplicate element holder[1]=null; return take0();} else return take1(); } }} return new UnionENUM(); } } return new _union(); }


Can you please show the class definitions?


EditText of this page (last edited December 30, 2004) or FindPage with title or text search