DEV Community

John Peters
John Peters

Posted on

Spot Separate Concerns Immediately

In accordance with the SRP principal we can train ourselves to recognize when to split out responsibilities while coding.

Take this example:

SRP

Do you see any concern with respect to SRP? Why yes, what is the call to UpdateArray doing? Is updating an array related to setting the state of an object? The answer is no.

Why?
Updating an array has nothing to do with stetting an object state. If we leave that code in, then it's most likely not going to be re-usable. A subsequent specification stated, repeat the HTTP get until a certain state is seen, then stop. In this case, by adding the repeated responses to the array, the output looked like this:

repeated

Each additional try was adding to the Array where, what needed to be done was each new response needed to replace the content. There's duplicate records in the array!

The Fix
Simply make a local array and return it. This localizes the array and allows the consumer to deal with it and does not mix concerns.

JWP2019

Top comments (0)