0

I've recently started on the maintenance of a mature product and littered throughout I see switch statements used like such:

switch (true)
{
    case true when object.firstBoolValue:
        // do something
        break;
    case true when object.secondBoolValue:
        // do something
        break;
    case true when object.thirdBoolValue:
        // do something
        break;
}

Personally, I would have preferred to write it like this:

if (object.firstBoolValue)
    // do something
    return;
if (object.secondBoolValue)
    // do something
    return;
if (object.thirdBoolValue)
    // do something
    return;

// do default
return;

Primary question: which is better for readability? Secondary question: which design (if any) is more performant?

Alphonso77
  • 67
  • 9
  • 3
    Does this answer your question? [Is there any significant difference between using if/else and switch-case in C#?](https://stackoverflow.com/questions/395618/is-there-any-significant-difference-between-using-if-else-and-switch-case-in-c) – Ergis Dec 03 '20 at 16:39
  • 2
    I don't think this is a case where performance would make much of a difference. I'd say readability is more important here and IMHO the second one is less confusing. – juharr Dec 03 '20 at 16:39
  • 4
    Have you used any tools to further your expectations of performance? – Trevor Dec 03 '20 at 16:39
  • 1
    I doubt that there is any measurable difference but at least for me the second option is more readable. – Klaus Gütter Dec 03 '20 at 16:39
  • 1
    @Ergis I'm not sure that applies here since that's a 12 year old question and this is a switch that uses the when clause. – juharr Dec 03 '20 at 16:42
  • 1
    I second the doubt, that you will see significant performance differences. If you are curious, you could look into the produced IL code to see if it differs much. And if you want to have "hard figures": [run your horses](https://benchmarkdotnet.org/). To me, the first code is an utter misuse of pattern matching and the second is much more readable, but well - that's me. tl;dr: I doubt if this justifies a major refactoring throughout the codebase _for the sake of performance_. Benchmarks _could_ prove me wrong, though. – Fildor Dec 03 '20 at 16:47
  • 1
    And as always, see the [famous rant on answering performance questions](https://ericlippert.com/2012/12/17/performance-rant/). – Joe Sewell Dec 03 '20 at 16:49
  • The benefit here isn't performance. The `switch` version allows the logic path to bail out of the switch block and continue in the current method; the `if` version relies on `return`, which exits the method. Some people might use `while (true)` and `break` to accomplish the same thing. – John Wu Dec 03 '20 at 16:52
  • 1
    @Fildor in this case, a quick scan at the IL makes it clear that it isn't worth benchmarking: they compile *to the same IL* – Marc Gravell Dec 03 '20 at 16:52
  • @MarcGravell I suspected it but well, you never know until to look into it. Thanks for doing that! – Fildor Dec 03 '20 at 16:53
  • 1
    @JohnWu `if (obj.firstBoolValue) {...} else if (obj.secondBoolValue) {...} else if (obj.thirdBoolValue) { ... } else { ... } ...` - fixed – Marc Gravell Dec 03 '20 at 16:53
  • @MarcGravell I know how `else` works, but that isn't the example. Presumably the developer has issues with the [arrow anti-pattern](http://wiki.c2.com/?ArrowAntiPattern) and is avoiding `else`. Not saying I would use this construct; I'm just pointing out the difference. – John Wu Dec 03 '20 at 16:54

2 Answers2

7

They currently compile to the exact same IL, so no: there is no interesting difference - although this is an implementation detail that can change.

Frankly, the switch looks weird here.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
1

You can use the concept of the Time Complexity. It is used to calculate the performance of a given method or a piece of code counting basic operations.

The Best Case scenario of the first algorithm is Ω(2). The Worst is O(4)

The Best Case scenario of the second algorithm is Ω(2). The Worst is O(7)

Both of these algorithms run on a Constant complexity. At this scale it doesn't matter which logic you decide to implement.

Ivan
  • 399
  • 2
  • 5
  • 18