According to the Single Responsibility Pattern (SRP) a method or class should have one responsibility. I have read a couple of sources and viewed some videos and I would like to understand it by writing some code samples.
According to me, the following:
def addAndSubtract(a,b,c)
a + b - c
end
is conflicting with the SRP as this method has two responsibilities, i.e. addition and subtraction.
So how to align this with SRP? One responsibility per method right? Is the following correct?
def addAndSubtractSrp(a,b,c)
subtract(add(a,b),c)
end
def add(a,b)
a + b
end
def subtract(a,b)
a - b
end
Discussion
According to me, this is aligned with SRP as there are now two methods that have one responsibility. When I would like to add multiplication and division then I need to create separate methods right?
According to me, this is aligned with SRP as there are now two methods that have one responsibility.
-- No.addAndSubtractSrp
still does two things; it adds and subtracts. That you've refactored out the equation (and turned it into two methods, making the whole thing more complicated) doesn't change that fact.adjustBalance
, or whatever that equation actually accomplishes.