O'Reilly logo

Real-World Maintainable Software by Abraham Marín-Pérez

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 3. Applying the Ten Guidelines

Apply All the Guidelines

Important as it is that these guidelines be applied to projects from day one, it is even more important that they are applied in their entirety. Applying only some of the guidelines may be even worse than not applying them at all, for this may cause a dangerous side effect: it could lead developers to think that the code is in a better state than it actually is. Let’s see how this applies in practice by assuming we are happy to apply the guidelines “Write Short Units of Code” and “Write Code Once,” but not “Keep Unit Interfaces Small,” and see what happens when we run the code.

Applying “Write Shorts Units of Code” and “Write Code Once”

Let’s assume that we are building a new system. The purpose of the system is not important right now, except to mention that this system will need a way to manage users. The first thing we create is a User class that simply holds the first, last, and middle name of the user.

public class User {
    private final String firstName;
    private final String middleName;
    private final String lastName;

    public User(String firstName, String middleName, 
            String lastName) {
        this.firstName = firstName;
        this.middleName = middleName;
        this.lastName = lastName;
    }
}

Adding Validation

The first validation requirement then arrives: while a middle name is optional, the first and last names are mandatory, non-blank values. We add some validation to the constructor. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required