I've just read the book called Clean Code. The author (Robert C. Martin) talks about a single responsibility that a function should have in a program. It should only do one thing.
Now, I would like to understand how is it now possible to reuse a code that does multiple things. Let's say I have a method called
runTrafficAndCheckIfItPassed and it calls two methods inside of it:
Now let's say that in my software I need to run traffic and to check it's result in a lot of places in my software. sometimes I need to check that traffic has failed, and sometimes I need to check if it passed.
Why wouldn't it be right to call the
runTrafficAndCheckIfItPassed function and why is it way better to call the functions inside separately?
As far as I see, if there will be a change in the
runTraffic function, for example to receive another parameter, the change will be implemented in one place, only in
runTrafficAndCheckIfItPassed, which we see will be easy to maintain.
But if I will use the functions separately I will need to change it in any place. But the author says it's wrong. So do you have any examples or tips why it is considered wrong?
Here's how I used it:
runTrafficAndCheckIfItPassed(TCPTraffic): trafficResults=runTraffic(TCPTraffic) hasAllTrafficPassed=checkIfTrafficPassed(trafficResults) return hasAllTrafficPassed