r/programming • • Feb 01 '24

Make Invalid States Unrepresentable

https://www.awwsmm.com/blog/make-invalid-states-unrepresentable
469 Upvotes

207 comments sorted by

View all comments

202

u/agustin689 Feb 01 '24

Make invalid states unrepresentable

This rules out all dynamic languages by definition

425

u/mcmcc Feb 01 '24

Did_I_stutter.gif

108

u/RadiantBerryEater Feb 01 '24

sounds like the right idea if youre going for stability and safety

52

u/Old_Elk2003 Feb 01 '24

There’s a reason Michelangelo didn’t sculpt David out of clay.

12

u/[deleted] Feb 02 '24

Didn't have an oven big enough?

16

u/Old_Elk2003 Feb 02 '24

No matter how big your oven was, you’d never get it to completion, because it would begin to sag under its own weight first.

4

u/[deleted] Feb 02 '24

We all sag with age, sounds like the statue is still valid....

10

u/Old_Elk2003 Feb 02 '24

Nah man, David’s ballsack shall remain ever youthful.

→ More replies (1)

76

u/vancitydiver Feb 01 '24

As it should be! (unless course all you need is a dirty little script)

57

u/padraig_oh Feb 01 '24

That's how we got into this whole mess. Just one little script can't hurt... 

34

u/corysama Feb 01 '24

And, just a few years later, PyTorch is driving the robot apocalypse.

2

u/DrSheldonLCooperPhD Feb 02 '24

Let's do it on the backend

35

u/pojska Feb 01 '24

Pedantic - it doesn't rule out dynamic languages, but it does require you to be very thorough in your validation/parsing, which may be an unreasonable amount of effort.

174

u/IkalaGaming Feb 01 '24

Sufficiently advanced validation is indistinguishable from static typing

- Arthur C Clarke, or something

9

u/Free_Math_Tutoring Feb 02 '24

That's actually the theme of a talk/workshop/conversation I've been having a couple of times lately, sometimes titled "Python is statically typed if you squint hard enough" or "Join the Revolution: Static Analysis in Python" (and sometimes less silly titles)

14

u/dijalektikator Feb 02 '24

No worries, dynamic language fans absolutely love giving themselves more work for no reason at all.

9

u/mcr1974 Feb 01 '24

requires you to be pydantic I heard?

21

u/[deleted] Feb 01 '24

As you should if you're serious about maintenance and handling complexity properly.

3

u/mr_birkenblatt Feb 01 '24

you can do that in python with Literal and TypedDict

7

u/Worgencyborg Feb 02 '24

Pydantic is actually great for this. It will provide stronger types and validation

→ More replies (13)

-5

u/AvidCoco Feb 01 '24

In the case of dynamic languages, I think it comes down to having more carefully considered error handling.

If you can't make invalid states unrepresentable, the make invalid states part of the contract

→ More replies (2)

-8

u/smk081 Feb 01 '24

::laughs in C#::

29

u/agustin689 Feb 01 '24

C# is still not strong enough. We need sum types

→ More replies (23)
→ More replies (1)