Why static typed languages are sometimes better.

Donn Cave donn at u.washington.edu
Thu Jul 1 18:15:46 EDT 2004


In article <roy-C894A6.17435601072004 at reader2.panix.com>,
 Roy Smith <roy at panix.com> wrote:

> I just had an interesting little surprise.  I've got a method that takes 
> a string as an argument.  I wanted to change it to take either a string 
> or a tuple of strings, so I did my usual "test first" thing.
> 
> I changed the unit test I already had from calling it with a string to 
> calling it with tuple of two strings.  I then ran the test, expecting it 
> to fail.  The next step would be to go write the code to make the test 
> pass.
> 
> Amazingly, the test passed (that means I'm done, right?).  Well, it took 
> me a moment to realize that the only thing I ever do with the argument 
> in the current version is use it as a dictionary key.  Since a tuple of 
> two strings is a valid key, so the test passed just fine.  Sometimes the 
> language is just too forgiving :-)

Static typing might also have the salutory effect of
helping you decide not to do this in the first place.

You'd change the function parameter type from String
to [String], and recompile.  The compiler would tell
you about every place where the function is called
with a String parameter, and you'd go and change them
from 'xyz "Hello"' to 'xyz ["Hello"]'.  Takes 10 minutes
and leaves you with simpler, easier to understand code.

   Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list