this post was submitted on 09 Feb 2024
89 points (100.0% liked)

Programming

16254 readers
61 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



founded 1 year ago
MODERATORS
top 7 comments
sorted by: hot top controversial new old
[–] Turun@feddit.de 22 points 5 months ago (1 children)

So the core concept is that when you validate some property about input you should also transform the input into a new form that represents the new guarantee in the type system.

This is very, closely related to the "make invalid states unrepresentable" concept. If we have validated our list to be non empty, we should return a non empty list - after all an empty list is now invalid and as such the type system should exclude that possibility.

[–] bugsmith@programming.dev 13 points 5 months ago

Yes! The concepts are intertwined. I think the key take away, for me, is to lean heavily into your type system and allow that to do some of the heavy lifting. Accept that something like a username is not a string, but a subtype of a string (this has to be true if any validation is required, otherwise you'd just accept any valid string).

[–] solrize@lemmy.world 14 points 5 months ago (1 children)

I remember this article and it's quite good, especially for Haskellers.

[–] bugsmith@programming.dev 13 points 5 months ago

It's one of my favourites. Something I revisit every couple of years.

[–] Ephera@lemmy.ml 4 points 5 months ago (1 children)

Hmm, I guess, this is why even a loosey goosey language like Python would have Pydantic...

[–] bugsmith@programming.dev 4 points 5 months ago

Honestly, for any large scale project in Python, Pydantic makes it bearable. We use Python heavily at work (and I'd argue we shouldn't be for the projects we're working on...), and Pydantic is the one library we're using that I wouldn't be without. Precisely because it allows us to inject some of these static typing concepts and keeps us honest, and our code understandable.

[–] ChubakPDP11@programming.dev 2 points 5 months ago

I subconsciously knew this, I currently am making a simple data exchange format to use with a program, and I am using PEG to create a parser. Chances of errors happning in this DXF is really low, but if the parser can't parse it, it's invalid.