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.
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.
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.
This is a good point. Usually, assert is to debug code, which may make sense in a non-production environment. If this is an ongoing and unknown issue, the code written wrong. IMO
Honestly, even in a unit test I'd still have issues with it. I'd have more issues with there being a switch statement in a unit test, but things like Assert.IsFalse(true) are just very cumbersome to read.
Recently at work I've been working on an API with integration tests that we set to run in a specific order (so that, for example, the first test creates an entity in a DB that is then pulled from the DB in later tests) and one of my colleagues insisted on repeatedly adding checks at the start of those later tests that looked like this:
if (!_fixture.IsFooCreated || !_fixture.IsBarCreated)
{
false.Should().BeTrue();
}
I repeatedly complained that the false.Should().BeTrue() line was awkward to read, and suggested changing the check to something like:
var preconditionsMet = _fixture.IsFooCreated && _fixture.IsBarCreated;
preconditionsMet.Should().BeTrue();
But I still periodically come across false.Should().BeTrue() in the code. I seem to be the only one irritated by it so I've gradually given up complaining about it - I might still call it out when reviewing PRs but I've just lost the energy to keep up fighting for what I feel is more logical and easy-to-read code.
Dont know what language is on the screen but that is a usual practice to handle switches enumrations in C or C++. This is for the case when you want some error if someone adds a new entry to enumeration and it does not have a case in your function. But yes, even for C and C++ this is the laziest way since this check can be performed at compile time or even with compiler.
Some languages can't check it at compile time when an enum is just an integer in fancy clothing. It can't know if it's exhaustive even if the enum only contains two items as you can do something like this:
Language itself cant do that. But in most cases you can. If you know that nothing is gonna extend your enumeration (which is 99% of cases) you can just add something like _LASTentry to your enum and put a static assertion near the switch which fails when underlying value of that entry changes. This enforces programmer to edit that line and update the switch and this line edit highlights logic change on code review so reviewer can see all affected places.
Everyone suggests that this was a cheap way to avoid writing a reason phrase for Assert.Fail(), so I propose Assert.FuckOff(), which is simply Assert.Fail(“Fuck Off”);
Just looks like they want to throw if the default path is taken. They could have asserted a helpful message and the return does nothing, but other than that I don't see the issue with this.
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.
37
u/shizzy0 Mar 03 '23
Assert.Fail() but without needing to provide a reason?
37
u/halfanothersdozen Mar 03 '23
It's not the best way to handle a default, but it makes sense
16
u/This_Growth2898 Mar 03 '23
This can be normal, but probably there should be a method like Assert.Fail() for such situation. It marks the branch that should never be executed.
12
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Mar 03 '23
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
9
u/StrangeworldsUnited Mar 03 '23
If this was a unit test, that would be fine, but Assert.Fail() would be better.
16
u/Cybasura Mar 03 '23
How philosophical
Is being True...false?
Like can the truth actually be a lie?
3
u/BS_BlackScout Mar 03 '23
Could've sworn I saw something similar just the other day...
3
u/JumpyJustice Mar 03 '23
Dont know what language is on the screen but that is a usual practice to handle switches enumrations in C or C++. This is for the case when you want some error if someone adds a new entry to enumeration and it does not have a case in your function. But yes, even for C and C++ this is the laziest way since this check can be performed at compile time or even with compiler.
2
u/ichubbz483 Mar 03 '23
‘The Philosophical Code’ would be a pretty cool book
2
u/[deleted] Mar 03 '23
Perfect!
2
u/randfur Mar 03 '23
This is fine assuming there's no better way to indicate not reached.
2
u/Aperture_Executive2 Mar 21 '23
Everyone suggests that this was a cheap way to avoid writing a reason phrase for Assert.Fail(), so I propose
Assert.FuckOff(), which is simplyAssert.Fail(“Fuck Off”);1
u/ByteArrayInputStream Mar 03 '23
This is just a halfassed halt and catch fire
1
u/[deleted] Mar 03 '23
if (false?false:true) throw new Exception();
1
u/LiterallyACupcake Mar 03 '23
Gotta defend against cosmic rays somehow
1
u/NickSicilianu Mar 04 '23
What is that function names IsFalse and receives a Boolean parameter is the million dollar question here. Usually is or has should not take params?
1
u/trutheality Mar 04 '23
When you want to raise an exception, but you don't know how to raise an exception.
1
u/[deleted] Mar 04 '23
Just looks like they want to throw if the default path is taken. They could have asserted a helpful message and the return does nothing, but other than that I don't see the issue with this.