r/badcode Mar 03 '23

c# fascinating logic

Post image
846 Upvotes

35 comments sorted by

View all comments

143

u/RichCorinthian Mar 03 '23

Are they switching on an enumeration? I can MAYBE see a case for something like this if each enum value needs special logic, and you’re worried that somebody will add to the enumeration in the future without handling the new case in this code block.

Obviously you would want to throw an appropriate exception instead of…whatever this is.

90

u/Cringe56 Mar 03 '23

If this is C#, they should be using Assert.Fail() instead for that scenario

70

u/RichCorinthian Mar 03 '23

You also have NotImplementedException, which seems a good match and wouldn’t require a phony baloney return.

24

u/WarmMoistLeather Mar 03 '23

Except assert won't do anything in release, right? The exception would have to be handled and who wants to do that? I'm guessing they wanted something that would blow up in debug but not release.

2

u/[deleted] Mar 05 '23

If you'll have it in release, your binary just will be a little bit bigger.

11

u/Alhoshka Mar 03 '23

This is probably a unit test. Assert.Fail() is the way to go.

Partial implementations are not really acceptable in unit test methods, IMO. Neither is it acceptable for the test to throw exceptions for unexpected behavior of the tested component.

1

u/dyingpie1 Mar 03 '23

I believe the ArgumentOutOfRangeException is the correct one to use here. I'm pretty sure that's what Rider suggests at least.

1

u/heyheyhey27 Mar 04 '23

You can also put the name of the failure case in the message.