How to check single reponsibility principle
In a good object oriented design each object should have just one responsibility as it guarantees simpler, more readable and maintainable code. Most programmers know that, but they don’t know how to check whether it’s really true. Fortunately, there is a really simple way to validate this idea.
Describe an object in one sentence without any and’s, or’s and but’s.
If you managed to describe it without any conjunctions you created a nice object with a single responsibility. Let’s have a look at an example of violation of this principle by asking a fellow programmer during code review about one of the objects they created.
Well, this objects calculates a GST rate for a given price … oh and it also gives you current GST rates.
The programmer used and while describing an object so by using this rule of thumb you can identify that it has two responsibilities and break it down to two — one for calculations and the other for rate provisioning.
Would you like to get top 5 links on Programming every Monday?
Sign up to Programming Digest and stay up to date!
Did you like the article? Send me a comment!