A month of D…

Whenever evaluating some new tool, its often not the first few days that tell you the most, but rather more generally how this new tool fares over a longer time scale.  Before getting into D, I was in the process of evaluating a few different languages, but I always had C as a backstop despite its short comings it has a few major things going for it.  Primarily a lot of libraries provide a C api and can be directly linked into your application without any kind of “wrapper”.   Readability is important, well I know you can make C code obscure if you want, but generally its not a mess of ASCII spew for source, the stability is nice too, over the years C hasn’t adopted this years latest programming fads, you could even argue that if its Turing complete why would you need to add anything to it at all…

While looking at different languages you’re often looking at bindings for different libraries, this is where you encounter FFI’s (foreign function interface) for a number of reasons if you have easy (and fast) access to a C interface, life is very much easier.  I’ve come to the conclusion that not needing to have a FFI to C libraries is a very important feature, especially if you’re writing games.  You really don’t want any barrier between OpenGL and your code or for that matter any of the other libraries that make creating a cross platform application so much easier than it used to be.

Over a month or so I’ve tried out a number of potentially problematic tasks, including things like porting some of my C code and integrating a simple static C library into a D project.  Happily by and large pretty much all I’ve tried has worked without a great deal of drama, but one discovery has been a pleasant surprise.  I found porting C very easy, often just take a section of C code, and with a few little tweaks it compiles and just works right off the bat, but there’s more!  I found that after porting code that often it was rather easy to improve it by adding some D features like generics and turning it into object oriented code.  I only ever use generics lightly and only where it make life easier, abused it can harm readability and certainly isn’t indispensable, its quite possible to program without it.  However with D you can lay a light touch of a template on a class and make code that is still easily understandable and doesn’t look like someone dropped a bunch of ASCII confetti on your source, and you often end up with a set of functions or a class that are that bit more elegant.

My recent experiences have also lead me to a gradual realisation that there really is too much of a good thing, I used to be a real Object Oriented Programming fan, but take Java – its entirely too much of a good thing, when everything is an object and all of your ecosystem sticks rigidly to a paradigm even when not every problem is suited to it – then you could have a problem.  But when used with a light touch Objects Oriented techniques do have their place, like make up, less is definitely more!  Fortunately while D does allow you to use classes and had facilities to allow programming by contract and so on, you’re not forced into doing everything using object oriented techniques, even the standard libraries and surrounding ecosystem doesn’t force any particular methodology on you.

While some people seem almost pathologically allergic to garbage collection, I think the benefits vastly over weigh the disadvantages especially when some of the disadvantages are little more than hollow memes.  OMG the world will stop! never heard of object pooling? it really is not at all difficult especially with applications like a game to put yourself in the position where the GC is bothered very little or not at all.  With the speed of modern hardware, a well implemented game is probably spending at the very least some of its time waiting for the next frame to be needed (get too far ahead and your users control will lag…).  Let’s not forgetting the fact that your code is more robust and you’re not wasting time trying to figure what’s causing that obscure memory leak or corruption.  Having a language without some kind robust memory management in this day and age just isn’t an option.  D has taken a pragmatic attitude towards GC and handed a lot of control to the programmer, but by and large I can see my own use of the GC will be just to forget about it running in the background and simply enjoy its advantages, but its nice to know that I have the flexibility there should I have the need.

So all in all, things are looking really very encouraging, code I’ve ported from C is running just as fast, and the code itself is looking just that bit more elegant – and often where you have code that is elegant without over complication you have code that doesn’t “smell“.  So now all I need is a great idea for a game and the world is my lobster….


Leave a Reply

Your email address will not be published.