3
\$\begingroup\$

I have written code that gets the reputation badge from given score like this:

I use an if-else statement here:

func GetReputationBadge2(score int) string {

    badge := "badges-off.jpg"
    if score >= 50001 { // Diamond
        badge = "diamond-1.gif"
        if score >= 500001 {
            badge = "diamond-5.gif"
        } else if score >= 200001 {
            badge = "diamond-4.gif"
        } else if score >= 150001 {
            badge = "diamond-3.gif"
        } else if score >= 100001 {
            badge = "diamond-2.gif"
        }
    } else if score >= 4501 { // Gold
        badge = "gold-1.gif"
        if score >= 45001 {
            badge = "gold-5.gif"
        } else if score >= 30001 {
            badge = "gold-4.gif"
        } else if score >= 15001 {
            badge = "gold-3.gif"
        } else if score >= 10001 {
            badge = "gold-4.gif"
        }
    }

    return badge
}

I was kinda thinking that I could make it better using a switch-case statement:

func GetReputationBadge(score int) string {

    badge := "badges-off.jpg"
    caseSwitch := true
    switch caseSwitch {
    case score >= 50001:
        badge = "diamond-1.gif"

        switch caseSwitch {
        case score >= 500001:
            badge = "diamond-5.gif"
        case score >= 200001:
            badge = "diamond-4.gif"
        case score >= 150001:
            badge = "diamond-3.gif"
        case score >= 100001:
            badge = "diamond-2.gif"

        }

    case score >= 4501:
        badge = "gold-1.gif"

        switch caseSwitch {
        case score >= 45001:
            badge = "gold-5.gif"
        case score >= 30001:
            badge = "gold-4.gif"
        case score >= 15001:
            badge = "gold-3.gif"
        case score >= 10001:
            badge = "gold-2.gif"

        }

    default:
        return badge
    }

    return badge
}

Notice that I'm comparing boolean in the switch-case statement. Which are better for performance: using if-else or switch-case statement in this case?

\$\endgroup\$
2
\$\begingroup\$

switch is usually easier to read instead of a long if-else chain.


You really don't need to write a switch like this:

    caseSwitch := true
    switch caseSwitch {

Replace with simply:

    switch {

There's no need to nest switch cases. It will be simpler and easier to understand to flatten the conditions. Also, there's no need for the badge variable, you can just return directly.

switch {
case score >= 500001:
    return "diamond-5.gif"
case score >= 200001:
    return "diamond-4.gif"
case score >= 150001:
    return "diamond-3.gif"
case score >= 100001:
    return "diamond-2.gif"
case score >= 50001:
    return "diamond-1.gif"
case score >= 45001:
    return "gold-5.gif"
case score >= 30001:
    return "gold-4.gif"
case score >= 15001:
    return "gold-3.gif"
case score >= 10001:
    return "gold-2.gif"
case score >= 4501:
    return "gold-1.gif"
default:
    return "badges-off.jpg"
}

What will be even better is making an array with values corresponding to the range start and image name pairs. Then instead of a switch, you could iterate over the values until you find the right range and return the image.

And what will be best is, instead of iterating over the values, use binary search to find the right value faster.

\$\endgroup\$
0
2
\$\begingroup\$

For just a few items, the difference is small. If you have many items you should definitely use a switch.

This question might help: https://stackoverflow.com/questions/29566229/go-switch-string-efficiency

\$\endgroup\$
1
  • \$\begingroup\$ since I'm comparing boolean in switch-case statement, is it going to decrease the performance? \$\endgroup\$ – Gujarat Santana Nov 27 '16 at 2:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.