1
\$\begingroup\$

Does this function

automationStatusIsSuccessful = (data) ->
    data.Automation.Status isnt "FAILED" and data.Automation.Status isnt "COMPLETEDWITHERROR"` 

make the conditional statement more readable or less readable?


With:

  automationStatusIsSuccessful = (data) ->
    data.Automation.Status isnt "FAILED" and data.Automation.Status isnt "COMPLETEDWITHERROR"

  checkIteration = (data) ->
    if iteration < Configuration.CHECK_ITERATIONS and automationStatusIsSuccessful(data)

Without:

checkIteration = (data) ->
  if iteration < Configuration.CHECK_ITERATIONS and data.Automation.Status isnt "FAILED" \
  and data.Automation.Status isnt "COMPLETEDWITHERROR"
\$\endgroup\$
6
  • 1
    \$\begingroup\$ Are you really looking for a code review? If so we will need context of what the code does, and your complete implementation not just a snippet. \$\endgroup\$
    – Phrancis
    Commented Sep 11, 2014 at 21:55
  • \$\begingroup\$ Updated original post with question \$\endgroup\$
    – dman
    Commented Sep 11, 2014 at 23:06
  • \$\begingroup\$ Could you also provide some context on this code (especially for giving a better title)? \$\endgroup\$
    – Jamal
    Commented Sep 11, 2014 at 23:48
  • 2
    \$\begingroup\$ To make life easier for reviewers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. See also this meta question \$\endgroup\$ Commented Sep 12, 2014 at 0:05
  • 1
    \$\begingroup\$ I don't know coffeescript, but it seems to me the "with" and "without" snippets have more differences than just using or not the automationStatusIsSuccessful. For example maybe the url.get().then (data) ... in "without" shouldn't be there? It would be a lot easier to see what you want. And yes, using a function is definitely more readable: extracting complex conditions to a method is a common refactoring technique. After you clarify I'll be happy to vote to reopen + upvote \$\endgroup\$
    – janos
    Commented Sep 12, 2014 at 16:54

1 Answer 1

1
\$\begingroup\$

I don't know coffeescript, but in general, extracting complex conditions to a helper function is a common refactoring technique. When you do that, you effectively give a name to the previously nameless condition, which works like documentation, and can naturally improve readability.

Another advantage is that such function would hide the details of the inner workings of determining whether the automation status was successful or not. So if later you want to change the underlying conditions, the place to do that will be well identified, in a dedicated function, rather than buried inside some other function.

Of course, this doesn't mean that you should extract every less-than-trivial boolean condition to a helper function. Where you draw the line may be different from person to person. The fact that you're asking this question seems to suggest that you're inclined to make this move. (Personally I would extract it too.)

\$\endgroup\$

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