Simple and complex type systems, and JavaScript

Tags: javascript, types, simplicity, complexity, duck typing

Javascript. It won't go away. Like many coders who are experience in compiled, strongly typed languages, my accommodation to JavaScript has been slow

One thing that I have learned from JavaScript is about other languages - how the complexity of type systems feeds on itself. I mean this:

• If you have a language that has Object-oriented type hierarchies, you have types in an is-a hierarchy, some of which are compatible, some of which aren't, some of which might or might not be. So you need casts and tests

if (myAnimal is Cat)
{
  var myCat = (Cat)myAnimal;
}

• If you need to use unrelated types in the same way but don't want multiple inheritance, you add interface types to the type system to get around it's limitations.

class Cat: Animal, IWalk 
{ 
  … 
}

And you need casts to interface types, and need to consider hierarchies of interfaces

• To reduce the repetition of similar code in types like CatList, DogList, AnimalList without going to the weak typing of ObjectList? Now you have generics like List<Cat>

And now you have the potential to cast to generic interface types, and to have hierarchies of generic classes and generic interfaces.

• Want to add methods to types (or families of types) that you can't alter? Now you have extension methods, and you may have extension methods on a generic interface type on a base class.

The complexity feeds on itself. You get around the limitations of the type system by adding another feature to the type system, which interacts with all the others. The total complexity may be proportional to the factorial of the number of features.

Duck typing does away with all of this. It has other drawbacks, but complex type systems are not something that you worry about.

You don't need extension methods in JavaScript as you can add methods to an object any time you want. Or its prototype. You can even change an object's base class at run time. Sure this gives some drawbacks - you have new and exciting ways to shoot yourself in the foot or to clash with what another library is doing.

Consider the following short JavaScript program:

<html>
<head>
</head>
<body>
<script type="text/javascript">
  var greeter = function(name) {
     return "Hello, " + name;
  };
  console.log(greeter("reader"));

  greeter.Name = "Jimmy the function";
  console.log("Today your greeter's name is " + greeter.Name);
</script>
</body>
</html>

If you fire this up and look at the console in the browser's dev tools, the output is

greetername.html:9 Hello, reader
greetername.html:11 Today your greeter's name is Jimmy the function

So we have a function and we go up to it and attach a property. We can do this because functions are objects, and javascript object work a bit like the c# type Dictionary<string, object> as far as reading and writing properties go.

Like I did, you may wonder: "Why does JavaScript let you do this?". This is the wrong question. JavaScript is not that kind of language. JavaScript is a "Why the hell not?" kind of language.

And we can add this code:

greeter.DescribeYourself = function() { 
  return "Your greeter describes themself as " + greeter.Name; 
} 
console.log(greeter.DescribeYourself());

This works, the output is Your greeter describes themself as Jimmy the function

So now the function has a function (which can have function which can have a function…). Sure, why not? You might find a use for it. There are some happy accidents that come from this – features that arise almost by accident, that look a bit clunky but they work. E.g. starting a variable scope in most c-like language looks like this

{
  // code goes here
}

In JavaScript it is the baroquely-named "self-invoking anonymous function":

(function() {
  // code goes here
})();

Which is a lot more verbose, and looks a bit hacky until your eye learns to slide over this common idom.

Links:
Javascript: the core
Crockford's the good parts.
This excellent 30 day series of video tutorials on jQuery goes at a good pace and assumes a working knowledge of JavaScript, so it's for when you know a few of the basics.

Add a Comment