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

Now In Eye Popping 3D!

It took a little bit of fighting with bugs that weren’t showing up in the 2D view, and a bit of time to figure out what was going on with the lighting system in JME, but I finally got the 3D display of the fractal landscapes working.

The first stage was just displaying each node as a discrete point so I could see that each was in about the right place. It looks a little bit like this:

 

Fractal landscape as points (click for bigger).

 

I did this by simply piping the spatial coordinates and colour information of each node into a pair of standard Java FloatBuffers, passing these to a JME Point class (which should really be called PointSet, in my opinion) and attaching this to the root display node of my JME application. The colouring scheme is the same as the one used for the 2D display. Some things don’t look quite right, largely due to the fact that I’ve just drawn the “underwater” points as blue, rather than adding any actual “water.” Don’t fret, it’s on the todo list.

That said, the landscape looks about right. All the points seem to be in their correct location. As a quick implementation note, I’m defining the (x, y, z) coordinates of the scene in the following way:

x = east

y = altitude

z = -north

With some scaling factors used to map the values from the [0,1] range used to address them to slightly more real world like dimensions.

The next stage was to display the landscape in wireframe to make sure the connections I’ll be using create a more solid looking polygon based display are all working correctly. Why not just go directly to polygons? You can see the the detail better in the wireframe display, making debugging much easier. I’ll definitely be using it again later.

This time, instead of piping each and every node into the vertex array, only the nodes at the highest level of detail are used. These are the nodes generated during the final “square stage” of the fractal algorithm, for those of you playing at home. Each draws a triangle (consisting of three separate lines) into the vertex buffer for each pair of parents it has in the landscape. The result looks something like this:

 

Fractal landscape as lines (click for bigger).

 

Everything seems to be in good order there, I think. One or two things don’t look quite right, particularly the beaches, but the tessellation and coverage of the polygons looks right. Here’s a closer in look at some of the polygons so you can see what the tessellation scheme actually produces:

 

Polygon tessellation (click for bigger).

 

You can (hopefully) see that each of the “active” nodes sits at the centre of a diamond formed from the shape of its parents, so it’s the points with four lines branching from them (rather than eight) which are actually being used to draw the scene.

Next: polygons. Again, only the nodes at the highest level of detail are used. This time, each inserts itself into the vertex buffer, then adds its parents if they’re not in there already. Each node remembers its postion in the vertex buffer, and these indices are then used to draw the actual polygons. These are declared by passing the indices in sets of three into a standard Java IntBuffer. The buffers are then passed to one of JME TriMesh geometry classes and displayed, like this:

 

Fractal landscape as polygons (click for bigger).

 

Again, the beaches don’t look quite right, but otherwise I’m reasonably pleased. I still need to add the actual water and improve the form of the landscape itself (and about a million other things), but in terms of display this is looking pretty good. Except for one thing: I’m using far more detail than I need to. Let me illustrate this with a slightly more extreme example. The pictures I’ve posed so far were generated using seven iterations of the diamond square algorithm. Here’s what happens when I use ten iterations (remember, the number of points increases exponentially):

 

MOAR POLYGONS! (click for bigger)

 

On the bright side the beaches look better, but that’s a lot of polygons. Far more then we actually need to display. 1579008 polygons, in fact. We need to reduce that somewhat, if we’re going to make things more complicated and maintain a reasonable frame rate (I’m getting about 60fps with this view at the moment). You can see the problem more clearly if I show you the same view using lines rather than polygons:

 

MOAR LINES! (click for bigger)

 

You can just about see the individual triangles up close, but further away the lines just mush together. I think we can afford to reduce the level of detail as we get further away, don’t you?

Well, I’ll get right on that, then…

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 2: Java.

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

The first language I’m considering is Java. This is by far the language I’m most comfortable and proficient with. It was used for about 90% of my Bachelors degree, I wrote the entire codebase of my PhD using it, and it gets used here and there in my day job. I’m comfortable with Java, and find it to be quite a pleasant language to program in. Big tick on the question regarding my ability to use it, then. Java has some modest dynamic capabilities built in, but it also has a lot of small options for using higher level languages for the scripting, the cleanest of which is possibly Groovy.

Java has a bad reputation performance wise, but this largely isn’t true any more. It does run using a virtual machine, but is compiled to native code at run time. It’s a lot easier to write good code using Java than the other languages I’m considering, and that can help with performance a lot, but in general Java has the potential to be the slowest of the three, all things being equal.

Tools are actually not a problem. There are a lot of high quality graphics engines available for Java, with the Java Monkey Engine (JME) being my favourite. A physics add-on is available in the form of JMEPhysics, with the next version slated to have a physics engine baked in. Raw OpenGL is also an option with LWJGL, should I want it. Likewise, I suspect that the Red Dwarf Server is likely meet my communication needs.

The applicability of Java to other interested parties is an interesting question. A lot of software gets written in Java. A LOT. But the vast majority of it is not games. Largely, I think this is because it’s perceived to be lacking in the performance department. It’s also a little harder to protect you code when you’re writing in Java, too. The previously mentioned JME has the support of a commercial games company, though, so clearly there is interest. Computers are getting faster at quite a rate, so performance has the potential to be less of a concern, especially if the project you’re working on has the whiff of a server side application about it. When it comes to server side code, I think Java is definitely winning the race. Frankly, I have a bit of trouble calling this one either way.

One language down, two to go. Look for the next post tomorrow (here), should you be interested in such things.

Web Frameworks, Desktop Application and My Newly Melted Brain

Let’s get the preliminaries out of the way: Hello, long time no see, how have you been? Etc…

So, whilst writing up my PhD (experiments finally finished!) and working three days a week (going fully full time at the end of September!) I have a fair bit on my plate at the moment. So obviously I’ve also started cooking up a side project…

I don’t want to talk too much about what it actually is (mostly because I don’t know how “open” I want it to be yet), but it’s definitely going to require a web service. It might also require a fully fledged website (lets call this the spotify model), or it might just use a desktop application which connects to the service when it needs to (lets call this the iTunes model). Then again it might have both, with the desktop application providing some extra functionality which is hard to do over the web or in the browser. I think there’s also scope for a mobile application, so lets throw iPhone and (what the hell) Android into the pot as well.

I had a bit of a poke around with some web development frameworks first, since this is probably the bit I know least about.

One of the first solutions I came across was Django, which is based on the python language. It looks good, through I have some issues with its handling of “one to many” relationships. Python is a language I have limited experience with, but it gets a lot of love, and people are putting a lot of work into into making it better. It also has good integration with native libraries. So that’s nice. It’s also named after Monty Python, which only raises my opinion of it.

I’ll essentially skip over TurboGears and Web2Py, two other python based frameworks I did some reading about. Django seems like the best choice out the python based frameworks I’ve looked at.

Soon afterwards I discovered Grails which is based on the very cool groovy dynamic language. Groovy, as well as being fun to say, is implemented directly on top of Java, by far the language I’m most comfortable with. The are a lot of existing Java libraries, and groovy has perfect integration with them. Groovy and grails have some serious weight behind them, also.

One framework I hadn’t really considered was Ruby on Rails, which is in many ways the daddy here. This is mostly because I dismissed Ruby as a language out of hand. It looks mental. For reasons I’ll come to in a minute, I’ve re-evaluated this desision, and will be having a good long look at it. On the one hand it’s been around a good long while, so has had the oportunity to become more refined. On the other hand, it was the first of its kind, which means it might also be stuck with some bad early design desicions. It’s the original, this might not make it the best. I don’t know yet.

In order to build this… thing, I’ll need data representation, I’ll need algorithms for processing this data, and I’ll need ways of displaying it. I’d rather not have to deal with multiple implementations in different languages, particularly with the algorithms.

Problem.

If I want to make desktop applications, python is a possibility. It has good bindings to GTK and QT for linux, and Cocoa for OSX (there’s probably also something which will work on windows, and at some point I’ll probably have to care about this). Java will work anywhere, but it’s quite difficult to make a desktop application with Java which won’t look like crap. My priority in this area is going to be OSX, and Java does not measure up here. I had not considered Ruby, until I came across MacRuby, which is a very interesting idea. It’s not a bridge or a set of bindings, you see, it sits right on top of the objective-c runtime. All of a sudden, Ruby and Rails are a contender.

In the mobile space, things get shitty. iPhone requires objective-c, Android requires Java. Bugger.

So here’s where I am: I think groovy is a better language than python (and probably ruby), and I think grails is a better framework than django (I’ll get back to you regarding rails)… but I’m not sure I want Java hanging around my neck when it comes to building a desktop application. I might also need some serious grunt in the algorithm stakes, and I’m not sure I want to do that in a scripting language. Yes, I need to be able to implement it quickly, but I also need it to run quickly. I’m not sure I want to do that in either C or C++. Java would be good for this (and yes, it’s plenty fast). Objective-c is an option I haven’t discounted.

The search continues… any thoughts?