I think it's a clever inversion of how switch statements normally work, but all the programmers I've shown this to started foaming at the mouth. đ I suppose horrible things can still be clever, lol.
Edit: oh, but actually what this does, without the break statements, is that it sets pageHeaderText to "Generic Header Text" if either secondaryDisplayMode OR createMode are true.
Neat implementation. I'd probably go with setting the variable to the default value first, then use an if/else if for the 2 cases. How is that considered verbose?
I've heard some devs complain that else ifs are verbose and to be avoided. I ended up doing that anyway, as it felt better than this and a nested ternary operator.
// secondaryDisplayMode and createMode received by component
let pageHeaderText;
switch (true) {
case secondaryDisplayMode:
pageHeaderText = 'Secondary Header Text';
case createMode:
pageHeaderText = 'Create Header Text';
default:
pageHeaderText = 'Generic Header Text';
}
// render pageHeaderText to dom
Create a variable named âmodeâ and an enum that includes each mode you want to support. Then instead of checking âtrueâ here, check âmodeâ then use the enum values for your cases. The best part is, if you ever want to add support for another mode, you just have to add it to the enum and add a new case for it and it wonât change any of the other existing logic.
Couldn't you use a map/dictionary, with the keys being your cases?
Also, I think you need break statements in there. Javascript switches seem to have fall through. I think in your example pageHeaderText will always be 'Generic Header Text' because of that.
Oh wait, you've got (true) as your switch expression? I think it takes the expression and checks for equality with === against the case value.
Honestly this example is completely broken, you might want to read the docs for this. Its a good read!
Switching on true is not bad in itself, I've used it to represent really complex logic more cleanly.
Another way is to break this function out so it returns the correct text and that's its only job. Then you can have single line ifs, and return a value.
The problem is that you have to switch on true at all-- probably some architecture refactoring would help here, so that the overall control flow is not so convoluted.
I'd use if statements without the else statements personally. I usually don't deal with switch statements until I hit 5 or 6 if statements in a row dealing with the same functionality.
Otherwise if your using a framework like Vue setting them to be computed properties or a single method in that case.
Hmm, generally I'm against storing constants in structures intended for logic flow control (such as switch or if statements).
EDIT: Instead, imo it'd be better to have one single function that gets the current mode. This makes the code easier to understand, more reusable and easier to test.
65
u/olegkikin Apr 15 '22
Your code is wrong without break statements anyway. Unless all your cases are guaranteed to be mutually exclusive.
7
u/alexalexalex09 Apr 14 '22
Honestly, this kinda blew my mind. Worth it.
3
u/ChocolateBunny Apr 14 '22
So what happens if secondaryDisplayMode and createMode are both true?
2
u/BD-96 Apr 15 '22
Neat implementation. I'd probably go with setting the variable to the default value first, then use an if/else if for the 2 cases. How is that considered verbose?
38
u/T-J_H Apr 14 '22
Or use an object/map. Wouldnât use it for too much logic, but I think it matches strings perfectly
14
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Apr 14 '22
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
1
u/taichouk Apr 15 '22
Why not use an Enum?
Create a variable named âmodeâ and an enum that includes each mode you want to support. Then instead of checking âtrueâ here, check âmodeâ then use the enum values for your cases. The best part is, if you ever want to add support for another mode, you just have to add it to the enum and add a new case for it and it wonât change any of the other existing logic.
3
u/dead_alchemy Apr 14 '22
Couldn't you use a map/dictionary, with the keys being your cases?
Also, I think you need break statements in there. Javascript switches seem to have fall through. I think in your example pageHeaderText will always be 'Generic Header Text' because of that.
Oh wait, you've got (true) as your switch expression? I think it takes the expression and checks for equality with === against the case value.
Honestly this example is completely broken, you might want to read the docs for this. Its a good read!
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch
2
u/Romejanic Apr 15 '22
I had a high school teacher who taught us to do this, it always felt kinda wrong but itâs also not bad
2
u/pivoters Apr 15 '22 edited Apr 15 '22
Wierdly kind of cool. Thanks for sharing!
0
u/[deleted] Apr 14 '22
It is. Myself, I would just use ternary statements.
1
u/mechanicalbro Apr 28 '22
Switching on true is not bad in itself, I've used it to represent really complex logic more cleanly.
Another way is to break this function out so it returns the correct text and that's its only job. Then you can have single line ifs, and return a value.
The problem is that you have to switch on true at all-- probably some architecture refactoring would help here, so that the overall control flow is not so convoluted.
1
u/anotherThrowaway1919 Apr 15 '22
Does anyone know if this ends up optimized out by the compiler? Iâm curious to if it affects branch execution
1
u/AdminYak846 Apr 15 '22
I'd use if statements without the else statements personally. I usually don't deal with switch statements until I hit 5 or 6 if statements in a row dealing with the same functionality.
Otherwise if your using a framework like Vue setting them to be computed properties or a single method in that case.
1
u/everettglovier Apr 15 '22 edited Apr 15 '22
Canât you do a one liner for each instead of using switch: secondaryDisplayMode && pageHeaderText=âSecondary Header Textâ;
1
u/[deleted] Apr 16 '22 edited Apr 16 '22
Hmm, generally I'm against storing constants in structures intended for logic flow control (such as switch or if statements).
EDIT: Instead, imo it'd be better to have one single function that gets the current mode. This makes the code easier to understand, more reusable and easier to test.
So then your code would look like this:
1
u/tesohh Apr 16 '22
Try ternary operators:
It looks complicated at first but trust me, it will save you so much time
1
u/Rick100006 Jul 18 '22
What?!
1
u/bitbot9000 Aug 11 '22
Create a function. GetPageHeaderText(âŚ). Use normal if statements that immediately return the string.