|
ExcessBLarg! posted:Of these, throwing an exception is my personal least favorite. I'm a believer that "exceptions should be used for exceptional situations" and a key-not-present condition is usually a frequent occurrence. The collection itself is not really in the best place to decide whether a key not being present is an 'exceptional situation' or a 'totally expected situation' because collections are very commonly used in both cases. And this isn't a problem with just collections, it's an issue with almost every sort of general library, and so the maxim of "exceptions should be used for exceptional situations" falls apart quickly in practice and as a result it shouldn't be the touchstone you strive to stick to. Instead, a better approach is that 1) your method names should be verbs or verb phases; and 2) the method should throw an exception if that verb was not successfully performed; and 3) the method should not throw an exception if the verb was successfully performed. This approach handles a method named "Get" on a collection. Did you get the requested value? No? Throw an exception. And the C# approach of having a non-throwing "TryGet" alternative. Did you try to get the requested value? Yes? No exception thrown. If you stick to this approach, it's very clear when a method should or shouldn't throw an exception without any ambiguity; and nobody has to make any judgement calls on what an 'exceptional situation' is. And reading code becomes a lot more straightforward because you see a set of actions and unless an exception bopped you out of normal flow control, you know all of those actions definitively will happen. biznatchio fucked around with this message at 22:44 on Dec 11, 2023 |
# ¿ Dec 11, 2023 22:41 |
|
|
# ¿ May 14, 2024 21:48 |
|
My submission for isEven, to take advantage of a service-oriented architecture:code:
|
# ¿ Mar 11, 2024 06:25 |
|
Perl is a devious creation by man, the pinnacle of our ingenuity and hubris, to create technical debt at a rate heretofore unseen and never to be outmatched.
|
# ¿ Apr 30, 2024 00:18 |
|
necrotic posted:Is Perl one of the languages with a runtime configurable array base index? Good news! There's a module in CPAN for that!
|
# ¿ Apr 30, 2024 00:28 |