Twitter icon.
Random notes on software, programming and languages.
By Adrian Kuhn

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.

  1. Remove all redundant this references.
  2. Rename builder to $.
  3. 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.

Comments are closed.

For.example is Digg proof thanks to caching by WP Super Cache!