9 ms·
The Unreasonable Effectiveness of Dynamic Typing for Practical Programs [video]
- saosebastiao 9y agoThis is just rehashing the same tired and fallacious argument that most errors aren't type errors. And it is pure fallacy because different languages have different ideas as to what constitutes a type, and therefore have completely different ideas as to what a type error is. If I write `Object.new.foo` in Ruby, I get a NoMethodError. Not a type error, right? But that's the thing, that would be a type error in almost all statically typed languages. And it doesn't even come close to stopping there...there are hundreds of examples of errors that are not Type Errors in your dynamic language that are type errors in some other language. NullPointerException? That's a type error that is entirely prevented by many type systems. If you accidentally multiply your time and distance instead of divide to get your velocity, that would be a Type Error in F# using their units of measure types. If you accidentally pop an empty stack, that might be an EmptyStackException even in many statically typed languages, but would absolutely be a Type Error in a language with dependent types like ATS or Idris. In the end, dynamic typing is just a way to make runtimes slow and building correct programs slower, in exchange for some entirely subjective aesthetic or ergonomic quality. And sure, most type systems in practical languages aren't "complete" enough to eliminate the need for tests. But don't tell me that because you don't eliminate tests that you don't need types. That's bullshit. Statically enforced types eliminate errors, and every check that you defer to runtime is an error waiting to happen. If you're willing to make that tradeoff, by all means do so...but don't lie to me.
- deleted 9y ago[deleted]
- alkonaut 9y agoAlso: tests don't prove the absence of errors, only their presence. Static types prove the absence of some errors. Any type snob will tell you that even in supposedly good type systems, without dependent types, all lists are dynamic! Yet most of us that swear by static types don't use dependent types (much). There might be a limit where "more types" don't pay off their added cost relative to the thinking and typing needed and the security gained. Where that limit is depends on the application. Formally verified programs aren't used for many simple web backends - for good reason. Also, type systems aren't equal. There are decent dynamic ones and terrible ones. Same goes for static type systems.
- continuational 9y agoAs for dependent types, it's probably just that the type systems for them are simply not yet mature enough to be practical for most tasks.
- seanwilson 9y agoIf you're using a dependently typed language, you have to write maths proofs to prove properties hold. It's incredibly challenging and not going to be automated any time soon.
- agentultra 9y agoI think that's going to change. It's my completely biased, uninformed opinion that dependently typed systems are much easier to reason about than their counterparts.
- evincarofautumn 9y agoExactly. Almost any error can be a type error with a sufficiently expressive type system, because a type system is just a way to mechanically prove things about your code. A more expressive type system lets you prove more useful things. Of course, you can easily do things in a dynamic language that are observably correct, but which are very difficult to prove correct without advanced logic (i.e., an expressive type system). And more advanced type systems typically lose some nice ergonomic properties of simpler type systems, such as clear error messages, type inference, principal types, or decidability. So I can definitely see how someone moving for example from Java to Python would find the latter very liberating at first. On the other hand, you can argue that people shouldn’t be writing code that’s so difficult to prove things about in the first place. :) To my mind, the benefit of static types is that they allow proving properties of your code, but the cost is that they may force you to do so; the benefit of dynamic types is that they don’t force you, but the cost is that they don’t allow it. On balance I think static types are therefore preferable, because you can always fall back to dynamic types (strings/hashes/variants/sums) in a static language, but you can’t “fall forward” to static types in a dynamic language.
- kronos29296 9y agoWith things like typescript or mypy in python you fall the other way around with types in dynamic languages. You can do it if you really want (optional but recommended for large projects). So Python and typescript have a nice sweet spot between static and dynamic if you want.
- junke 9y ago> ...the cost is that they don’t allow it. Saying they don't allow it is too strong (they allow it, but it might be harder).
- evincarofautumn 9y agoYou can’t prove even very simple universal properties of dynamically typed code, such as “when given an integer, this function always returns an integer”, because its result type can depend on the value of the integer passed in at runtime. You can only prove existential properties (“there are integers for which this function returns an integer”) through testing. That testing can be done before runtime in some languages, e.g., with Perl’s BEGIN/UNITCHECK/CHECK/INIT blocks. But it’s not really the same. In a referentially transparent, statically typed language, if you give me a function of type ∀a. a → a, then I know for a fact that if this function halts, it must be the identity function. The only thing it can do is return its input. The same function in a dynamically typed language could return any value of any type, because its result’s type can depend on both the type and the runtime value of its input, or not depend on its input at all, even if it’s referentially transparent. Even if you supply that type annotation and check it at runtime (“this function returns a value of the same type as its input”), it would only be checking a single code path, and it could still give me any value. That is, a valid implementation would be “return 3”—I wouldn’t get a type error if I only ever called it on integers, but it would still be non-parametric.
- junke 9y agoYes, everything can be a type error (security, memory management, concurrency (deadlocks), ...). You define properties about the code and you can check them either at runtime, or statically. If I choose to write in OCaml, the memory management is done with a GC, which relies on dynamically typed memory; but I write in Rust, there is no GC but a kind of static analysis for managing ownership; both approaches work. Your fallacy is saying: there is a whole class of errors that can avoided if only we use a different type system, so there is no point in using that type system which defers (some or all) checks at runtime. I can write code that manipulates values which represent types in the language, at runtime. If the types happen to be known or declared during compilation, fine enough, they can be optimized away. Otherwise, they will be checked later (which transparently invokes dynamic dispatch if needed), and I can even use them once they are known to compile the code myself, on a closure; there, along with other values which are then known, they can be taken into account to perform more aggressive optimizations. Why don't all language provide this? As for bugs or errors, if you cannot lower risks (with static guarantees) then you have to improve on fault tolerance. You take other approaches to ensure the system behaves well, even if some parts can fail (e.g. Erlang).
- seanwilson 9y ago> In the end, dynamic typing is just a way to make runtimes slow and building correct programs slower, in exchange for some entirely subjective aesthetic or ergonomic quality. To be honest, I really don't understand what the benefit of dynamic types are supposed to be. People say it's easier and less hassle to get something up and running because you don't need type annotations but this isn't an issue with decent type inference and all you're doing is replacing compile time checks with runtime crashes.
- carlmr 9y agoI still feel there's a correlation between Python coding bootcamp survivors and the new wave of people thinking static typing is hard. It makes it easier if you know what you're doing.
- flavio81 9y agoThere are also people with 10+ years of using static typing languages like Java or C++, that then go to Python or Lisp or Racket and find out they are more productive with dynamic typing, while writing even more readable code than before.
- abetusk 9y agoI don't claim to be very knowledgeable about this but it seems like you're bringing up a valid point. In the presenters analysis, he looks for very specific types of bugs when making the claim that static typing only addresses 1-5% of bugs. Your claim is, essentially, his type of analysis undercounts the number of bugs a non statically typed language by creating too heavy handed of filter. Do you have any data to back this up or have links to papers that try and do a better job of counting issues that statically typed languages catch?
- hyperpallium 9y agoInsightful +1. But by "types", they really mean the so-called practical languages' types, not the academic definition. Types allow you to prove something about your program - but it takes extra upfront effort to prove something. The trade-off between ease and proof-power has settled on what we have... IDK of course, but I don't think it will change much. And worse, the popularity of static typing is mainly due to performance, followed by documentation, and correctness of proof not really in it. But maybe this story's empirical github evidence on errors will shift it a little. When you're re-coding all the time, the upfront cost isn't amortized over future runs as much as it used to be. Libraries are, but the massive real-world crowd-testing (i.e. usage) catches them... BTW I really like units of measure types, you get dimensional analysis almost for free. You can do it in e.g. java but it's verbose (i.e. it's java). Classes, with arithmetic methods (add, sub, mult etc), You need an ezplicit class for each unit combination (though you can do a little better with generics). And get compile-time type errors when you assign, compare or operate incorrectly.
- flavio81 9y ago> This is just rehashing the same tired and fallacious argument that most errors aren't type errors. And it is pure fallacy because different languages have different ideas as to what constitutes a type, Ok, so you are writing an algorithm for a fast approximation of ATANH using IEEE floating point standard. In type: IEEE floating point number Out type: the same. Tell me how the magical type checking of the excellent programming language you propose will ensure that my calculations are correct.
- falcolas 9y agoAttempting to argue about the value of dynamic typing right now is like trying to win the vim/emacs (or is that now just Sublime Text/Atom) editor wars. It's more a recitation of dogma than useful conversations; you can't discuss any type system's pitfalls without "oh, that's just algebraic typing, not static typing". Fear not, my fellow Ruby/Python/Lisp/Perlers - the pendulum will swing back, and all that will be discussed is how much of a pain the current static typing systems are and how type hinting is the real future.
- abetusk 9y agoI get the feeling that you didn't watch the talk. He specifically talks about the field making large decisions based mostly on dogma and that evidence should be used to make those decisions instead. He's trying to advocate the use of the scientific method as applied to language design choices. He admits that the papers he cites are too few to make large decisions and that the analysis he does by looking at how many issues on GitHub are actually caused by type system errors (1-5%) but he's trying to steer away from dogmatism in favor of empirical evidence.
- galaxyLogic 9y agoI think one needs to be clear about the difference between "typing" and "type-checking". Typing means that objects can be instances of one or more type. Type-checking means that we can annotate the code so that it contains assertions about the types of its objects. Static type declarations can help program understanding a lot without no effect on performance. But on the other dynamic assertions of properties of objects can be more flexible, you can program your own type-checking algorithms and define your own types and what it means to be a member of them.
- erokar 9y agoLike Smallshire mentions in the talk I think the way forward is gradual typing, like you have in e.g. TypeScript. Best of both worlds.
- KillSticky 9y agoThe presenter focuses on the cost of building software in the first place. Yet, in practice, the cost of maintenance often dwarfs the cost of initial development. This is where statically typed languages hold major advantage of dynamically type ones. In part, thanks to the ease of refactoring with modern IDEs. The presented studies can hardly be used as a basis for making his arguments. The study where a student invests a new language that nobody else could possibly know is especially bad. Of course the learning curve is going to be longer for an invented statically typed language! You cannot assess productivity without factoring out the initial learning time. If anything, that study only shows that dynamically typed languages are easier to get into.
- flavio81 9y ago> Yet, in practice, the cost of maintenance often dwarfs the cost of initial development. However, funding the development is sometimes harder than funding the maintenance: You need the system to operate in the first place in order to get the benefits/money/revenue/etc. Once the system is in place and brings benefits, it is easier to fund further investment on the system, be it maintenance, extension,etc.
- KillSticky 9y agoThe initial development of a chunk of code is not the same as the initial development of a shippable product. Code maintenance starts the moment you need to go back and add anything nontrivial to the existing code. And you will have to go back and change your code many times while using your initial funding. So the product funding argument, while true, does not apply here. Try reading your own code written two weeks ago. How about a month ago? Three month ago? Let's say your code is making some calls and gets back an object. Can you list all the important methods on that object from the top of your head? With a statically typed language IDE can always do that for you. If your team member added some more methods, they will be listed too. No need to waste time reading the function source to figure out the return type, and then waste more reading the class's source.
- 9y ago
- michaelfeathers 9y agoAt the same time that this is showing on HN there is an article about an ESPN Analyst walking away after being upset at trauma on a football field [1]. The comments are very interesting in that they point out that football helmets may actually exacerbate injury because they their protection leads to players feeling protected enough to risk harder hits. I don't think we have that same effect in software but I think there is an related one: when you don't have the protection of a static type system, you may become more careful. [1] https://news.ycombinator.com/item?id=15141495 https://news.ycombinator.com/item?id=15141495
- 21 9y agoI've heard many times something along the line "I can just start hacking on a big refactoring because the compiler will tell me all the places that need fixing".
- kcorbitt 9y agoWell, that doesn't mean it's wrong. I'm far less likely to introduce bugs when "just hacking" on a Flow-typed JS project than I am when carefully (and tediously) trying to refactor an untyped one.
- seanwilson 9y agoAgree with this. I recently refactored a TypeScript code base for around 3 hours (renaming things, moving functionality into new classes, reducing duplication) without running the code once and it nearly worked first time. Simply renaming a variable in plain JavaScript can be a challenging refactor in comparison.
- Silhouette 9y agoI don't think we have that same effect in software but I think there is an related one: when you don't have the protection of a static type system, you may become more careful. It's an interesting theory. On the other hand, the amount of time and money lost to null pointer bugs over the years suggests that we still aren't careful enough.
- flavio81 9y agoWhen static typing vs dynamic typing is discussed, i see that often a mistake is made and "static typing" is confused with "strong" typing, and "dynamic typing" is confused with "weak typing". Just FYI: If one wants his/her "programming language" to catch typing errors/violations/mismatches, one needs to use a language with strong typing, regardless of if the language itself supports static or dynamic typing. Dynamic typing relies on being able to infer the types; this can be often achieved at compile time but in any case at runtime, if the platform is strongly typed, any type error will be caught. Then, after being caught, what could be done about it depends on your platform -- some platforms will die with an error; some others will die but allow you to inspect what happened; then others allow you to patch (correct) the error and continue. Static typing requests you to explicitly declare types. This helps the compiler do as much checking as possible at compile-time. This has the added side benefit to increase the speed of the compile code, since less checking is needed at runtime. Some systems are dynamically typed but weakly-typed. Examples are PHP and ES/Javascript. Some systems are statically typed but fairly weakly-typed. The Kerninghan&Ritchie C language is an example. Some systems are dynamically typed and strongly typed. Python is one of them, although it also has what is so-called "duck" typing. Some systems support dynamic and (optional) static typing, and are strongly typed. Common Lisp is an example, as well as in Julia. In both cases (CL and Julia), static typing helps for increasing performance, and for achieving multiple dispatch (i.e. for Object-Oriented programming). CL is able to do compile-time checking as well, if the implementation supports it. Some systems have fairly complete strong typing which relies on extensive static typing facilities. Haskell is an example.
- kazinator 9y ago> often a mistake is made and "static typing" is confused with "strong" typing, and "dynamic typing" is confused with "weak typing". Do you identify that problem in this specific video? "Strong typing" is itself loaded. There is a definition of "strong typing" which means "no conversions occur between types without programmer requests". For instance, under this notion, a language in which we cannot use an integer expression in a floating-point context without putting in an integer-to-float coercion is more strongly typed than one in which the conversion is inferred and takes place implicitly. "Strong typing" has been used in reference to the facility, as in the Pascal family, for declaring type alias names for identical types (such as integers) such that the aliases are considered distinct types. The C typedef mechanism is weakly typed because a mode_t and pid_t, both perhaps just aliases for int, are considered to be indistiguishable from int and compatible. C++ has more strongly typed enum types than C, because a location of some enum type in C++ cannot be assigned a value of an integer type, or of a different enum type, without a cast. Sometimes "strong typing" means that there are no holes in the type system: no datum is being misused if there are no diagnostics about type. (This is related to the other senses of "strong typing" because if an implicit conversion is allowed in a program which is free of type diagnostics, that conversion could be unsafe and hide a software defect. The type system knows very well that here there is an expression of real type and the context requires a integer value and silently generates the conversion; but what if it is out of range? In, C, the behavior of an out of range floating to integer conversion, and vice versa, is undefined.) > Static typing requests you to explicitly declare types. Not in anything that can be called state-of-the-art static typing, except at module boundaries (where the compiler cannot always infer parameter or return types since some of the information is in another module that it doesn't know about). Types can be inferred implicitly from basic terms in the program syntax tree whose type information is known (like literal operands, standard functions and operators and such) and then from there by inference (which can also go in the other direction, like from known function parameters to unknown terms nested inside the function). https://en.wikipedia.org/wiki/Hindley%E2%80%93Milner_type_system https://en.wikipedia.org/wiki/Hindley%E2%80%93Milner_type_sy...
- cwyers 9y agoIf one doesn't have an hour to watch this (or wants to know if spending an hour watching this is worthwhile), is there a summary somewhere of the points this covers?
- FridgeSeal 9y agohttps://news.ycombinator.com/item?id=15144544 https://news.ycombinator.com/item?id=15144544
- s17n 9y agoThis video is not worth 50 minutes. tl;dr: A bunch of stuff about typing that you probably already know and for which there are better resources. He cites a couple studies showing that static typing doesn't improve productivity. His own analysis of python projects on Github shows that only 3% of bugs are caused by type errors. He acknowledges that the evidence is not good and thinks that the industry should invest in doing better studies.
- ivan_ah 9y agoI agree with the speaker that adding basic static types (Java/C/C++) is lot of work for not a lot of benefit. However advanced type systems like the one in Elm, now that's something else. Especially when refactoring and almost-writes-it-for-quality error messages from the compiler.
- preordained 9y agoThe thing I find interesting is that in many cases the types are "there", dynamic or not, except in most statically typed languages you have to type them out for the compiler. If explicit types everywhere arent an awesome communication tool, then shouldn't we be able to do away with them and keep the benefits? Or 85%? 90? Why not an uncluttered lisp or Python syntax and tools that don't require us to spell out the types by keyboard?