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.
publicclassUser{privatefinalStringfirstName;privatefinalStringmiddleName;privatefinalStringlastName;publicUser(StringfirstName,StringmiddleName,StringlastName){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. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access