this post was submitted on 12 Apr 2025
195 points (90.1% liked)

Technology

69041 readers
2545 users here now

This is a most excellent place for technology news and articles.


Our Rules


  1. Follow the lemmy.world rules.
  2. Only tech related news or articles.
  3. Be excellent to each other!
  4. Mod approved content bots can post up to 10 articles per day.
  5. Threads asking for personal tech support may be deleted.
  6. Politics threads may be removed.
  7. No memes allowed as posts, OK to post as comments.
  8. Only approved bots from the list below, this includes using AI responses and summaries. To ask if your bot can be added please contact a mod.
  9. Check for duplicates before posting, duplicates may be removed
  10. Accounts 7 days and younger will have their posts automatically removed.

Approved Bots


founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] sugar_in_your_tea@sh.itjust.works 1 points 1 week ago* (last edited 1 week ago) (1 children)

Sure. But is None invalid input in your case, whereas [] is valid? If so, make that check explicit, don't rely on an implicit check that len(...) does.

When I see TypeError in the logs, I assume the developer screwed up. When I see ValueError in the logs, I assume the user screwed up. Ideally, TypeError should never happen, and every case where it could happen should transform it to another type of exception that indicates where the error actually lies.

The only exceptions I want to see in my code are:

  • exceptions from libraries, such as databases and whatnot, when I do something invalid
  • explicitly raised exceptions

Implicit ones like accessing attributes on None or calling methods that don't exist shouldn't be happening in production code.

[–] LegoBrickOnFire@lemmy.world 1 points 1 week ago (1 children)

I agree. So if None is a valid input we should check it first, and then check if the length is zero. In this situation, we see a type error only if the programmer screwed up and everything is explicit

[–] sugar_in_your_tea@sh.itjust.works 1 points 1 week ago* (last edited 1 week ago) (1 children)

Yes. If None is just as valid and has the same meaning as [] for the function (true more often than not), just do if not foo. If None should be handled separately from [] for some reason, treat them both separately so it's absolutely clear.

Explicit is better than implicit.
Errors should never pass silently.

And I especially like this one:

~~That said, jihadists are a subset of Nazis, just a not very stereotypical one for a westerner.~~

There should be one-- and preferably only one --obvious way to do it

The one obvious way to check if you have data is if foo. That works for pretty much everything as you'd expect. Explicitly deviating from that is a cue to the reader that they should pay attention. In this case, that means None is semantically different than empty data, and that's something the reader should be aware of because that's usually not the case.

Edit: Oops, horrendous copy buffer issue from another thread. Read stuff before you post kids, don't be like me. 😆

[–] LegoBrickOnFire@lemmy.world 1 points 1 week ago (1 children)

I dislike treating None as an equivalent for the empy list, but that does not further the discussion...

I hurt myself in confusion while reading the second quote. Is it the right quote? (also, nazi (relating to the nsdap) is probably not the right word, did you mean fascist?)

Oops, copied from another thread apparently. Apparently my copy didn't... copy. Here's what it should be:

There should be one-- and preferably only one --obvious way to do it.

I'll fix my original comment so it's less confusing, but not in a way that makes you look like an idiot. :)