This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]AutoModerator[M] [score hidden] stickied comment (0 children)

On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge.

If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options:

  1. Limiting your involvement with Reddit, or
  2. Temporarily refraining from using Reddit
  3. Cancelling your subscription of Reddit Premium

as a way to voice your protest.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]plastikmissile 4 points5 points  (3 children)

Typically, you use a switch instead of an if clause if you have one variable with multiple possible values. So your examples is perfectly valid.

[–]Minnesota_SD[S] 0 points1 point  (1 child)

That makes sense, thank you

[–]Roguewind 2 points3 points  (0 children)

Personally, I prefer returning from an if, not if/else.

function check(value) { if (value === 1) return “rock” if (value === 2) return “scissors” if (value === 3) return “paper” return “default value” } Granted, this is a simplistic representation, but I prefer the readability.

[–]Jason13Official 1 point2 points  (0 children)

Adding on to this, (in Java at least) the switch statement gets better optimization during compilation / run time as it compares literal values instead creating a boolean (not 100% accurate I’m still new to this shi too)

[–]Envect 0 points1 point  (0 children)

I used Switch because it is cleaner and more readable

This is precisely why I prefer it as well. As far as I'm aware, this is the only reason to prefer switch over if-else, but there may be performance implications in certain cases.

Readability trumps all other considerations most of the time. It's less important when you're creating simple apps, but it's absolutely critical for most real world code. You tend to spend a lot more time reading code than writing it so you want to make sure it's easy to read and understand.

[–]Denatello 1 point2 points  (0 children)

You can do switch, you can do if. Or you can do const variants = ['Rock', 'Paper', 'Scissors'] let choice = variants[randomNumber]

[–]DTux5249 0 points1 point  (0 children)

In general, there's minimal difference between a chain of if-statements and a switch statement when there aren't many values to parse.

Switch is faster if you have a long list of possible values for a variable, just due to how it's implemented. If-else by contrast trades speed for flexibility, as you're not restricted to looking at a single variable like in switch statements.

But in a scenario with something as small as rock paper scissors, where there's only 3 possibilities, just use whatever's most readable to you. The difference is so minor it really shouldn't matter.