Understanding Single Responsibility Pattern (SRP)

by 030   Last Updated January 13, 2018 00:05 AM

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?



Related Questions