0

I have method that checks 2 parameters and return string

Here is code

private getClientBankAuthStatus(bankAuth: BankAuthenticationDto, isExpired: boolean): string {
    if (bankAuth && isExpired) {
        return 'Expired';
    }
    if (bankAuth && !isExpired) {
        return 'Yes';
    }
    if (!bankAuth) {
        return 'No';
    }
}

Can I somehow convert it to switch-case?

Eugene Sukh
  • 2,357
  • 4
  • 42
  • 86

4 Answers4

4

instead of a switch case use a nested Ternary Operator ?::

return !bankAuth ? "No" : isExpired ? "Expired" : "Yes";

Example:

const getStatus = (a, e) => !a ? "No" : e ? "Expired" : "Yes";

console.log(getStatus(false, false)) // No
console.log(getStatus(false, true))  // No
console.log(getStatus(true,  false)) // Yes
console.log(getStatus(true,  true))  // Expired

Here's Another example just if you like code-golfing with a Left Shift << (don't use in production to impress coworkers):

const getStatus = (a, e) => ["No","Expired","Yes"][a << !e];

console.log(getStatus(0,0)) // No
console.log(getStatus(0,1)) // No
console.log(getStatus(1,0)) // Yes
console.log(getStatus(1,1)) // Expired

Or if you want just a single character shortcode: getStatus=(a,e)=>"NXY"[a<<!e]

Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
  • *"If you want you could even simplify it further to"* If I found that in code written by a team member, we'd be having words. ;-) – T.J. Crowder Mar 05 '21 at 10:09
  • 1
    @T.J.Crowder I must agree on that one haha- will rephrase – Roko C. Buljan Mar 05 '21 at 10:13
  • _"don't use in production"_ - could you please explain? – ruth Mar 05 '21 at 11:21
  • 1
    @MichaelD it's not human readable, anyone trying to fix a bug will probably be stumped in front of that Left Shift wondering if that is the source of the problem :) I mean, you *can* but we should always write code like the person who will replace us - knows exactly where we live :D Therefore - yes you can, but Ternary operators are (arguably) far more readable. – Roko C. Buljan Mar 05 '21 at 11:32
  • @RokoC.Buljan: Right, I was wondering for reasons other than readability. Thanks for the clarification :) – ruth Mar 05 '21 at 11:36
  • @MichaelD there's no other reason: passing two Boolean arguments into `getStatus(boolean_a, boolean_e)` will give exactly the expected string results without errors or nuisances. Thx for the – Roko C. Buljan Mar 05 '21 at 11:46
1

Can I somehow convert it to switch-case?

You can (see below), but it probably wouldn't be best practice. You can simplify it a bit though:

private getClientBankAuthStatus(bankAuth: BankAuthenticationDto, isExpired: boolean): string {
    if (!bankAuth) {
        return "No";
    }
    return isExpired ? "Expired" : "Yes";
}

But for completeness, the switch version might be:

private getClientBankAuthStatus(bankAuth: BankAuthenticationDto, isExpired: boolean): string {
    switch (`${bankAuth ? "auth" : ""}-${isExpired ? "expired" : "ok"}`) {
        case "auth-ok":
            return "Yes";
        case "auth-expired":
            return "Expired";
        default:
            return "No";
    }
}

...or there's Nina's version, but again, I wouldn't use switch here at all.


Side note: I'd use a different return type on the function. Either use an enum rather than string literals, or use a union of string literals types:

private getClientBankAuthStatus(
    bankAuth: BankAuthenticationDto,
    isExpired: boolean
): "Yes" | "No" | "Expired" {
   ^^^^^^^^^^^^^^^^^^^^^^^^
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    Your switch is quite a bit harder to read than the hard to read nested ternary. But miles better than switch(true) – mplungjan Mar 05 '21 at 09:59
  • 1
    @mplungjan - Big time. For me it's: 1. My first block above, 2. nested conditionals, 3. an alternative I haven't thought of yet. 4. another alternative I haven't thought of yet, 5. 6. 7. 8. 9. `switch`. :-D – T.J. Crowder Mar 05 '21 at 10:11
1

Please do not try to use switch on booleans.

switch (true) is a code smell and not recommended. It is using a side effect of the switch and not what the switch statement was made to do.

Instead use one test and a ternary

if (!bankAuth) return 'No';
return isExpired ? 'Expired': 'Yes';

A nested ternary is harder to read

mplungjan
  • 169,008
  • 28
  • 173
  • 236
0

You could move the conditions a bit and exit early.

switch (true) {
    case !bankAuth: return 'No';
    case isExpired: return 'Expired';
    default: return 'Yes';
}
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • I don't understand the downvote here. The OP specifically asked for a `switch` solution, and this is a concise but (to a JavaScript coder) fairly clear `switch` solution. (I would never do it in production code, but that's not the point.) – T.J. Crowder Mar 05 '21 at 10:00
  • It is mine. I ABHOR using side effects. It is a code smell! – mplungjan Mar 05 '21 at 10:02
  • @mplungjan - It's not a side effect. But I don't care for doing it. – T.J. Crowder Mar 05 '21 at 10:03
  • 1
    I call it a side effect. If there is a better name for abusing a construct like this, then let me know and I will use that instead :) – mplungjan Mar 05 '21 at 10:06
  • 1
    `case` labels are expressions, just like the bit in `()` in an `if` (or `else if`) is an expression. Are those side-effects? :-) It's just that JavaScript's `switch` differs from that in C, C#, Java, etc., and that difference is surprising. That's why I avoid making use of it. (A side effect would be, say, a function call in a `case` label that did something other than calculate a return value. And God help me, I've seen it.) – T.J. Crowder Mar 05 '21 at 10:07