Typing system vs. Java

Jonathan Hogg jonathan at onegoodidea.com
Sat Aug 4 06:50:55 EDT 2001


In article <5k1nmts15l7nsio00bmht2gnp12tbuq3qn at 4ax.com>,
 Courageous <jkraska1 at san.rr.com> wrote:

> I feel alot of synergy for the above sentiment and believe it is quite
> insightful. Of late, I've been thinking alot about lexical, grammatical,
> and semantic complexity as it applies to computer programming and
> the various different correctness memes which float around in the
> community. I've concluded  that comprehensible tersness trumps all
> other measures of merit.

Top point. Since I think this thread originally arose out of a 
comparison to Java (I think, seems a long time ago now...) I've got a 
Real World (tm) example that might be interesting.

I'm working on a system at the moment that I prototyped in Python and am 
now re-coding in Java - unfortunately the latter language is the only 
one I can use officially. The most trouble I've had has been in 
translating the following pattern:

   def something( self, y ):
      for x1, y1 in self.foo.something( y ):
         for x2, y2 in self.bar.something( y1 ):
            yield (x1, x2), y2

The original was pretty much this much code. Problems I've had 
translating this to Java:

* I've had to create some additional classes to encapsulate things that 
I previously just used tuples/lists for (as in the code above).

* I have to explicitly type all of my variable declarations (more on 
this in a tick).

* There is no standard iterator pattern (for x in xs) so I have to 
explicitly ask for an iterator and do the hasNext() and next() calls on 
it.

* There is no simple way of doing generators (Way to go Python 2.2) so I 
have to construct the entire list and return it.

So my code has ended up something like:

    public Vector something( String y )
    {
        Vector results = new Vector();
        Vector foos = this.foo.something( y );
        for ( Iterator i = foos.iterator(); i.hasNext(); )
        {
            Thingy xy1 = (Thingy) i.next();
            Vector bars = this.bar.something( xy1.getY() );
            for ( Iterator j = bars.iterator(); j.hasNext(); )
            {
                Thingy xy2 = (Thingy) j.next();
                Vector xs = new Vector();
                xs.add( xy1.getX() );
                xs.add( xy2.getX() );
                results.add( new Thingy(xs, xy2.getY()) );
            }
        }
        return results;
    }

Writing this out again (abstracting on the fly to protect the guilty) 
I'm pretty sure that the Python code would compile, but I'm not sure the 
Java code would. I generally have found with moving code from Python to 
Java that most of my type errors are not logical errors, but me writing 
the wrong type declaration.

I was chatting to a guy at work the other day who doesn't know Python, 
but had heard of it and seen some code before. He said, "Python, isn't 
that that 4GL language". At first I started going on about how it wasn't 
a 4GL, but then stopped. The thought struck me that Python is a 4GL. The 
difference becomes obvious in comparison to a 3rd generation language 
like Java. Python gives me much higher level constructs to program with.

Since I can write more powerful programs in smaller, more readable 
chunks, I think I write less buggy code. If we have to reduce the 
simplicity/expressiveness of the language in order to add type-checking, 
then I think the tradeoff is not worth it.

I'd argue that even the protocols/interfaces idea falls into this 
category. Since the majority of protocol/interface errors fall out in 
runtime testing very quickly, the additional effort that would be 
required to specify the interfaces and the hugely difficult compilation 
problem of enforcing them, just doesn't add up for me.

Jonathan



More information about the Python-list mailing list