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

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)

def add(a,b)
  a + b

def subtract(a,b)
  a - b


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