Incomplete object
I’m still trying to understand these ideas, how they fit together and what consequences have each one. So this will be more of a presentation of those ideas and the theories I have than a post. The content will be too abstract, I’m sorry about that. I couldn’t find a good example to explain them.
While refactoring the GUI code of tic-tac-toe I found an interesting technique that simplify moving things around where they should be and may unveil smells. An example could be found in my tic-tac-toe.
It is based on the idea that objects are bags of functions (methods) that operate with data (attributes). Also, it has in mind that is desirable to have high-cohesion, which in simplified terms means that each one of the functions use all the data. If you have high-cohesion could mean that the data and functions belong together.
In object-oriented, we have different ways to send that data to the object. We could send those arguments in the methods:
We could send it as arguments in the constructor:
Or we could have a combination of both:
You may prefer one or another depending on how the object collaborates with others.
If you know that the values of a
, b
and c
do not change often, you may prefer to pass all the arguments in the constructor and all the users of the object not worrying about any of those values.
If you know that some values change quite often, you may prefer to pass them as an argument and the clients are going to need to provide them.
But this seems odd. We are contaminating the solve_the_problem
procedure with more responsabilities: It is obtaining c
. It is using c
to complete what object
needs. And it is using object
to solve_the_problem
.
We need to provide a
, b
and c
before doing anything with object
. The problem we have is that those values come from different places. Here the Builder pattern may work. But I didn’t know it before this problem and it may seem extra complicated, so I did a simpler version of it:
Having that, there’s no much difference from:
To:
And from:
To:
With that, I had much more flexibility of moving object.a = ?
, object.b = ?
or object.c = ?
around until I found the place that made more sense, and then changed those to constructor and method arguments again.