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?

2 Answers2

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

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.