DEV Community

Discussion on: Code Smell 118 - Return False

Collapse
 
grahamthedev profile image
GrahamTheDev

I was kidding, I am enjoying the series, it was self deprecating humour! (Or meant to be 🤣)

Thread Thread
 
otumianempire profile image
Michael Otu • Edited

I have and I am learning a lot from these series. @mcsee a question please (your opinion)

say we have the code:

function canWeMoveOn() {
  if (work.hasPendingTasks())
    return false;
  else
    return true;
}
Enter fullscreen mode Exit fullscreen mode

All we want is the result of work.hasPendingTasks() and in this post, you suggested that it is better not to explicitly return a boolean.

So you refactored it to:

function canWeMoveOn() {
  return !work.hasPendingTasks();
}

Enter fullscreen mode Exit fullscreen mode

Now, the refactored code looks like a wrapper around work.hasPendingTasks(); and question is that, when it happens that we have a simple single lines like, it is okay to wrap them up? I mean, we could used the work.hasPendingTasks(); directly. What do you think or encourage? Thanks

Thread Thread
 
mcsee profile image
Maxi Contieri • Edited

canWeMoveOn the 'what'

hasPendingTaks the 'how'

they are accidentally coupled, now. But the caller to 'canWeMoveOn' does not know,
If you change your implementation or add new business rules or add a cache the caller should not be aware

I used this example here