5

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: runTraffic and checkIfTrafficPassed.

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
8
  • It is very difficult to answer this question without some example method calls so we can see how these fictional methods are being used. Feb 15, 2019 at 12:37
  • well, its in theory. and it's in Perl and i don't want to scare everybody. i will at some pseudo code
    – Azuk
    Feb 15, 2019 at 12:39
  • I don't think Perl will scare anyone here. Feb 15, 2019 at 12:42
  • added in python :)
    – Azuk
    Feb 15, 2019 at 12:45
  • 2
    I would argue that there is no need to have a function which simply calls another two public functions with no added value or efficiency. A function can be called in multiple places in your code and still do one thing. If you need to create a runTrafficAndCheckIfItFailed function, you've defeated the purpose of trying to simplify and merge functions.
    – Neil
    Feb 15, 2019 at 13:00

2 Answers 2

15

Though the name gives a different impression, the function runTrafficAndCheckIfItPassed does only "one thing" in the "clean code sense": it coordinates the calls to two other functions.

The operational code for those two actions included, however, is in runTraffic and checkIfTrafficPassed, which are two functions for which we can assume they "do one thing" as well, but on a lower level of abstraction.

To apply the "do one thing" metapher correctly, one needs to take different levels of abstraction into account. Things which may be seen as one operation on a higher level usually consist of lots of single commands, actions and steps on a lower level.

To make the different levels of abstraction more transparent, it is often a good a idea to give a function like runTrafficAndCheckIfItPassed a different name, one which summarizes the combined actions under one concise term. If you cannot come up with such a name, then alas, let the name as it is now, naming things is one of the toughest tasks in software development ;-)

You find a good example in this older SE post.

5
  • so your saying that it is ok to use a runTrafficAndCheckIfItPassed function and to add another one runTrafficAndCheckIfItFailed`?
    – Azuk
    Feb 15, 2019 at 13:49
  • @Azuk: sure, why should that case be different? (It is actually hard to reason about artificial, contrived examples).
    – Doc Brown
    Feb 15, 2019 at 14:11
  • it isn't its the same just with a validation, im just making sure :)
    – Azuk
    Feb 15, 2019 at 14:16
  • @Azuk wouldn't failure be the logical inverse of passing? Why not !runTrafficAndCheckIfItPassed()?
    – cbojar
    Feb 16, 2019 at 1:05
  • The ckeck if traffic fails also reports the ui about each traffic component, and returns the summery. #spaggethy
    – Azuk
    Feb 16, 2019 at 9:12
4

Generally, anything with "and" in the name leaves a suspicion that it's violating the SRP. However, that sort of function could be renamed to something like "tryRunTraffic", with little change in meaning.

Functions named "try..." are often used to indicate that they will return a success or failure code, rather than throwing an exception if they fail. This is useful if failure is not an error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.