-1

Is there any difference between 2 blocks of code below?

I would like to know which is the best practice. Is it legal to declare a new variable within a return statement like so in Block 2?

Block 1:

function caseInSwitch(num) {
  let answer = "";
  switch (num) {
    case 1:
      return answer += "alpha";
    case 2:
      return answer += "beta";
    case 3:
      return answer += "gamma";
    case 4:
      return answer += "delta";
  }
}
console.log(caseInSwitch(3)); //output gamma
console.log(answer); //output gamma

Block 2:

function caseInSwitch(num) {
  switch (num) {
    case 1:
      return answer = "alpha";
    case 2:
      return answer = "beta";
    case 3:
      return answer = "gamma";
    case 4:
      return answer = "delta";
  }
}
console.log(caseInSwitch(3)); //output gamma
console.log(answer); //output gamma

As you can see, I have tried both which yield the same result in the console.

shrys
  • 5,860
  • 2
  • 21
  • 36
kcs
  • 1
  • 1
  • 1
    Why not just `return "alpha"`? – adiga Jul 16 '19 at 03:51
  • 1
    With block 1, the console.log(answer); should return undefined, because it was defined with `let` in the function, so it's not exist in outside scope.With block 2, it's equals to defined a global variable `answer`. – AppleJam Jul 16 '19 at 03:59
  • @AppleJam you should post that as an answer – adiga Jul 16 '19 at 04:02
  • Neither one of these seems like “best” practice. I would just write it out so it’s obvious what is going on, using more than one line if needed. – John Wu Jul 16 '19 at 04:04
  • 1
    Wrong statement: "both [...] yield the same result in the console". Did you press "Run code snippet" at block 1? ;-) –  Jul 16 '19 at 04:13
  • Related, and worth a read: https://stackoverflow.com/questions/36707/should-a-function-have-only-one-return-statement – Jon P Jul 16 '19 at 04:13
  • As long as there is no error in the console, why should it be "illegal"? –  Jul 16 '19 at 04:29

3 Answers3

1

I think you can do as the following. There is nothing like legal. Do the most compatible way.

function caseInSwitch(num) {
  let answer = "";
  switch (num) {
    case 1:
      answer += "alpha";
      break;
    case 2:
      answer += "beta";
      break;
    case 3:
      answer += "gamma";
      break;
    case 4:
      answer += "delta";
      break;
  }
  return answer;
}
melvin
  • 2,571
  • 1
  • 14
  • 37
0

In Block 1, Your code is concatenated two strings together wherein Block 2 your code is assigning value to the variable. I suggest go with Block 2 because Block 1 is not the best practice.

0

In my opinion neither are a great practice.

Both options use multiple return paths, which should only be done with caution.

Your second option uses an undeclared variable which is therefore a global variable. Gobal variables are bad practice: Why are global variables considered bad practice?

Further more, not all paths return a value, again this is considered a bad practice, what should your function return if the parameter is invalid? It should return something.

Melvins answer is pretty much how I would do it but I wouldn't concatenate the string.

function caseInSwitch(num) {
  let answer = "";
  switch (num) {
    case 1:
      answer = "alpha";
      break;
    case 2:
      answer = "beta";
      break;
    case 3:
      answer = "gamma";
      break;
    case 4:
      answer = "delta";
      break;
  }
  return answer;
}

console.log("1 " + caseInSwitch(1));
console.log("7 " + caseInSwitch(7))

Other options could be to drop the switch statement and use an array or associative array

 function fromArray(num) {
      let answers = ["alpha", "beta", "gamma", "delta"];
      
      return (typeof answers[num-1] === 'undefined') ? "Not Found" : answers[num-1];
    }
    
 function fromAssocArray(num) {
      let answers = {1:"alpha", 2:"beta", 3:"gamma", 4:"delta"};
      
      return answers.hasOwnProperty(num) ?  answers[num] : "N/A";
    }    

    console.log("1 " + fromArray(1));
    console.log("7 " + fromArray(7));
    console.log("---------- Associated Array Below --------------");
    console.log("1 " + fromAssocArray(1));
    console.log("7 " + fromAssocArray(7));
Jon P
  • 19,442
  • 8
  • 49
  • 72