Dojo and Conventions in Programming Languages

02 Apr 2008

I went to the Web Monday event yesterday. This was the third time this was arranged in Stockholm, but it was the first time for me. This time, only a handful of people attended, probably because lots of people from Stockholm went skiing last week and spent this week at home with a broken leg. It was a nice event nevertheless. I mean: Discussing software and web development, listening to interesting short presentations, eating pizza, drinking beer, watching 256 byte demos and YouTube clips, what more can you ask for?

Peter Svensson held a presentation about Dojo. The Dojo Toolkit is a framework for making internet applications with JavaScript and Ajax. I have no experience with it, except for a quick look after which I decided that it was a bit too complex for my requirements at the time. Peter noticed me frown at one slide, which wasn't because of the presentation, or even the Dojo Toolkit itself, and I'll try to explain why.

I had a problem with this code:

dojo.declare("myclass", null,
{
  var1: "",
  var2: 17,
  constructor: function(arg)
  {
    this.var1 = foo;
  }
});
var myobj = new myclass(4711);

This is how you declare a class (named myclass) with two data members (var1 and var2) and a constructor. The funny thing is that this way of declaring a class is a part of Dojo. JavaScript is so dynamic and flexible that it's possible to implement object orientation in different ways, so Dojo has its own way of doing it. That is my problem. While it's definitely nice for a language to be dynamic and flexible, I would prefer a standard way to do such basic things as declaring a class.

The standard way to do something in a programming language can be enforced by rules in the language. For example, Java, C++, Python and probably lots of other languages have a class keyword for declaring a class. That is simply the one you use when you declare a class in those languages. (In Python it's certainly possible to create a class with other constructs if you really want to, but you practically never do.)

The standard way to do something can also be just a convention; the way you typically do it. Java has a standard way to document code, called Javadoc. Since this was part of the Java development kit from the very beginning, all Java code uses those conventions, even though they are not enforced by the language itself. Java has benefited a lot from conventions of this kind. Standards for naming is another example where Java has succeeded very well. So, even if the JavaScript language doesn't enforce a specific way to declare a class, there is much to gain by having a convention for doing it. If everyone follows the convention, you avoid a lot of confusion and interoperability problems.

Dojo's way of declaring a class looks nice. Dojo also has other features that seem to make programming easier (such as a package system using the require and provide functions). So I have no problem with that. The problem is that there is no standard way of doing these things in JavaScript so Dojo needs to implement its conventions. Perhaps the Dojo conventions deserve to be the way of doing things in JavaScript, but unfortunately there are no standard conventions yet.

Now, I haven't programmed very much in JavaScript (so far), so perhaps I have no clue of what I'm talking about here. Perhaps this is not a problem. But until I get proven wrong, I'm skeptic about a language that has no standard way of doing very basic things. It makes me think of C++, where every other project declares its own string class. It is plain silly.

blog comments powered by Disqus