Refactor Code : Separate Query from Modifier
In this post we will learn about the one of the important code refactoring algorithm "Separate Query from Modifier".When you have a method that gives you a value and has no observable side effects, you have a lot less to worry. You can move the call to other places in this method freely. A good rule to follow is to say that any method that returns a value should not have other observable side effects.
We first take the example of the below and will find out what is wrong with this code and refactor it.
In the above , method findMiscreant(List<Person> people) signifies that its responsibility will be to find miscreant only. But if we give clear look to the method , apart from finding Miscreant , it is sending some notification as SendAlert() to other system .
Hence we can easily see that this method is doing two job as :
1) querying to find miscreant
2) sending notification and may change state of the some object.
First principle of Single Responsibility from S.O.L.I.D design principle state that "every class or method should do only one job".
We can break the findMiscreant(List<Person> people) into two method as SendMiscreantNotification(List<Person> people) and findMiscreant(List<Person> people) itself
where responsibility of each one will be to send notification and find miscreant respectively.
The code after refactoring will be as below :
Reference :
https://www.csie.ntu.edu.tw/~r95004/Refactoring_improving_the_design_of_existing_code.pdf
It covers the "Separate Query from Modifier" concepts , if you have any issue or anything to add , please comment below .
You have a method that returns a value but also changes the state of an object . Create two method, one for the query and one for the modification.
We first take the example of the below and will find out what is wrong with this code and refactor it.
In the above , method findMiscreant(List<Person> people) signifies that its responsibility will be to find miscreant only. But if we give clear look to the method , apart from finding Miscreant , it is sending some notification as SendAlert() to other system .
1) querying to find miscreant
2) sending notification and may change state of the some object.
First principle of Single Responsibility from S.O.L.I.D design principle state that "every class or method should do only one job".
We can break the findMiscreant(List<Person> people) into two method as SendMiscreantNotification(List<Person> people) and findMiscreant(List<Person> people) itself
where responsibility of each one will be to send notification and find miscreant respectively.
The code after refactoring will be as below :
Reference :
https://www.csie.ntu.edu.tw/~r95004/Refactoring_improving_the_design_of_existing_code.pdf
It covers the "Separate Query from Modifier" concepts , if you have any issue or anything to add , please comment below .
This comment has been removed by the author.
ReplyDeletewhy are you calling sendAlert() again in findMiscreant() after refactor?!
ReplyDelete