0

classes and method should do one thing like the code below:

class A {
  public int a() {
    b();
  }

  private int b() {
    c();
  }

  private int c() {

  }
}

The code mentioned below I found it as an interface in Clean Code book written by Robert C. Martin. I found some class like this in the book. In the code block the two methods doing two tasks.

class Modem {
  boolean connect() {
    //doing something
  }

  boolean disconnect() {
    //doing something
  }
}

But why by doing more than one thing like the second code block is valid?

3
  • 1
    You appear to be conflating multiple public methods with doing multiple things. You may want to read more on the single responsibility principle on this question. Jul 14 at 17:25
  • 1
    Since you define what "things" are, you can always say that something is one thing for you. I can have 10 different methods and still say it is one thing. This is also the reason SRP doesn't mean anything and you should just ignore it. Jul 14 at 21:07
  • It's valid because you got yourself utterly confused about the meaning of "Single Responsibility Principle". My local restaurant tried to implement SRP, so they hired one chef for beef, one for pork, one for poultry, one for fish, one for vegetables, one for soup, one for deserts. Then the pork chef complained he couldn't do both pork chops and pork sausages, that was too much responsibility. They are out of business now.
    – gnasher729
    Jul 15 at 6:49
0

Single responsibility does not means responsibility of single public method. Here is a code block:

interface Modem {
  public void dial(String pno);
  public void hangup();
  public void send(char c);
  public char recv();
}

Obviously, This interface violates the SRP rule. Here, We can find two responsibility: connection and communication. We need to separate the Modem into two classes. But we do not need to divide the class into four classes for four individual methods.

2
-2

One thing does not translate to one method in a class. Think like it's a "responsibility" of the class to do exactly one thing. In the example of Modem , it's just working for modem. So to change a state of the modem it has two method, it's doing one thing, changing the state of the modem.

Let me know this is the right thought or you have a different question.

Not the answer you're looking for? Browse other questions tagged or ask your own question.