This is Verbose!
Yesterday I came across the following piece of Java code. The code visits an abstract syntax tree and pretty prints it.
public void visitIf(If node) {
this.builder.append(this.indentation());
this.builder.append("if ");
node.condition.accept(this);
this.builder.append(" then\n");
this.increaseIndentation();
node.body.accept(this);
this.decreaseIndentation();
this.builder.append(this.indentation());
this.builder.append("end\n");
}
This is verbose! Way too much syntactic noise. As we know from XP, it is sometimes best to turn the knob to the extremes—so let’s turn the knob from Corporate Java™ to Spartan Programming.
- Remove all redundant
thisreferences. - Rename
builderto$. - Replace the procedural indentation with Class Tab.
public void visitIf(If node) {
$.append(tab+"if ");
node.condition.accept(this);
$.append(" then\n");
tab.more();
node.body.accept(this);
tab.less();
$.append(tab+"end\n");
}
This is Sparta! Less noise, more readable.