4 ms·
Yes, this is true, the dynamic language communities have compensated for the lack of a type system with explicit type checks (or type constraints) and lots of t
by jrockway 16y ago
Yes, this is true, the dynamic language communities have compensated for the lack of a type system with explicit type checks (or type constraints) and lots of testing. But it's still nice to have a real type system (and I don't mean C or C++ or Java; those languages also have no type system). When I compile a Haskell project successfully, I have a reasonable belief that it's going to run to completion and at least produce some sort of result. The same is not true of Perl, even with lots of tests, my programs almost always die in the middle (during development) where a runtime type check or assertion fails.
The reality of programming is that all functions and variables have types. If you add 42 to "hello world", you get garbage. In dynamically typed languages, you keep the types in your mind -- you know that the variable you are adding 42 is a number. In statically typed languages, you tell the compiler. The second way lets the computer do some of the work for you (ensuring that if you use the variable like a string, your program won't compile), the first way requires you to do everything. If you aren't careful, you get the wrong answer or a runtime error.
I think dynamic typing was a nice way for the programming language community to realize that C's type system sucked, but it isn't the ultimate answer, it's just a stopgap. It's also good that people realized they needed to test their code automatically; but that works even better with statically-typed languages.
Anyway, I am not going to give up Perl anytime soon, but I secretly wish it wasn't dynamically-typed.
- jcsiracusa 16y agoIs running to completion and producing an incorrect result really preferable to dying in the middle of execution due to a runtime check or assertion? Either your program works or it doesn't.
- jrockway 16y agoYes.
- gnaritas 16y agoNo. Failure is failure.
- paulbaumgart 16y agoWith dynamically typed languages and implicit variable declarations, I find the most common runtime errors are, in descending order of frequency: * Something returned null when I assumed it would return a value/object. * I used the wrong variable. * I made a typo in an assignment that implicitly created an unused variable (this is easy to catch with static analysis even in dynamically typed languages, admittedly). The first one is by far the most common. I'm not particularly experienced with Haskell, but my understanding is that the static typing will usually catch the second one and that more advanced static analysis (more advanced than type checking) can easily catch the third one. Most important, though, is the case of the first and most common error. Here, the language will very explicitly require you to handle the case that "Nothing" will potentially be returned. So, most of the sorts of errors that would have caused failed execution in a dynamically typed language will be caught at compile time. This means that when the program runs, it will usually be closer to being correct than it would be if it were written in a dynamically typed language and failed in the middle of execution.
- sid0 16y agoI agree. One of the biggest benefits of a proper type system (and by saying "proper" I immediately exclude C and Java, so don't even bother mentioning them) is that you get clean semantics for both nullable and non-nullable types. Hoare called null a billion-dollar mistake, and I completely agree.
- jrockway 16y agoand by saying "proper" I immediately exclude C and Java, so don't even bother mentioning them If there was a "jrockway award for explaining type systems", this statement would qualify you for one. I am glad I'm not the only person that thinks this :)
- gnaritas 16y ago
- digitallogic 16y agoDepends on the context of the application. Social networking web sites? Yes. Video games? Definitely. Business applications (or anything dealing w/money)? You sure had better fail and roll back any transactions. But I think your point stands when you consider that the cases where it's ok to keep going after something wrong happens are the exception, and not the rule (most software is automating boring business practices).
- digitallogic 16y ago"In dynamically typed languages, you keep the types in your mind" That's one way, or you could write clear unambiguous code such that the types are reasonably self evident.
- jrockway 16y agoIn that case, why not let the computer tell you whether or not the types are reasonably self-evident? That's what static typing is. (Remember, don't mash up "Java and C++" with "static typing". Java and C++ are not worth mentioning when discussing type systems; they have type systems that get in the way and make it harder to code.)
- edanm 16y agoI've only every programmed in Java/C++, and dynamic languages, so I don't know what you mean. Do you know of any good resource to read up about a better type system and its benefits, but without having to learn a whole new language? (I don't have time to learn a new language, but this sounds important enough to read up on).
- Periodic 16y agoA lot of the fun-to-program statically typed languages use type inference to determine types automatically. In these languages, as long as you make your types reasonably clear the compiler can go through and do type checking for you without you ever having to explicitly type a single expression. http://en.wikipedia.org/wiki/Type_inference http://en.wikipedia.org/wiki/Type_inference
- edanm 16y agoThe big problem with statically typed languages isn't having to say "int" constantly. It's having to decide on a type hierarchy. What I mean is, when I write a function to do something with files, I don't want to have to decide whether I'm going to accept a "FILE" class or not. This isn't laziness: it's realizing that I will one day want to use a different class which looks like a file, but isn't part of the "file" hierarchy. This situation comes up far more often than one might think, and it saves a lot of headaches up-front (designing complicated hierarchies to prevent future use-cases you'll want). From what I've read, Type inference doesn't help with that. (But I haven't read much, so I could be wrong).
- chromatic 16y agoI think dynamic typing was a nice way for the programming language community to realize that C's type system sucked.... Dynamic typing is an answer to the problem that getting types right in a program is as difficult as getting anything else right in a program, especially given the limitations of type systems. I can write a trivial Haskell program which will not halt (due to trivial mathematical properties of negative numbers) but which the type checker will allow by allowing the compiler to infer the wrong type.