Revisiting the Language Issue

Some time ago, I wrote a series of posts about language choice for my Clockwork Aphid project. In the end I decided to start the project in Java, this being the language I’m most comfortable with. Once the project reaches a stable state, with some minimum amount of functionality, the plan is to port it to C++ for comparison purposes, this being the language which is likely to provide the best performance.

I still plan on doing this, but I’ve also decided to add a couple of extra candidate languages to the melting pot and get an even broader comparison. The first of these languages is Go, a relatively new language developed at Google. This is not coincidence. I’ve been doing some reading about it lately and finding a lot of things I really like. It has the potential to provide the benefits of both Java and C++, whilst avoiding many of the pitfalls. This is definitely a good thing. It will also give me chance to dogfood (there’s that word again!) some more Google technology.

One of Go’s features which I really like is implicit interfaces. Allow me to explain. In most regular statically typed object orientated languages, such as Java (which I’ll use for this example), you can abstract functionality using something like an interface. For example, let’s say I have a class which looks like this:

class Counter {
  int value;
  int get() {
    return value;
  }
}

Here we have defined an class which declares a single method which returns an integer value. I might then make use of this an instance of this class elsewhere:

class Printer {
  void update(Counter counter) {
    System.out.println(counter.get());
  }
}

All is good with the world, unless I decide I want to change the behaviour of the code. Perhaps I want the value to increment after each call, for example. I could extend the Counter class and change its behaviour that way:

class IncrementingCounter extends Counter {
  int get() {
    return value++;
  }
}

I can now pass an instance of this new class into the update method of the Handler. Done. Right? Well… no. This is a bit of a clumsy way to go about this. It doesn’t scale and it’s not always possible. A better way to handle this is to use an interface:

interface CounterInterface {
  int get();
}

This specifies the interface of the methods, but not their implementation. We can then change the Printer class to use this interface, rather than the concrete class:

class Printer {
  void update(CounterInterface counter) {
    System.out.println(counter.get());
  }
}

Now any class which implements this interface can be passed to the Printer. So, going to back to our original example:

class Counter implements CounterInterface {
  int value;
  int get() {
    return value;
  }
}

We can now make any number of alternative implementations (incrementing, decrementing, random, fibronatchi…) and as long as they implement the interface they can be passed to the printer. This is fine if you’re in control of the implementation, and even more fine if you’re in control of the interface as well. There are times, however, when you’re in change of neither. Things can get a little messy and you may have to find a way of pushing a round peg through a square hole.

In dynamically typed languages, such as Python and Ruby, things work a little differently. These languages are often referred to as being “duck” typed, as they make the assumption that if something “looks like a duck and quacks like a duck, treat it as though it’s a duck.” In this case we wouldn’t bother with any of the interfaces and our Printer class would look more like this:

class Printer:
  def update(counter):
    print counter.get()

So long as the counter object has a method called get() we don’t have a problem. Everything will be fine. This is much simpler, and is one of the things which makes Python very quick to program in, but it does have problems. The main problem (for me, at least) is specification. Without examining the source code, I can’t see what sort of object I have to pass into the update method. If the method has been manually commented then there’s no problem, but this is an incredible tedious thing to have to do. In the Java code I can see the type right there in the auto-generated documentation, and even if the writer has written no comments at all (what a bastard!) I can still get a good idea of what I need to pass into the method.

Go takes a different approach. It’s statically typed, and it has interfaces, but a class doesn’t need to state that it implements an interface. This is implicit and automatic. If a class has the methods defined in an interface, then it is automatically considered to implement it. You get the flexibility of Python with the specification and predicability of Java. This is just one of the things in Go which I think is a really good idea.

On the other hand, I think functional programming is a really stupid idea. I find the languages to be completely horrendous. I feel they must be created by the sort of people who think Linux is user friendly. I consider them curiosities whose only merit is academic. It appears to me that their major use case is to make programming appear more obscure than it actually is and to abstract way the programmer’s knowledge of what the computer is actually doing.

You may be surprised to learn, then, that the third language I’m going to be trying to port Clockwork Aphid into is Scala, a functional programming language. The reason for this is simple: while I personally believe that functional programming (FP) is rubbish, many people disagree. Not a majority, but certainly a very vocal minority. Within Google this minority is very vocal in indeed. The word “fundamentalists” might be appropriate to describe them. When someone believes something that hard it makes me very curious. This is turn tends to lead me towards testing my own beliefs. Sometimes I discover something new and exciting which I was missing out on previously*, and sometimes my initial suspicions are confirmed**. We’ll see which way it goes with Scala.

* Such as the Harry Potter books, which I had stubbornly refused to read until just before the first film was released.

** Such as when I noticed that the Twilight books had taken up the first four places on the Waterstone’s chart and decided I aught to find out what all the fuss was about.

Advertisements

Language Post Mortem and Some Other Notes

A couple nuggets of knowledge came out of my “You’re Speaking My Language, Baby” series of posts, so I though I’d just take a quick moment to talk about them.

The first two are perhaps the most obvious by far. Firstly: if you actually write blog posts, people are more likely to read your blog. Funny that, huh? While my post on installing Celtx on the Acer Aspire one is still my most popular by some margin (probably because it actually provides some utility), I actually had my highest numbers of hits per day during the last week. Secondly: I get less hits over the weekend. Lax working habits for the win!

What’s also interesting is the relative popularity of the individual parts of the series. Most popular first, it goes like this:

  1. Introduction
  2. C++
  3. Conclusion
  4. Java
  5. Objective-C

Now, my number of hits still isn’t exactly stellar, so this is a fairly small sample size, but it’s still quite interesting. People seem to be far more interested in reading about C++ than any of the other languages. In general, readers tend to want to know what it is I’m actually talking about, and what conclusion I come to, but when it comes to specifics, C++ gets the most interest. Is this a recommendation of the language, or the oposite, though? People could be reading what I say about it because they think it’s the sensible option… or because the folly of the language makes them seethe with rage. Hard to say. Perhaps I’ll look for some metrics of programming language popularity online.

In one of those curious events the internet throws up, the writer of a blog I read on a regular basis also just started to work on a project of a potentially similar nature, and started off with some musings on which programming language to use. I’m talking about Shamus Young in his Twenty Sided blog (I should really add it to my blog roll). Interestingly, and slightly comically, he came came to an equal and oposite conclusion to my own. He didn’t consider Objective-C (not out loud, anyway), but decided that Java was the language to use if he wanted to produce something with commercial viability, but C++ was the language to use if he wanted to do some prototyping.

I’m still scratching my head at this in some ways. I don’t care how much experience you have in C++, you’re still likely to program faster in almost any language other than C. But in other ways it makes perfect sense. He has about a decade’s worth of experience with C++ (likewise I have about ten years worth of Java under my belt), but only limited exposure to Java. He’s looking at building a complete game, so he’s being influenced by games like Minecraft (which I will be talking about more later) which were successfully developed by an individual (in Java, as I understand it). If you’re making something a bit niche and you don’t have massive amounts of resources, then having a game which can be effortlessly ported to every major operating system is a good thing.  You want to expand you potential audience as much as possible. Also, if your target demographic slants towards the nerd side of the spectrum then you don’t want count out Linux, nor OSX (which gets more nerd love than you might expect). Having your game be able to run out of a browser doesn’t hurt, either.

I’m not (at this point) looking at building a complete game, but a piece of technology which could potentially be used by multiple games, though. Something of the order of a physics engine. Middleware is the term I seem to hear used. Thus Java (which I have more experience with) is my prototyping language, but C++ makes sense as an eventual target.

I’ve been holding back on what I’m actually doing, but I essentially outed myself when I said it was similar to what Shamus is. So: I’m doing something connected to procedural content generation. I’ll explain more about what that means as I go along.

In other news I have two weeks off work. Seems I haven’t used the vast majority of my holidays this year and taking the entire month of December off is not considered to be ideal. Thus: I have two weeks to do with as I please. So long as what I please doesn’t cost too much or inconvenience my girlfriend. I may visit my parents or even some of my friends down south. I’ll also spend quite a bit of time sitting in coffee shops with a book and a note pad. Coffee shops are good places to think, I find. Just the right amount of bussle. I’m also going to crack on with Clockwork Aphid. I’m tinkering with some implementation details at the moment, but I plan on writing about what I have so far as well. I’m also hoping to make the Heston Blumenthal chilli con carne I mentioned in a previous post, but there are complications. Firstly, he’s quite specific about the types of chilli powder you should use and some only seem to be available from the good ol’ USA. They’re on order, so hopefully they’ll arrive fairly soon. Secondly, there’s clearly a mistake in the recipe, unless Heston want me to boil a pan of water and prepare a bowl of ice water for purely ornamental purposes. This isn’t completely outside the bounds of reason.

More updates soon. Look to the skies!

You’re Speaking my Language, Baby. Part 5: Conclusion.

Author’s note: As this post started out HUGE, it’s been split into parts. You’ll find the introduction here, my comments on Java here, my comments on C++ here, and my comments on Objective-C here.

So… what’s the conclusion? It mostly comes back to the fact that I’m doing this mainly for fun (though you may have trouble believing it). That being the case I’m going to start working in Java. In fact I already have started working in Java, and I’ve already written the first bits of code. I’ll talk about and make them available soon.

I just can’t ignore the sheer applicability of C++, though, much as I may dislike it as a language. Most game developers are going to have the majority of their legacy code written in C++ and that creates a lot of momentum. Games are among the more demanding things most people do with their computers, so they generally try to squeeze every last drop of performance out of the system they’re running on. C++ does have the potential to provide a performance advantage over Java (even if you might end up loosing that to your AI system when you starting having to use Lua to script behaviours). Another one of the reasons for this project was to create a bit of work which might act sort of like a portfolio piece. So, once the project has reached an early, but functional, stage of development I’m going to re-implement it in C++ and then see how I feel about the two different implementations before continuing. It’s not impossible that I’ll end up keeping both, but more than likely I’ll kill one and just stick with the other.

By a process of elimination you might have realised that I’m now counting Objective-C out. This is true, but I have another side project I may end up using it on. One which lends itself quite well to being either an iPhone/iPad app or a website. Or all three. Objective-C is clearly quite applicable to the first two, and surprisingly applicable to the last, if you go the Objective-C -> MacRuby -> Ruby on Rails route.

That was the plan, at least, until I went ahead and did something silly. I have more than a passing interest in programming language design and so found myself reading about other programming languages. Stupidly, I found a couple which have enough merit that I really can’t count them out.

The first of these is D, which is designed to fix a lot of the problems with C++ whilst maintaining its advantages. It seems to succeed at this quite well, so far as I can tell. It also seems to have direct access to a lot of things built directly for C/C++.

Then we have Scala and Fantom, which use the Java virtual machine as their runtime. Both seem capable of achieving the same level of performance as Java itself, but take away some of the legacy cruft which Java has been unable to shake, whilst adding extra useful features. Scala I’m only just starting to learn about, but people seem to like it a lot. Fantom, I think, might be perilously close to being the perfect programming language by many metrics, though. Don’t take my word for it, have a read about it. I dare you not to be impressed (assuming, of course, that you are the sort of person who gets impressed with these sort of things). It adds some very cool extensions and has direct support for some very useful things, such as allowing both dynamic and static typing under the developer’s control.

Both Scala and Fantom can transparently use libraries written in Java, though Fantom can also deploy to both .net and javascript (for web development).

All three of the these languages are interesting enough for me for to not count them out entirely, so I might also try a port to one or more of them.

As always, comments are welcome, so please feel free to try and convince me of the error of my ways. Keep it civil, though, I know how excited programming language discussions seem to make some people.

You’re Speaking my Language, Baby. Part 3: C++.

Author’s note: As this post started out HUGE, it’s been split into parts. You’ll find the introduction here, and my comments on Java here.

The second language I’m considering is C++. This is the language that I use the most at my day job. It’s also the language that’s used to build the vast majority of computer games and one hell of a lot of commercial software. I’m not as familiar with it as I am with Java, but I know it well enough to be productive with it. I’m also familiar enough with it to know how horribly broken it is in many respects. One of the major design goals of Java (among other more modern programming languages) was to fix the problems with C++. It also has no dynamic capabilities what-so-ever, but it’s possible to paper over this by using a minimal dynamic runtime such as Lua for scripting.

All things being equal, C++ is the fastest of the three languages. It is also the one you’re most likely to write bad code in, though, so there’s a bit of a trade off here.

As I mentioned, most games are programmed using C++. As a result, there is a veritable shit load of graphics engine options. I would probably tend towards using the open sourceOgre3D rendering engine (or something similar), but it’s worth baring in mind that I could easily switch to using, say, the Quake 3 engine (open sourced by id) if I wanted to. I could also port the project to using a commercial graphics engine if I had the desire to do such a thing.

The measure of applicability to other parties is definitely a point in favour of C++. Code written in C++ would be the easiest of the three for deployment as part of a larger project, as that project is most likely to be written in C++. In terms of acting as a developer showcase C++ has the edge as well, as it’s the language a lot of companies ask for code samples in.

Looks for my comments on the last language I’m considering tomorrow (here).