When organizing methods inside a class, I use Uncle Bob’s stepdown rule (page 37 of the Clean Code book, Episode 3, Functions of the Clean Coders series).

What does the rule say? The code should read like a top-down narrative. Start with high-level, abstract paragraphs. Then dive into details. Translating it into the world of functions: public go first, below you put private functions (used by the public functions), then private functions called from private functions, and so on.

Until recently, I was reordering methods manually, until I discovered that IntelliJ can do this work for you (with some limitations, as we are going to see soon).

This formatting is not enabled by default. To turn it on, go to Preferences, then Editor/Code Style/Java. In the Arrangement tab, check Keep dependent methods together and select breadth-first order or depth-first order.

Configure Java code arrangment

How does it work? Let’s reformat this class:

public class StepdownRule {
    private void nestedPrivateMethod() {}

    private void secondPrivateMethod() {}

    private void firstPrivateMethod() {
        nestedPrivateMethod();
    }

    public void publicMethod() {
        firstPrivateMethod();
        secondPrivateMethod();
    }
}

Show Reformat File Dialog (alt-shift-cmd-L on Mac), mark Rearrange code and let the IDE do the work for you:

stepdown_rule_reformat

Breadth-first

public class StepdownRule {
    public void publicMethod() {
        firstPrivateMethod();
        secondPrivateMethod();
    }

    private void firstPrivateMethod() {
        nestedPrivateMethod();
    }

    private void secondPrivateMethod() {}

    private void nestedPrivateMethod() {}
}

Depth-first

public class StepdownRule {
    public void publicMethod() {
        firstPrivateMethod();
        secondPrivateMethod();
    }

    private void secondPrivateMethod() {}

    private void firstPrivateMethod() {
        nestedPrivateMethod();
    }

    private void nestedPrivateMethod() {}
}

In both orderings, public method goes first. Fine.

Breadth-first puts then both first-level private methods, placing them in the same order as they are called from the public method. Then go second-level private methods.

Depth-first algorithm doesn’t take into account the call order. It favors private methods without nesting. Then it puts private methods calling other private methods.

I’d expect that depth-first ordering should reorder everything perfectly according to the stepdown rule. However, there is an issue – private methods of the same call level hierarchy remain unordered. Breadth-first takes the call order into account, but the stepdown rule is not 100% followed. Choose the lesser evil.

Featured image by Donncha Ó Caoimh.

Advertisements