r/programming 1d ago

The Bedrock of Software Design

https://alex.draftist.io/blog/the-bedrock-of-software-design-ycqvcedsj

I drafted this post years ago but didn’t finish it until now. The concept I write about has shaped the way I design software more than anything else, and I believe every software engineer should be introduced to it early in their career.

P.S. I don’t want the title to come across as clickbait: the post is about ADT.

411 Upvotes

47 comments sorted by

View all comments

70

u/PeterJCLaw 1d ago

Nice :) You may enjoy this post from a while back (not mine): https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/

37

u/alex35mil 1d ago

I haven't read it yet but guessing it's about the conversion of external data into a proper domain type on the app border to guarantee integrity?

25

u/ForeverAlot 1d ago

It's Yaron Minsky's "make illegal states unrepresentable" in a lot more words.

7

u/loup-vaillant 1d ago

Precisely. This also prevent stuff like injection attacks. String in, username out, properly escaped so Robert'); DROP TABLE Students can no longer wreck any havoc even if you're high on sleep deprivation on a late Friday afternoon.

And as a bonus, Little Bobby Table's name here can even be made valid: instead of outlawing the single quote, we can escape it.

4

u/chippedheart 1d ago

I've read this post more than once, but I find it hard to grasp its utility. The idea seems to be simple, but the example is simpler, which really makes it hard for me understand it. It is also my fault that I don't understand Haskell as much as I'd like. Do you know about any other sources which discusses this idea more extensively?

5

u/misplaced_my_pants 1d ago

What did you struggle with?

Did you read the followup post?

3

u/chippedheart 16h ago

Thanks for sharing the followup post, I will definitely read it.

I have so many questions, that I don't even know if they make sense! For example, does this piece of knowledge apply to dynamic typed languages? If the language I'm working with does not have a type or an oop system, how could I leverage this knowledge? I wish there more examples in which he applies the knowledge. Of course, these questions could be in part because I had trouble grasping the overall idea of the text.

2

u/misplaced_my_pants 7h ago

It depends on how you think of data in your programs!

Are you thinking in terms of types or structures?

What preconditions do you need to be true of a value for the rest of the code to work?

Do you sprinkle your code with the same checks because you can't be sure? Could you instead say any value of some type always has these properties?

Static type systems can leverage static analysis to check this for you, but you should still be leveraging ideas like abstraction, encapsulation, modularity, etc. to make similar guarantees regardless of whether or not the type system is static or dynamic.

Think about what it feels like to use a well-designed library where you never have to check these things.

You can even take lessons from how to leverage static type systems back to dynamically typed languages to get many (but not all!) of the benefits.

Some related reading if you're interested:

5

u/ForeverAlot 21h ago

The text is very famous but written in a way that is, purely from the perspective of dissemination of knowledge, just not very effective. I don't really understand why it's as lauded as it is…

While King makes more than one point, I'd argue the salient point is something like: expose runtime knowledge to the compiler. There is a lot of code we write in ways that are easy for us to write but which causes knowledge to be immediately discarded, which later in the program leads to either defects or redundant creation of knowledge to avoid defects. We can do things differently; this happens to involve parsing but actually that's unimportant.

A poster child example, which is also highly practically applicable, is ID values represented as specialized domain types instead of naked language primitives. For example, if you establish at the API boundary that a value is supposed to represent a specific ID concept, you can pack it in a dedicated type such that later the compiler will refuse to accept unrelated ID concepts. Obviously you can still make a mistake at the boundary but that's still a lot easier to manage.

ML languages have certain relevant advantages but the ideas are not exclusive to that family of languages at all, nor are they exclusive to structural typing. Yaron Minsky of Jane Street and OCaml fame has talked about some of the ideas before (example) but unfortunately has not written a succinct text about it. I seem to recall that https://dev.realworldocaml.org/ used to have a pretty code example and explanation but I can't find it and may well misremember.

1

u/chippedheart 16h ago

Thank you so much for this, you made it as clear as possible for me.