Menu

Making illegal state unrepresentable
📰
0

Making illegal state unrepresentable

DEV Community: kotlin·Nicolas Fränkel·about 1 month ago
#vzfvfZwE
#dev#class#crust#code#self#article
Reading 0:00
15s threshold

A couple of years ago, I wrote that The Builder pattern is a finite state machine! . A state machine consists of states and transitions between them. As a developer, I want to make illegal states unrepresentable, i.e. , users of my API can't create non-existent transitions. My hypothesis is that only a static typing system allows this at compile-time. Dynamic typing systems rely on runtime validation. In this blog post, I will show that it holds true, with a caveat. If your model has many combinations, you also need generics and other niceties to avoid too much boilerplate code. My exploration will use Python, Java, Kotlin, Rust, and Gleam. With that background in mind, let's move on to the model itself. The model I will keep the theme of the aforementioned post: a pizza. Here's the full model: Every pizza starts with dough. Then, you need to choose your base, either regular tomato or cream. At this point, you can't add any ingredients to it.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More