Tuesday, September 06, 2005

The Tyranny of Types

Having been a programmer for more decades than I care to recall, I've seen a fair few languages come and go.

Starting with fortran (although I don't *quite* go back as far as punch cards), then moving on briefly via Pascal to C. A brief affair with Forth followed before I settled down with C++, and object oriented programming.

Looking back, I can classify ease of programming by categorinsing the tasks that I had to keep doing, and doing.

And doing again.
  • With Fortran (and SinclairBasic), it was keeping track of all those pesky gotos.
  • With more structured languages Pascal and C, it was keeping track of all those pesky subroutines, building and maintaining lists of data, and plugging memory leaks.
  • With C++, and OO programming, it was building and maintaining lists of data. And plugging memory leaks.
  • With the advent of the STL and collections, my problems dwindled to: plugging memory leaks.
  • With a better understanding of templates, I realised the joy of shared pointers (*not* those auto_ptr things!), and suddenly, even the memory leaks receded into the background.
Of course, as major concerns decreased, minor ones became more prominent. Chief of these was trying to keep all the objects and types talking to each other. Templates helped here, of course, since they can be described as glorified macros, with typing as part of the input. (However, 'function' templates were a while in coming.)

Meanwhile, as these revelations were occurring to me, I missed the Java wave, figuring I'd catch it when I needed to (I didn't reckon on the GlassWall and AnAthena antipatterns that pervade employment agencies these days). Ditto C#.

However, I did manage to get on the Python ripple, and have been doing a bit of PHP of late. These are scripting languages, and share one naive characteristic.

They are typeless. (More correctly, they are 'dynamically typed', which means that the type specification is defined at the time of assignment. If it's important, you can ask the variable what type it is.)

Now, anyone coming from the strongly typed world of Pascal or, to a lesser extent, C/C++ will throw their hands up in horror! It sounds like a recipe for disaster.

Bear with it.

When several languages jostle for attention, it is inevitable that the respective practitioners compare and contrast, usually with great vigour (after all, what I'm using must be the best: I'm using it!!). I have indulged in this pastime occasionally. While others jeer at the complex, clunky nature of C++ constructs, with umpteen ways of data reference, and umpteen ways to leak, I look smugly at the single class inheritance and multiple interface definitions of Java and C#, and then look at C++ 'templates'.

And smile.

Templates are an *extremely* powerful language feature, if you know how to use them properly. They are what make the STL library possible, with all its attendant benefits.

And it occurred to me, as I got into the coils of Python, that templates are *precisely* what a typeless language gives you.

And the result is pure simplicity. Gone are the bureaucratic overheads of cramming square parameters into round methods, of performing dynamic_casts at each closed interface border. Gone to, are the naked pointers so beloved of the vendors of 'BoundsChecker' and 'Purify'!

In short, Python rocks!

Meantime, I'm getting back onto another project, using C#: strongly typed, with a resulting myriad of functions and calls for every occasion.

The language of the future? But I think I've been here before!

Meantime, where's that variable I haven't initialised?

1 Comments:

At 2:26 PM , Blogger Dave said...

It could have been the start of a wonderful affair, me and Python. But then along came Ruby, and Python was left by the wayside.

Seriously, if you like Python, have a look at Ruby. It's got some pretty nice features and is really nice to write in.

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home