Friday, May 02, 2003

Bruce Eckel has made a thoughtful post about Strong Typing vs. Strong Testing which makes the case for Python versus strong statically typed languages and includes code examples. I recommend you check it out, I just wish Bruce had a place for people to leave comments. Here's a taste:

But without a full set of unit tests (at the very least), you can't guarantee the correctness of a program. To claim that the strong, static type checking constraints in C++, Java, or C# will prevent you from writing broken programs is clearly an illusion (you know this from personal experience). In fact, what we need is

Strong testing, not strong typing.

So this, I assert, is an aspect of why Python works. C++ tests happen at compile time (with a few minor special cases). Some Java tests happen at compile time (syntax checking), and some happen at run time (array-bounds checking, for example). Most Python tests happen at runtime rather than at compile time, but they do happen, and that's the important thing (not when). And because I can get a Python program up and running in far less time than it takes you to write the equivalent C++/Java/C# program, I can start running the real tests sooner: unit tests, tests of my hypothesis, tests of alternate approaches, etc. And if a Python program has adequate unit tests, it can be as robust as a C++, Java or C# program with adequate unit tests (although the tests in Python will be faster to write).

5:36:01 PM    comment []

Bill Venners asks Can Static and Dynamic Languages Live in Harmony? This is a follow-up article to Robert Martin's Are Dynamic Languages Going to Replace Static Languages?

This is probably a good time to quote a section from Mark Pilgrim's DiveIntoPython []

An erudite reader sent me this explanation of how Python compares to other programming languages:

statically typed language
A language in which types are fixed at compile time. Most statically typed languages enforce this by requiring you to declare all variables with their datatypes before using them. Java and C are statically typed languages.
dynamically typed language
A language in which types are discovered at execution time; the opposite of statically typed. VBScript and Python are dynamically typed, because they figure out what type a variable is when you first assign it a value.
strongly typed language
A language in which types are always enforced. Java and Python are strongly typed. If you have an integer, you canít treat it like a string without explicitly converting it.
weakly typed language
A language in which types may be ignored; the opposite of strongly typed. VBScript is weakly typed. In VBScript, you can concatenate the string '12' and the integer 3 to get the string '123', then treat that as the integer 123, all without any explicit conversion.

So Python is both dynamically typed (because it doesnít use explicit datatype declarations) and strongly typed (because once a variable has a datatype, it actually matters).

10:16:38 AM    comment []