0

Consider following class:

public class Foo 
{
    public Foo() {}

    public void Bar(int input)
    {
        Console.WriteLine("Working on input ...");

        switch(input)
        {
            case 1:             
                File.WriteAllText("PATH", "The input is 1");
                break;

            case 2:             
                var data = int.parse(DateTime.Now.ToString("yyyy")) + input;

                File.WriteAllText("PATH", data);
                break;

            ...
        }
    }
}

This is a simple class, still have more than 1 responsibility, I think there is 2 responsibilities:

  • Logging Console.WriteLine
  • Persistence File.WriteAllText

Maybe there is more than this 2

Is there any guideline in order to helping us detect class responsibilities, so we can refactor it and for example apply SRP?

1 Answers1

4

Stop right there. You are falling into the trap - misinterpreting what "single responsibility" means.

Take a driving instructor whose single responsibility it is to teach people how to drive a car. He teaches the students hundred different things they need to know to drive. He also drives around to pick students up for their lessons or drop them off afterwards, does paperwork so that bills can be sent, makes sure the care is in a safe condition and so on. It's all part of the single responsibility.

Single responsibility does not equal "one single action".

gnasher729
  • 49,096