-4

Which way of defining a string would you use?

Are there better ways to do this with even more strings?

I want to set abbreviations for a large set of predefined strings.

If style 1:

let stat = str3;

if (stat === "str1") {
    stat = "string1";
} else if (stat === "str2") {
    stat = "string2";
} else if (stat ==="str3") {
    stat = "string3";
} else if (stat === "str4") {
    stat = "string4";
} else if (stat === "str5") {
    stat = "string5";
}

If style 2: (I think this is the best visually)

let stat = str3;

if (stat === "str1") stat = "string1";
else if (stat === "str2") stat = "string2";
else if (stat === "str3") stat = "string3";
else if (stat === "str4") stat = "string4";
else if (stat === "str5") stat = "string5";

Switch style:

let stat = str3;

switch (stat) {
case "str1":
    stat = "string1";
    break;
case "str2":
    stat = "string2";
    break;
case "str3":
    stat = "string3";
    break;
case "str4":
    stat = "string4";
    break;
case "str5":
    stat = "string5";
    break;
}
Whatever
  • 46
  • 5
  • 2
    `stat.replace("str", "string")` :P – Julia Feb 19 '22 at 13:32
  • So you would prefer 50+ stat.replace("str","string")? How would that affect performance, compared to if or switch? – Whatever Feb 19 '22 at 13:48
  • 1
    _"How would that affect performance, compared to if or switch?"_ That's called a micro optimization and wastes your time. Write readable and maintainable code. One line with `stat.replace("str", "string")` is more readable than a block with multiple if/else conditions. – jabaa Feb 19 '22 at 14:04
  • @Julia I would say that's the best answer for the given question. I would upvote it. – jabaa Feb 19 '22 at 14:07
  • Thank you, @jabaa, but my comment was meant in jest. I'm not sure about the OP's specifications—I assumed this was a (very) contrived example. – Julia Feb 19 '22 at 14:11

2 Answers2

1

Put all the data in an object and then just access the values by key.

const dict = {
  str1: 'string1',
  str2: 'string2'
};

const stat = 'str1';

console.log(dict[stat]);
Andy
  • 61,948
  • 13
  • 68
  • 95
  • 1
    A `Map` should be preferred to using a raw `object` as a dictionary type though: https://stackoverflow.com/questions/18541940/map-vs-object-in-javascript – Dai Feb 19 '22 at 13:36
  • 1
    We're not talking about a giant number of properties here @Dai. An object is fine. – Andy Feb 19 '22 at 13:39
  • Thank you, great one. How would you handle strings that don't match anything in the dict? – Whatever Feb 19 '22 at 13:55
  • 1
    Well, that's up to you. What result do you want? @Whatever `undefined`? `null`? `Not available`? – Andy Feb 19 '22 at 14:03
  • Nvm, I figured it out with a simple if condition, just wanted to keep the original string without changing the function which uses the string. – Whatever Feb 19 '22 at 14:06
0

In my opinion you should use Switch because it's faster than "if" and also if it gets bigger it will be easier to read.

A switch statement might prove to be faster than ifs provided number of cases are good. If there are only few cases, it might not effect the speed in any case. Prefer switch if the number of cases are more than 5 otherwise, you may use if-else too

You could read More in this link

Mohammad Edris Raufi
  • 1,393
  • 1
  • 13
  • 34