this post was submitted on 12 Apr 2025
196 points (90.2% liked)

Technology

69154 readers
3111 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

it's still implicit

I don't see it that way. If you're doing if len(foo) == 0, you're implying that foo is expected to not be None, and expecting an exception should not be the default assumption, because exceptions should be... exceptional.

Here's what I assume:

  • if foo is not None - empty values are explicitly acceptable
  • if not foo - the difference between an empty and None value isn't important
  • if len(foo) == 0 - implicit assumption that foo is not None (I frequently forget that len(...) raises on None)

If an exception was intended by the last bullet point, I prefer an explicit raise:

if foo is None:
    raise ValueError("foo may not be None")

I actually use schema validation to enforce this at the edge so the rest of my code can make reasonable assumptions, and I'm explicit about whether each field may or may not be None.