0

I have often wondered if it is bad practice to allow sequential execution to decide the return value and termination point. EG (this is a question surrounding IF statements, so ignore the fact there are myriad better ways to do this simple function!):

function isGreaterthanTen(val) {
  if(val > 10) return true;
  return false;
}

Same effect, is smaller but maybe less readable than:

function isGreaterthanTen(val) {
  if(val > 10) {
    return true;
  } else {
    return false;
  }
}

So is one better than the other, best practice wise and/or min-wise?

user4893295
  • 533
  • 6
  • 25
  • 2
    maybe this helps: [Should I return from a function early or use an if statement?](https://softwareengineering.stackexchange.com/q/18454/) – Nina Scholz Jul 10 '19 at 12:40
  • 9
    Why not just: `return val > 10;` ? – Mr. Polywhirl Jul 10 '19 at 12:40
  • 1
    `const isGreaterthanTen = (v) => v > 10;` – Reactgular Jul 10 '19 at 12:41
  • 1
    Im not sure about best practice but seems like the sort of thing that may be covered by most organisations coding standards. For me, the first is better and also lends itself better to longer functions handling multiple return statements. – Liam MacDonald Jul 10 '19 at 12:41
  • 1
    I would prefer the first version. If you are sure to return in your `if` block, there is not point in having an `else` block. – Seblor Jul 10 '19 at 12:44
  • I strongly prefer to only ever have **one** return statement per function (unless you need early ejects). Especially in long code I don't want to have to search for potential exit points. – connexo Jul 11 '19 at 05:47

2 Answers2

1

At my current work - Leads never advise to use single line execution without brackets stating readability lacks there.

if(true) {
 return 'good practice'
}

if(true) 
 return 'bad practice'

Another related point I always follow is checking for negative value first

if(false) {
  return 'this is the negative case'
}
return 'case for most of the positive case'

However I know different ways to just handle such single line returns

If just boolean than you can simply return the condition

function foo(){
 return 1 === 1
}

console.log(foo())

You can use coerced value for just boolean value if your value is treated as truthy or falsy and not a boolean

function foo() {
 return !!1 // 1 treated as true 
}

console.log(foo())

If you have two different values to get returned so you probably use ternary too.

function foo() {
 const me = 'satyam',
  friend = 'jack',
  satyamAge = 23
  
  return satyamAge === 20 ? me : friend

}

console.log(foo())
Satyam Pathak
  • 6,612
  • 3
  • 25
  • 52
  • This is not what OP's question is about. It's about whether or not it is "better" to create an `else` block when the `if` block has a return statement at the end. – Seblor Jul 10 '19 at 14:29
  • Thanks updated my answer as per my understanding over good or bad practices in the referred case – Satyam Pathak Jul 11 '19 at 05:42
1

You should avoid writing so called spaghetti code. That means, one should be able to read your code without jumping from one place to another in order to untangle and understand it. Having return statement in the middle of your code can be considered a spaghetti code, especially in longer functions.

I prefer having one return statement that returns a variable, which I declare at the top of the function and manipulate throughout the execution.

function isGreaterthanTen(val) {
    let isGreater = false;

    if (val > 10) {
        isGreater = true;
    }

    return isGreater;
}

of course that function can be shortened to:

function isGreaterthanTen(val) {
    return val > 10;
}

A one exception from this rule which I'd allow is at the top of the function when you validate the data and prevent the execution:

function isGreaterthanTen(val) {
    if (typeof val !== 'number')
        return;

    return val > 10;
}

which fill return nothing if the parameter isn't a number.

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
Nightray
  • 93
  • 3