Live data from Hacker News

Things I Was Wrong About: Types

v5.chriskrycho.com

241–250 of 468 posts

Re: Things I Was Wrong About: Types

#241

A lot of people in the comments are saying how they started off in Java, or C, and hated types, but eventually grew to love them. I started off in PHP. It was wild. Anything could be anything. Refactoring was a nightmare. Our codebase was littered with mystery variables like $hold, $hold1, and $holda, which were re-used all over the place. (Granted, this two other problems entirely orthogonal to types.) Then I got a…

Even though I learned C/C++ in school, I started off my career with a typeless language, Perl. I loved it for its simplicity and power to quickly spool up working code, but realized it was problematic to use for large projects for many of the same reasons you state. I then switched to a Java project and was immediately frustrated with types because of how verbose it was, but after a while I came to appreciate just ho…

In the Clojure world we use clj-kondo, maps, and spec to solve these problems

We get editor time feedback of mistakes with optional type hints + light inferance via clj-kondo and a data modelling system that we can export out as database schema, JSON schema etc

And dynamic enough constraint system to express something like all human names must be "Tony" only on Tuesdays

The same constraint that can be shared server side and client side without writing it twice without learning more syntax

Additionally check what the expected inputs for a function are by checking the function specs I think guardrails pro will make this more ergonomic when released

And finally ask the constraint system to generate valid examples of the constraints great for mocking data

I don't miss type systems but I also understand if you're not using solutions here then you're in trouble

Re: Things I Was Wrong About: Types

#242
Author here, with a few comments on things that I see coming up through the discussion—

1. This is not a post bashing dynamic types. When I write that I now value type systems, that doesn’t imply or entail thinking that dynamically-typed languages are bad, wrong, etc. I have a preference for (a certain variety of) static types these days, but some of the best and smartest engineers I know have a preference for dynamically-typed languages—and as my conclusion notes, I take that much more seriously than I did earlier in my career. I’m particularly interested in spending time with Erlang/Elixir and Clojure in the future, as both of them take very different approaches to robustness from the dynamically-typed languages I’ve worked with in the past.

2. Folks have asked about what specifically I found lacking in the type systems I encountered earlier (Fortran, C, Java). I alluded to this in the post, but I’ll elaborate a bit. For my part, I found that all of those—really, anything in that line without influences from e.g. Standard ML—required a great deal of the work of a type system without the degree of benefit I wanted from it. There has been some progress here in languages like Java, C++, and C with `var`/`let`/`auto` with inference, and more by adding in closures and the like. But even today, the gap in what I can express and have the compiler check for me between Java and even TypeScript (much less TS, Rust, Elm, Haskell, etc.) is large. That gap is where a ton of the value is, at least for me.

3. Other folks allude to coming from e.g. PHP to C and Java and finding even C and Java’s relatively limited type systems to be a blessing. I can see how that would be the case, especially given Java’s really great tooling for refactoring. I actually worked with C, C++, Fortran, PHP, JS, and Python all in parallel for the first four years of my career (and semi-regularly poked at Java; I had some early-2010s interest in Android dev that never went anywhere), so it was certainly not from lack of exposure that I didn’t find the tradeoffs all that valuable in the type systems of C, Fortran, Java, etc.

4. I strongly suspect that “cognitive style” (for lack of a better way of putting it) plays an enormous role in how one feels about and approaches types. The literature on types is not very conclusive or robust, and while it finds (e.g in studies of TS) that it does eliminate certain categories of bugs, advocates like me would do well to remember that the effects are relatively small and relatively limited even so. And as I alluded to in (1), I know a bunch of brilliant developers who recognize the value of types in principle—who have worked with Haskell or other similarly robust languages!—but don’t prefer them, and from discussions with them it’s very clear to me that we just think differently, and therefore approach building systems differently!

5. I think there are places where not using the best and most robust type system imaginable is negligent: a TLS implementation, for example. But in those areas, I also think that you’d better be doing an enormous degree of testing, and putting formal methods to use, and doing multiple audits, and basically throwing every possible solution at the problem. Same thing for aircraft or spaceships or medical hardware. Your average CRUD app… probably doesn’t need that level of effort, though. One of the problems in any discussions or debates about these kinds of things is failing to distinguish appropriately between the things which are necessary for different domains and indeed which may be better suited for those domains.

6. Finally, a meta-point: I find it entirely predictable, but a little bit sad, that this post blew up here on HN, when I tossed it off in under 15m… while a deep dive on a cutting-edge JS reactivity system I spent 3 weeks writing and revising got far less attention (and I’m sure that goes for many such careful write-ups). I get it: we all have opinions about things like types. But it’s still too bad!

Re: Things I Was Wrong About: Types

#243
post #226

I see a lot of comments on HN the last few years that just assume the dynamic vs static debate was settled at some time. It never ended. Static typing merely became trendy because dynamic typing was the trend for many years (Ruby, PHP, Python, Perl, JavaScript). Like all fashion, things that are trendy will once again become untrendy, and the cycle repeats. With that said, it's important to note that type inference i…

> Without the annotations, you can't look at code and know what is going on. In Rust, type inference is only inside the function, which I think gets you the best of both worlds. > In addition to that, you get the pain in the ass of having the compiler always complaining. This has stopped so many dumb errors of mine. My types aren't complex enough to guarantee that my program is correct , but they're complex enough to…

> This has stopped so many dumb errors of mine.

I take this one step further: Having a language with rich-ish types and good error messages like rust means I can very often rely on the compiler to tell me how to fix my dumb mistakes. In other words I know where I can be just as if not more sloppy than in an interpreted language and actually get away with it for little effort. I spend a little time getting the parameter and return types as I want them, quickly write an implementation without thinking too much about references and lifetimes then let the compiler work out the details pretty much automatically.

Re: Things I Was Wrong About: Types

#244
post #3

I followed a similar trajectory. Types were the bane of my early career. Hideous, extraneous. But really, they're the light at the end of the tunnel once you've worked your way though the dynamic / weak typing minefield. It took me a lot of Python, Javascript, and Ruby for me to get there, but now I'm way more comfortable on the other side. The correct type system is actually way more expressive than not having stron…

Same here! I've started with C++ and Java, learned to hate excessive typing, went through a long period of dynamic typing, and now I'm at the point you and the the author are. I still code a lot of Common Lisp on the side, but my Lisp code now looks entirely different than it looked just 3 years ago. The language standard does support optional typing declarations, and there's an implementation (SBCL) that makes use o…

You might be interested in Carp: https://github.com/carp-lang/Carp

Re: Things I Was Wrong About: Types

#245

I which there was a little more elaboration on _why_ he originally disliked types so much. This perspective is still very strange to me, as the value of types seems self-evident for systems more complex than a script, and I'd like to understand it better. As it stands now, my only assumption is that this comes from the place the author is coming from: people who don't think types are useful are generally coming from…

I can't speak for Chris, but I can speak for myself. I did the bulk of my early programming in statically typed languages. Specifically, late 90s C, C++, and Java. Then I found Perl, and pretty much went into dynamically typed languages only for the next near-decade. At the time, I felt like the types didn't pull their weight. There was a lot of extra writing, and you got very little benefit for it. Plus, dynamically…

Steve says he can’t speak for me, but in this case… he basically can. The timelines are a little different (late ’00s for me instead of late ’90s), but the trajectory was the same, and the feature gaps he alludes to here were definitely a part of it.

I recall arguing in 2012 or so that types were useless… with a colleague who had a background with OCaml. The problem was that when we said “types” I meant something completely different than he did. (Jerrad, if you happen to read this, you were right. )

Re: Things I Was Wrong About: Types

#246
post #199
post #187

Earlier quoted context omitted.

> Understanding other people's code is at least half the job of a programmer This was one of the pain-points when I was working more with node.js: the function signature told you nothing - like whether the function would even return or not would sometimes be a mystery. In very, very short scripts you can get away without types (like in a notebook for example), but once a project starts to get even medium size the tin…

This kills me about python. So many times I cannot figure out what exactly a functions expects and what it returns, sometimes even from reading the documentation! Matplotlib is especially bad.

Good grief, matplotlib and the whole scientific computing stack are abysmal. Every function takes dozens of arguments, but any given call only has to pass some subset, and depending on the subset and their runtime types, the function just tries to guess what the caller wanted to do. I get that this stuff was written by amateurs, but in a saner world this stuff would have been addressed by now. Anyway, I moved on from Python to Go and I’m super happy (!!performance!!, !!package management!!, editor tooling, top notch document generation, quality ecosystem, !!static binaries!!, and types—even if Go’s type system isn’t as robust as Rust’s, it still goes a long way).

Re: Things I Was Wrong About: Types

#247
post #145

I which there was a little more elaboration on _why_ he originally disliked types so much. This perspective is still very strange to me, as the value of types seems self-evident for systems more complex than a script, and I'd like to understand it better. As it stands now, my only assumption is that this comes from the place the author is coming from: people who don't think types are useful are generally coming from…

I originally worked with C++. I then moved to Python and loved it, partially because of the dynamic type system. I'm still in the dynamic-types camp, though I haven't really learned a language with a good static type system yet, and the little I've learned of Haskell has made me super interested in a good type system. To answer your question, the reason I prefer Python to C++, in terms of types at least, is that I fe…

In my experience, types (at least in the kinds of languages I like) and tests are orthogonal, though people enamored of either often try to use it to do the work the other is best at. I both TDD and TDD: that is, I do both type-driven and test-driven development, and the combo is incredible.

I strongly suspect that if and as I’m able to add tools like formal methods, logic programming, etc. to my tool belt they will similarly become orthogonal tools for correctness that I can employ where appropriate.

Re: Things I Was Wrong About: Types

#248

Earlier quoted context omitted.

Didn't Python 3.5 introduce optional typings?

Only annotations which can be checked by mypy or other linters. I think cpython doesn't check them at runtime.

And mypy is still very immature. You can’t denote a recursive type (e.g., a JSON type) or specify a callback that takes keyword arguments. Even getting it to load type annotations from third party packages is hard in many cases. Worse, it seems to be improving at a snail’s pace if at all.

Re: Things I Was Wrong About: Types

#249
post #219
post #159

A lot of people are making comments about how they can code "faster" without types. But for the majority of the code we write, inital speed isn't that important. Understanding the code and maintaining it are orders of magnitude more important for any non-trivial code. Types are not only a way for the compiler to understand your code and impose constraints. They're also your API to other programmers. When they see a s…

I agree with this for really large codebases, but I think you can get a surprising amount done before your program becomes "non-trivial" using a good dynamic language. For example, the website you are writing this comment on has been perfectly maintainable in lisp without any static typing for the past 15 years.

I get the feeling that lisp doesn’t produce as many runtime type errors as Python or JS and I’m not really sure why that would be. Maybe there’s something to a functional style of programming that improves quality even apart from type checking?

Re: Things I Was Wrong About: Types

#250

Static types are great for Fungeable Programmer at Big Co. where you can be dropped into a project and it sort of gives you a way to figure out what's present in the code. However, if you need to get somewhere fast, be it a side project where you've only got so much time due to other constraints in life, or it's a product you're trying to get to market first with a small team, then you're not going to benefit from os…

I think this is a false dichotomy. When I’m knocking out something as quickly as possible, I actually prefer statically typed languages. I have some quirks b/c of my particular background (my formative years were a constant mix of C and C++ and PHP and JS), so I often build even “script”-like tools in Rust instead of Python or Ruby or whatever. I find the ability to refactor rapidly is increased for me as I work in languages like Rust, ReasonML, etc.

But I also work with and know folks who are absolutely brilliant, who build large systems carefully and slowly… living almost 100% in the REPL of a dynamic programming language. This approach is not the one I prefer, and in fact when I’ve tried it it has kind of driven me batty. But this doesn’t seem to be a function of either competence or context whatsoever (though certainly types do help somewhat in the Working In Any Sufficiently Large Codebase scenario); it seems to be a function of cognitive style.

Post reply on HN