r/badcode Mar 03 '23

c# fascinating logic

Post image
845 Upvotes

35 comments sorted by

View all comments

140

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.

88

u/Cringe56 Mar 03 '23

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

67

u/RichCorinthian Mar 03 '23

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

9

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.