˝Refactoring is a controlled technique for improving the design of an existing code base” — Martin Fowler.
Discipline and methodology are critical when refactoring.
We should refactor applying a series of small controlled steps. After each of these steps our tests should still be green.
This way, if we break something in the process we can easily roll back.
Which prevents having a broken code base while we do the refactor.
Imagine we have a class which has some public methods and they have similar signatures. This is the perfect case to apply the Parameter Object Refactoring.
Here are the steps on how to do it:
nullfor this parameter value.
If we do it right, then at the end of the refactor our tests should pass and the system should work as before. It is important to have in mind that we are only improving the design of the code, not changing its behaviour.
Remember the Tell, don't ask principle:
"Object-orientation is about bundling together data and behaviour."
Passing around the same group of parameters, like database query parts, is a code smell we call data clumps which can be seen in the book Refactoring by Martin Fowler.
These clumps of data should be grouped in a class because they lead to code duplication. Maybe not in the sense of duplicated methods but in the treatment we do to those parameters like validations, parsing, etc.
You can check out this nice list for more code smells like the one mentioned.
Learning about refactoring techniques is very important for two reasons:
If you like this technique and you can't wait to jump in your code and refactor all the things I have one more piece of good news for you.
Android Studio (and of course IntelliJ) does this for you. For free. Gratis. Automatically.
So you don't need to remember all those steps. But it is always good to know what is going on under the hood, right? ;)
We plan, design, and develop the world’s most desirable software products. Our team’s expertise helps
brands like Sony, Motorola, Tesco, Channel4, BBC, and News Corp build fully customized Android devices
or simply make their mobile experiences the best on the market. Since 2008, our full in-house teams work
from London, Liverpool, Berlin, Barcelona, and NYC.
Let’s get in contact