Object Calisthenics is an exercise proposed by Jeff Bay in the book The Thoughtworks Anthology, that outlines a set of nine rules to be used to write a new object-oriented project, with around 1000 lines of code, to help us design a piece of software that really implements encapsulation well.
Some of the nine rules are very strict and challenging to apply somewhere else besides the exercise project, but a few of them can be easily implemented in any real project, resulting in a great improvement of reusability, cohesion, readability and maintainability.
Let’s take a look into all of them in detail (click on the link) to see how we can apply them with the latest Java updates such as lambdas, and modern frameworks such as ORMs and annotation processors. Then, let’s elaborate on the benefits of using these rules.
else
keyword.String
s.I strongly suggest trying out the Object Calisthenics exercise. Try creating a new project, and writing at least 1000 lines of code that follows these nine rules.
When I did it a few years ago it felt a bit weird in the beginning. Wrapping all those primitives and creating a bunch of classes just to accommodate the attributes that would end in just one table on the database, seemed too much code to not do much.
However, shortly after I started to notice how insightful the experience was: I didn’t have to provide more fields than I needed to handle a simple operation such as updating a password; my service classes were really small since they had to deal with smaller objects; and I never had to look into the declaration of my constructor anymore to check whether the first String parameter was the username or the password, because they were no longer Strings but proper Username and Password objects.
Those skills were quickly applied on my real projects: never used an else keyword anymore, now my code is easier to read with early returns, fewer lines and only one level of indentation; when an NPE is thrown, I know exactly where it happened since I am no longer chaining method calls in the same line.
Writing truly object-oriented code is challenging. Techniques such as Object Calisthenics, design patterns and best practices help us with accomplishing better this task.
Hope that happens to be as insightful to you as it were to me.
Happy coding =]
This guide will show you how to create a Python function decorator with a few…
This guide will show you how to fix the error Got permission denied while trying…
This guide will show you how to create a Python virtual environment on Intellij IDEA…
This tutorial will quickly show you how to to find and kill processes on Linux,…
This guide shows a possible solution for Python error Relocation R_X86_64_PC32 against symbol can not…
I condensed below a cheat sheet of Kubernetes useful commands. I will keep updating this…