Understanding the Single Responsibility Principle

后端 未结 2 1330
闹比i
闹比i 2020-12-17 20:25

I am quite confused on how to determine if a single method has one responsibility being done just like the following code from the book Clean Code

public Mon         


        
2条回答
  •  一向
    一向 (楼主)
    2020-12-17 21:02

    I generally apply the SRP at the class level. It prevents classes becoming too big and taking on too many roles.

    I treat the 'responsibilities' as conceptual. Therefore, I would argue that your method has only one responsibility: to calculate pay.

    I try and follow Google's guidelines on this and look for these warning signs that suggest you're wandering away from the SRP:

    • Summarising what the class does includes the word ‘and’.
    • Class would be challenging for new team members or inexperienced developers to read and quickly ‘get it’.
    • Class has fields that are only used in some methods.
    • Class has static methods that only operate on parameters.

    On another note, however, your code contains a switch statement that suggests you might be wandering away from the OCP...

提交回复
热议问题