Slashdot Mirror


Guido van Rossum On Strong vs. Weak Typing

Bill Venners writes "In this interview, Java creator James Gosling says, 'There's a folk theorem out there that systems with very loose typing are very easy to build prototypes with. That may be true. But the leap from a prototype built that way to a real industrial strength system is pretty vast.' In this interview, Python creator Guido van Rossum responds with 'That attitude sounds like the classic thing I've always heard from strong-typing proponents. The one thing that troubles me is that all the focus is on the strong typing, as if once your program is type correct, it has no bugs left. Strong typing catches many bugs, but it also makes you focus too much on getting the types right and not enough on getting the rest of the program correct.'"

18 of 100 comments (clear)

  1. Guns don't kill people by KDan · · Score: 4, Insightful

    "Strong or Weak Typing doesn't make good programs. Good programmers make good programs."

    Daniel

    --
    Carpe Diem
  2. Strong Typing is a Must by the+eric+conspiracy · · Score: 4, Insightful

    I think that this argument is exactly backwards. Strong typing allows mechanical type checking via compilers and incremental type checkers during the development process. Strong typing takes a lot of the load off a human developer, and puts it on mechanical systems that can be made much more reliable than humans. The important thing is to take maximum advantage of strong typing - use an IDE that does incremental compilation, etc.

    On the other hand, since it is not possible to mechanically check weak typed code, weak typing places much more load on the programmer to make sure the types are correct than a strongly typed system. The result is many more bugs, and bugs caught much later in the development process when they are more expensive to correct.

    1. Re:Strong Typing is a Must by Jerf · · Score: 5, Insightful

      Sorry, but that's spoken like someone parroting the party line, not someone with experience in both.

      Strong typing allows mechanical type checking via compilers and incremental type checkers during the development process.

      OK, but A: it only allows type checking and B: it enforces type checking.

      A: Type consistency is not a guarentee of correctness. It's not even close to a guarentee of correctness. It's not even more then loosely related to guarentee of correctness. Type 'errors' are only one class of error, and they are detectable in both paradigms, just at different times. Don't skip the focus on "unit-test based testing"; the point of unit-based testing is that it tests for any kind of error you can program a test case for, including type errors, wrong-output for input, environment interaction, and all the other kinds of non-type errors that happen in real life. Unit-test based programming says why pay so much for such a weak guarentee of correctness when you can spend that time writing unit tests and test for useful properties?

      Note that XP calls for unit testing in all languages, not just dynamic ones. You need to be writing them anyhow; why not lean on them and keep your program flexible and tested correct, rather then "proven type correct"?

      B: It enforces type consistency. How many times have you wanted to jump out of the type cage? When programming in a dynamically (not really 'weakly') typed language, it encourages an "interface based" style. If you implement an object that has all the necessary methods, and it'll work right, why shouldn't you be able to use it just because it's not the right "type"? Many, many things in Python present a file-like interface, or a string-like interface, or a sequence-like interface, without having to inherit from the official implementations which may be very wrong for their purposes. And many other parts of the language do that too.

      And guess what? Even in large programs, the world does not come to an end. See unit-testing, although even without that it's still usually OK.

      Because the interface style pervades the whole language, you get to easily write programs that can handle certain types of "type" violations without blowing themselves to smithereens like you might expect if you've never tried it before.

      On the other hand, since it is not possible to mechanically check weak typed code, weak typing places much more load on the programmer to make sure the types are correct than a strongly typed system.

      Two more things:

      One, my experience and the experience of a lot of others says the opposite. You spend an unbelievable amount of time jumping through compiler hoops in a static typed language, and sometimes the smallest change cascades down your entire static hierarchy. You spend a lot of time worrying about things that really aren't importent in the grand scheme of things. Half the time if you're trying to write with any sort of flexible pattern like Iterator or something you end up with a superclass called "Iterable" or "Object" (sound familiar?) or some equivalent, which isn't that far from dynamic typing anyhow.

      The other thing is again, "mechanically checking" code isn't possible for anything but type-correctness. No other goodness properties can be checked that way; in fact Rice's theorem proves no non-trivial property of programs can be proven mechanically. (This implies type correctness is not an interesting property ;-) )

      If type safety could give me something above and beyond type safety, I might be interested. But all it does is "prevent" a class of problems that don't exactly plague most coders anyhow. It's an incredibly steep price to pay for something that doesn't do much for you.

      I've experienced both sides of this. Sure, in theory type checking is great, but in practice, the dangers of type errors are incredibly exaggerated. It's just not a problem. It's too late to convince me with theory that has completely failed to play out the way the the type-safety advocates said it would.

    2. Re:Strong Typing is a Must by MaggieL · · Score: 2, Insightful
      Type consistency is not a guarentee of correctness. It's not even close to a guarentee of correctness.
      Type consistancy may not be a guarantee of correctness, but type inconsistancy is a guarantee of trouble. :-)
      --
      -=Maggie Leber=-
    3. Re:Strong Typing is a Must by jbester1 · · Score: 3, Insightful

      Ever tried an ML derivative?
      They are statically typed, yet typed-inferred so you save quite a lot of time and any change cascades itself down. Saves a whole lot of time, especially nice since the type system is flexible enough to do symbolic computing.

    4. Re:Strong Typing is a Must by Phouk · · Score: 1, Insightful

      [...] weak typing places much more load on the programmer [...] The result is many more bugs

      Let me guess: This is what your professor told you?

      Try it out sometime, you might find that neither is true in practice.

      --
      Stupidity is mis-underestimated.
    5. Re:Strong Typing is a Must by shaper · · Score: 3, Insightful

      Unit-test based programming says why pay so much for such a weak guarentee of correctness when you can spend that time writing unit tests and test for useful properties?

      Maybe it's just me, but I've found that much (most?) non-trivial systems that I write are not easily unit-testable. They are usually part of or dependent on large libraries and infrastructure. Most of that infrastructure I did not write nor do I have access to its source. Much of it is often poorly documented, so that one does not have any clear spec upon which to build tests.

      Instead, I tend to rely on the tools to do as much as possible for me, type checking being just one tool in one phase. I expect and plan for unexpected behavior and design for graceful error handling and degradation at every level of code. I know that the developer that wrote that library is at least as lazy as me, probably even more, so I cannot depend on documentation and correct behavior and I definitely do not have the time to write test cases for every interface in every external library that I use.

      Maybe its my early background in military systems. I just got used to disciplined development and I don't really have to think too much about type correctness. I just tend to be careful about it out of habit.

      And finally there is the one iron-clad fundamental rule of software development: a defect found earlier is much easier to fix. I prefer to catch as many errors as I can by compile time. Everything after that starts getting much more expensive really fast.

    6. Re: Strong Typing is a Must by Black+Parrot · · Score: 2, Insightful


      > Sorry, but that's spoken like someone parroting the party line, not someone with experience in both.

      OK, I have experience in Ada, BASIC, C, and C++, and I spend far less time debugging stupid errors in the strongly typed language: Ada.

      > One, my experience and the experience of a lot of others says the opposite. You spend an unbelievable amount of time jumping through compiler hoops in a static typed language, and sometimes the smallest change cascades down your entire static hierarchy.

      Were we talking about "strong" typing or "static" typing?

      > The other thing is again, "mechanically checking" code isn't possible for anything but type-correctness. No other goodness properties can be checked that way

      But that's a rather weak reason for not doing whatever checking we can do.

      > If type safety could give me something above and beyond type safety, I might be interested. But all it does is "prevent" a class of problems that don't exactly plague most coders anyhow. It's an incredibly steep price to pay for something that doesn't do much for you.

      Strong typing is nothing more than saying what you mean and meaning what you say. I don't know where the heck the purported steep price comes from, since in my experience the strong type definitions in my programs provide me with a set of abstractions to work with. If I declare some colors, they're always colors, not just facades for numbers. And thus I immediately and consistently think of them as colors rather than as obfuscated numbers.

      You should learn to think of strong typing as a mechanism for providing additional constraints on the behavior of your programs. It baffles me that people think that is a bad thing. I find that using a strongly typed language moves bug discovery forward in the development process and makes me think more clearly about the higher-level algorithms I'm coding.

      --
      Sheesh, evil *and* a jerk. -- Jade
    7. Re:Strong Typing is a Must by L3WKW4RM · · Score: 2, Insightful
      One, my experience and the experience of a lot of others says the opposite. You spend an unbelievable amount of time jumping through compiler hoops in a static typed language, and sometimes the smallest change cascades down your entire static hierarchy. You spend a lot of time worrying about things that really aren't importent in the grand scheme of things. Half the time if you're trying to write with any sort of flexible pattern like Iterator or something you end up with a superclass called "Iterable" or "Object" (sound familiar?) or some equivalent, which isn't that far from dynamic typing anyhow.

      Sorry, but what you describe above is the java Interface, a way of guaranteeing that an object will meet a particular group of method signatures (but doesn't require any of the work like creating parent-class implementations). This is something that Python lacks in a big way. For instance, all the almost-like-all-file objects you mention don't have the same group of file-like methods. An interface promises you that all the methods you expect will be there.

      And I really don't understand the argument against strong typing anyway, are you too fucking lazy to use an extra keyword for an argument and return type? You don't like having auto-generated documentation that you can actually use without examining the code to fill in the details?

      The other thing is again, "mechanically checking" code isn't possible for anything but type-correctness. No other goodness properties can be checked that way; in fact Rice's theorem [stanford.edu] proves no non-trivial property of programs can be proven mechanically. (This implies type correctness is not an interesting property ;-) )

      I'm sorry, but this is just wrong. I'm writing code in a weakly-typed language with the most advanced IDE possible. If I'm writing inside a function or method definition, that IDE cannot provide me any intelligent auto-completion or method checking of ANY of the arguments passed in. That IDE can provide me no help when using that funtion or method because it doesn't know what it returns. This might not mean much on a small API that you wrote yourself, but working with someone else's code on a large project means that you've got to have access to all source, all the time to serve as your own documentation.

    8. Re:Strong Typing is a Must by battjt · · Score: 2, Insightful

      I started in ObjC when all Objects were void* (now I think there is normally more checking). I loved it.

      Then I did 6 years of Java and C++.

      A year ago I started working with my original coworker on a large project in Java. He is still using a not very strongly typed programming style (lots of casting and parameters of type Object). It is extremely hard to understand what is going on. There are sections of code that have strong static typing and they are much easier to understand, because the type is obvious. I don't need to go digging through the code to find out if I have a list of switches, switchFamilies, or references to archivedSwitches; the type can tell me right there what I'm dealing with.

      After almost 10 years of corporate IT programming experience at 6 different companies, I find that strong typing is appropriate for large jobs with higher turn over and dynamic typing may be more powerful for jobs with a few guru programmers that need job security.

      Joe

      --
      Joe Batt Solid Design
  3. -1 Flamebait by clem.dickey · · Score: 4, Insightful

    So what other imperfect things should we do away with? Roads with median strips? Safety locks on guns? Metal detectors?

    I would argue that strict typing reduces one class of bugs so that I can concentrate on other less tractable classes. Who gave Guido the idea that strong-typing programmers are satisifed with clean compiles. I will be satisfied with clean compiles when the compiler can detect *all* bugs. Until then we (some of us, at least) need to work on improving languages and language tools toward that goal.

    1. Re:-1 Flamebait by DannyBoy · · Score: 5, Insightful

      I completely agree with you. Any software developer who doesn't have their head up their ass knows that a clean compile only means that you've made it past the simplest bugs. The good ones have unit and regression tests, the others just run a few testcases by hand. Who ships as soon as it cleanly compiles???

      The main thing that scares me about languages with run-time time checking is that the types can change at run time. If you have unit tests where a variable is a integral/scalar value, and then somehow at runtime it has a string value (cause someone called your function incorrectly), you're screwed because you didn't test that. And if your response is that you should have had a unit test for that, then you'd have to have a unit test for every type (incl user defined) types in the language. This isn't really a problem if it is a small program or you are scripting something, because you can have more control over the inputs, but in a large system, especially with a large development team, compile time type checking has an incredible value!!!

      dan

  4. Say it, Guido! by The+Pim · · Score: 1, Insightful
    It's a shame that this view is dismissed out-of-hand by the industry. Of course, this is an industry so conservative that a primitive language like Java is considered "state of the art". I sometimes wonder which will happen first: the industry wakes up to the possible productivity improvements of runtime-typed languages, or type-inferencing languages become convenient and accessible enough to take over. Either one would be a great day.

    Those who say "why would I ever choose not to have certain errors detected at compile time" are missing the big picture. The errors that can be caught by static typing are only the beginning of the errors that can occur in a software system. And they are the ones that will be caught easily in testing. If the uncertainties of runtime typing encourage you to write more tests, so much the better! And have you noticed how much easier it is to whip up tests, and to add the extra code to deal with corner cases, in such languages? In my experience, strongly-typed code tends to be more susceptible to unexpected inputs, just because it's such a pain to handle and test them.

    Further, runtime-typed languages are usually better at slaying the real dragons of software developments: the complex errors that are beyond the scope of typing. Guido said a wise thing:

    You can also easily write a small piece of code in Python and see that it works.
    As a maintainer, I would much rather have code that I can see is logically correct with my eyes, than code that a compiler can tell me type checks. High-level languages are much better at expressing complex ideas in clear code; in C and Java, the idea gets lost in the mechanical details.

    My experience has convinced me that a strong team can produce more robust code in a runtime-typed language than a similar team using a traditional strongly-typed language, given the same amount of time. The first team also has more leeway in trading robustness for speed of completion, when that counts.

    --

    The evaluation of an action as 'practical' . . . depends on what it is that one wishes to practice.
    1. Re:Say it, Guido! by The+Pim · · Score: 1, Insightful
      There's a reason why it's dismissed out-of-hand. That's because the industry has to work with real computers, not virtual machines.

      That's a bizarre thing to say. Do you think that Python code doesn't eventually execute real machine instructions on real ints, floats, and pointers? What evidence do you have that the programmer's view of "data" should match the hardware's view of data? For certain performance-critical or low-level code this is necessary, of course. But I'd much rather have a higher-level view of the world. It's simlpy more productive.

      Since when is Java a "primitive" language?

      Will you accept that C is a primitive language? What does Java has that C doesn't? Garbage collection, which is old hat (what languages in use today besides C and C++ don't have garbage collection?). A simplistic object system to which it does not even adhere faithfully (int et al). It doesn't have pointers, but it does have unsafe casts, so it's not type-safe. If you add generics and boxing, I'll probably take Java out of the primitive category.

      Or is this just your bias that all strongly typed languages are primitive?

      No!! Haskell, for example, is quite advanced.

      p.s. I am not a Java fan.

      I am neither a Java fan nor a Python fan.

      That's why you only accept string input, and validate as a string, and only then convert it to your strong typed data.

      I'm glad you do. But it will take you longer to process the text than it would in Python. Is there any chance that you would do more careful validation, or at least emit more meaningful error messages, if you were programming in Python? This is a perfect example of how "scripting" features may lead to greater robustness.

      We all want code that we can see is logically correct without a compiler.

      I still contend that it is much harder to produce such "obviously correct" code in a low-level language. You lose concision, mechanical details get in the way, and related pieces are in different parts of the code. All of these things make reading harder.

      But compilers are also useful tools to catch those dumb mistakes that we all make.

      I really don't deny that. I just don't think the sacrifice in productivity and expressivity are worth it, just to catch these errors. Have you tried a language like Haskell? One of the wonderful things about it is it catches type errors without imposing a burden on the programmer.

      PS. Thanks for replying thoughtfully to an admittedly trollish message.

      --

      The evaluation of an action as 'practical' . . . depends on what it is that one wishes to practice.
  5. Unit testing has practical limitations by yerricde · · Score: 2, Insightful

    You don't run unit tests at run time! You run them before you ship.

    No practical unit test provides 100 percent coverage of all special cases that can conceivably occur in a sufficiently complex system.

    --
    Will I retire or break 10K?
  6. the majority are not good programmers... by LinuxXPHybrid · · Score: 3, Insightful

    > "Strong or Weak Typing doesn't make good programs. Good programmers make good programs."

    That's probably true, but my wild guess is that the majority of so called programmers are not good programmers. Knowing that, what tool (programming language) should be provided to programmers to write a better (not necessarily good) programs? I think that's the question to be asked.

  7. Here's my vote for static typing! by Tom7 · · Score: 4, Insightful

    Strong typing catches many bugs, but it also makes you focus too much on getting the types right and not enough on getting the rest of the program correct.

    (Does anyone else find it a little scary that Guido confuses "strong" and "static" typing?)

    There's not much substance in this article to actually refute, but I would like to share my experience on this. I have had a lot of experience with static and dynamic, strong and weakly-typed languages, though not much with Python.

    I'm a fan of statically-typed functional languages, especially SML and O'Caml. I agree that static typing catches many bugs; ones that would not be caught at compile-time in a dynamic language. However, in my experience, spending time getting the types right is not a distraction but actually a guide in the design of the program. Static typing encourages . Even if I considered all of that time (which amounts to very little once you become good at the languages) a burden, I think static typing would still be worth it. The reason is that compile-time errors are much, much easier to track down and fix than ones that occur only dynamically (or only once you've shipped your program!).

    By the way, "strong" typing does not mean writing down a lot of types. (ML and Haskell have type-inference systems where you end up writing less than you would in C or Java, and maybe even less than in Python!) By the time you become an expert in a language like ML, you are hardly encountering type errors (except when you make a typo or actual mistake), and hardly writing down anything having to do with types -- the best of both worlds!

  8. Re:Some helpful distinctions by e-Motion · · Score: 2, Insightful
    Weak Dynamic typing: Perl5 (no, really, test it, it's scary - worst of both worlds!)

    That's why the first thing every Perl programmer should type (ok, after the hash-bang line) is "use strict; use warnings;". (Or s/use warnings;/something else that enables warnings/). The Perl interpreter will happily process your mistakes without blinking if you don't take precautionary measures. For example:
    # use strict;
    # use warnings;

    $foo = "bar";
    $foo += 3;
    print $foo, "\n";
    If uncommented, the "use strict;" line will cause the interpreter to complain about $foo not being associated with a package (guards against typos). The "use warnings" line will cause the interpreter to print a warning message when it executes $foo += 3 ("Argument "bar" isn't numeric in addition (+) at..."). With both commented out, the interpreter happily executes the code and prints (on my system) "3". I agree that this behavior is horrendous. So remember kids, "use strict" and enable warnings!