Skip to main content
14 events
when toggle format what by license comment
Oct 11, 2023 at 22:16 comment added Flater Additionally, the question was about whether the last case should have a condition on it; not what the implemented body of the last case should be. Throwing exceptions in the last case is fine (I frequently use it too) but it doesn't help answer the question whether the last case (regardless of it being a "normal logic" or "throw exception" implementation) should carry a condition on it or not.
Oct 11, 2023 at 22:14 comment added Flater @BlackJack See above comment.
Oct 11, 2023 at 22:13 comment added Flater @Shadow You're missing the key point being made here. By the very definition of providing an example (to talk about something else) I needed to use one of the many possible ways to respond to cases that aren't covered. I never claimed that the approach my example takes is the only correct way. The comments here boil down to either "well it depends" which mirrors exactly what the first paragraph of my answer points out, or "well I would do [X]" which is just a projection of what use case that the comment author happened to be thinking of - i.e. the part where it depends.
Oct 11, 2023 at 17:55 vote accept Nikhil Kumar
Oct 11, 2023 at 14:16 comment added BlackJack @Flater Doing nothing maybe not correct behaviour either, so an exception is the better option because the error then doesn't slip by silently. Even if doing nothing would be the correct behaviour this should be said explicitly in the code.
Oct 11, 2023 at 2:31 comment added Flater @Shadow: How the code should behave when a new option is introduced is subjective/contextual and there's no one correct answer to cover all use cases. Exceptions are not necessarily the correct behavior and I intentionally stay away from that kind of blanket advice in this answer.
Oct 11, 2023 at 2:25 comment added Shadow I'd put an else: throw Exception('unhandled value') if doing an elif chain. It might seem redundant but, using your salary example, the code you provided will be logically flawed and potentially difficult to notice when it doesn't explode when accountants are added to the system.
Oct 10, 2023 at 23:08 history edited Flater CC BY-SA 4.0
added 393 characters in body
Oct 10, 2023 at 23:05 comment added Flater @Indigenuity: I'm no Python expert and wasn't aware there was a non-numerical value possible here. If this were a C# integer, OP's cases would completely cover the possible values and then the two presented options would be logically equivalent. But you're right about this because it's Python, so I'm adding an asterisk to this part of my answer.
Oct 10, 2023 at 18:51 comment added Indigenuity I love the points addressed here. But this misses the (IMO) biggest point by declaring "Logically, they're equivalent". You can't correctly select an if-style without recognizing the logical impact they have when your assumptions about values are (inevitably) incorrect. Oscar's answer illustrates this quite well. Combined, your answers cover it all wonderfully
Oct 10, 2023 at 17:40 vote accept Nikhil Kumar
Oct 11, 2023 at 17:55
Oct 9, 2023 at 22:01 history edited Flater CC BY-SA 4.0
added 1 character in body
Oct 9, 2023 at 8:21 history edited Flater CC BY-SA 4.0
deleted 1 character in body
Oct 9, 2023 at 3:49 history answered Flater CC BY-SA 4.0