Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
biznatchio
Mar 31, 2001


Buglord

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

Adbot
ADBOT LOVES YOU

biznatchio
Mar 31, 2001


Buglord
My submission for isEven, to take advantage of a service-oriented architecture:

code:
async function isEvenAsync(value: number, signal: AbortSignal): Promise<boolean> {
    const resp = await fetch(`https://numerics-service.local/api/getProperties?num=${value}`, { signal });
    const jsonObj = await resp.json();
    let result = false;
    for (let i = 0; i < jsonObj.factors.length; i++) {
        if (jsonObj.factors[i] == 2) {
            result = true;
            break;
        }
    }
    return result;
}

biznatchio
Mar 31, 2001


Buglord
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.

biznatchio
Mar 31, 2001


Buglord

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!

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply