I was recently tasked with adding the ability to change an owner id on a template on creation and, subsequently, to add the ability to modify the id after creation as well. I worked on the task and was able to get the functionality that I wanted. But after submitting it for code review, I was lucky to have a colleague point out some places where the code could be refactored to make it easier to understand and to make it more maintainable. On my first stab at the task, I started off in the controller which included a method to create a template. Originally, the create_campaign_template method took just an id as an argument I modified it to also take a object which when present would be the user who would become the new owner.
The user that was passed in to the create_campaign_template method was looked up by the controller using an email string that was passed in from the client, if no user was found the value was defaulted to nil. That method would go ahead and delegate the work to the Creator object. There I would check to see if a User object had been passed in, if it had I went and checked that it had certain authorizations, then if those conditions were met I would go ahead and reassign.
At this point the code worked and it did what we wanted it to do, but I had broken the single responsibility principle. The Creator object now not only had the task of preparing an object for creation, but it also had to check if the object needed to be reassigned and then went ahead and did the reassignment. It also violated the Open/Closed principle by adding a new concept that the object did not need to know about thereby making it more difficult to extend. These were good indicators that another object could be abstracted from here.
So I created a new Assigner object that removes some of the unnecessary responsibilities from the Creator. In the Assigner object I go ahead and make the assignee email string perform the user lookup, which was previously done by the controller. The Assigner also goes ahead and checks for the conditions that indicate that it is ok to reassign and because they are made in the context of the assignment they are communicated more clearly. After the first go through, the Assigner was in charge of wrangling data to look the way it needed it to look, checked to make sure the reassignment was possible, and did the reassignment. This felt like a bit too much responsibility. So I decided to go ahead and extract an Assign object out of the Assigner; the Assign object would simply perform one clear operation and that would be to assign stuff.
Get Ivan Gonzalez’s stories in your inbox
Join Medium for free to get updates from this writer.
After going through the refactor the objects in the code maintain Single Responsibility, follow the Open/Closed principle, and are also Liskov Substitutable. I mentioned an example of single responsibility earlier when I talked about the Creator object originally being in charge of the reassignment. This is important because an object with a single responsibility is easier to debug; when we start giving it more responsibility we give it more room for error. After the refactor, the Objects that were previously there and ones that were created during the rewrite are open for extension but closed for modification. This is accomplished by keeping their scope narrow, they only know what they need to know and only perform one task. This means that it needs no modification but, if later on, new checks needed to be performed during assignment they could easily be added on in an intuitive location.








