logoalt Hacker News

matltc • today at 4:15 PM • 0 replies • view on HN

Ruby and python were my introductions to programming. I picked up C and found the type system to just be annoying. Then I picked up a TS Node project that used Zod and it changed my perspective.

One of the primary structures that the project concerned itself with was written as a big fat z.object with all optional keys. In the logic, there were all kinds of runtime checks as nested if/else ifs looking for the presence of the keys and branching on their value if they existed. I discovered z.discriminatedUnions and realized that this could be checked once at compile time by defining the possible states of this structure, which progressed monotonically from one state to the next, never backwards

The logic then became a single comprehensive switch statement with an assert never default. Vastly simplified the rest of the application and obviated the nullish guards that were interspersed throughout the front- and backend logic. Life changing.

Now, I only use dynamic languages for trivial scripting or where the type system would be unneeded overhead. I am finding this to be rarer as my ambitions grow.

Also, the benefits of a good LSP provided by strongly typed languages with a robust ecosystem are huge. Makes reasoning about code so much simpler for me when I can peek any object's type in my IDE and go to definition. Ruby LSP is awful at this, you can't rename a variable globally nor can you go to a function's implementation if it cannot be determined. Not so with out-of-the-box tooling in strongly typed languages.