Object Calisthenics

Your code sucks, let's fix it!

Yay!

class Board {
    public String board() {
        StringBuilder buf = new StringBuilder();

        // 0
        for (int i = 0; i < 10; i++) {
            // 1
            for (int j = 0; j < 10; j++) {
                // 2
                buf.append(data[i][j]);
            }
            buf.append("\n");
        }

        return buf.toString();
    }
}

Extract Method

public String board() {
    StringBuilder buf = new StringBuilder();

    collectRows(buf);

    return buf.toString();
}
private void collectRows(StringBuilder buf) {
    for (int i = 0; i < 10; i++) {
        collectRow(buf, i);
    }
}

private void collectRow(StringBuilder buf, int row) {
    for (int i = 0; i < 10; i++) {
        buf.append(data[row][i]);
    }
    buf.append("\n");
}
http://refactoring.com/catalog/extractMethod.html

Example

public void login(String username, String password) {
    if (userRepository.isValid(username, password)) {
        redirect('homepage');
    } else {
        addFlash('error', 'Bad credentials');

        redirect('login');
    }
}

Early Return

public void login(String username, String password) {
    if (!userRepository.isValid(username, password)) {
        addFlash('error', 'Bad credentials');

        return redirect('login');
    }

    redirect('homepage');
}

Variable

public void login(String username, String password) {
    String redirectRoute = 'homepage';

    if (!userRepository.isValid(username, password)) {
        addFlash('error', 'Bad credentials');
        redirectRoute = 'login';
    }

    redirect(redirectRoute);
}

Example

class Location {                    class Piece {
    public Piece current;               public String representation;
}                                   }

class Board {
    public String boardRepresentation() {
        StringBuilder buf = new StringBuilder();

        for (Location loc : squares()) {
            buf.append(loc.current.representation.substring(0, 1));
        }

        return buf.toString();
    }
}

Talk To Your Friends!

class Location {
    private Piece current;

    public void addTo(StringBuilder buf) { current.addTo(buf); }
}

class Piece {
    private String representation;

    public String character() {
        return representation.substring(0, 1);
    }

    public void addTo(StringBuilder buf) {
        buf.append(character());
    }
}

Talk To Your Friends!

// Before:
// buf.append(loc.current.representation.substring(0, 1));

// After:
class Board {
    public String boardRepresentation() {
        StringBuilder buf = new StringBuilder();

        for (Location location : squares()) {
            location.addTo(buf);
        }

        return buf.toString();
    }
}

Write The Same Name Over And Over Again?

Then, your method is reused multiple times.

Looks like code duplication.

Method Name Too Long?

Maybe your class has multiple responsabilities.

Violation of the Single Responsibility Principle.

How?

https://github.com/TheLadders/object-calisthenics#rule-8-no-classes-with-more...