Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switch? #13

Open
paulomcnally opened this issue Dec 24, 2014 · 2 comments
Open

switch? #13

paulomcnally opened this issue Dec 24, 2014 · 2 comments

Comments

@paulomcnally
Copy link

What is the best practices with switch?

switch (new Date().getDay()) {
    case 0:
        day = "Sunday";
        break;
    case 1:
        day = "Monday";
        break;
    case 2:
        day = "Tuesday";
        break;
    case 3:
        day = "Wednesday";
        break;
    case 4:
        day = "Thursday";
        break;
    case 5:
        day = "Friday";
        break;
    case 6:
        day = "Saturday";
        break;
}
@EggDice
Copy link

EggDice commented Dec 25, 2014

I would say try to avoid using switch. It is many times a smell of some kind of object hierarchy. In your example way clearer to use a plain data-structure that contains the day information:

var DAYS = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var day = DAYS[new Date().getDay()];

I don't state that switch is always wrong, in some cases it is the best option, but it is a dangerous tool (you can forget breaks) and many times there is a way clearer and easier to read way.

@gergelyke
Copy link
Contributor

@EggDice, for the record, could you send a PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants