A few years ago a brewing company had an ad on television in which two Irish guys who are abroad are asked by the locals to either sing or dance, because that’s what all Irish can do according to the cliché. The two lads can neither sing nor dance, so they cunningly choose a third option: they start speaking in Irish (the few words of Irish they remember from school, to be precise). They make a big impression on the locals and the ad goes on to say “It’s not just A or B. There’s probably C.” When it comes to software development, however, C is not always the best option. But let me tell you, there’s probably D.
I first came across the D programming language towards the end of 2010 when I was reading in a Linux magazine that the (then) latest version of Fedora shipped with a compiler for the D programming language. I thought to myself that hopefully D was a modern language that could be compiled to native machine code. The reason why I was looking for a language like D was that I had tried several languages for cross platform development. C was not an option, because I prefer object oriented programming, and C++ was out of the question due to its long and steep learning curve. After all, time was an issue. Objective-C, an otherwise good candidate, is not really cross platform. In the case of interpreted languages, there would be performance issues, the fact that byte code could easily be decompiled (copyright) and that the promise “write once, run everywhere” would not be fulfilled consistently, which defeats the purpose. Also, users should not have to worry about third party software like virtual machines, interpreters and the like. The lack of proper inbuilt Unicode support was also a big minus in many cases.
The software we are developing deals to a large extent with text processing, including non-ASCII characters. The project was originally web-based and the bulk of the code was written in Python, which is fine as long as the program lives on a server and waits for input. However, due to growing demand, it soon became clear that we needed to be able to install our software on laptops, desktops and possibly mobile phones (or cellphones). There were some difficulties with Python such as performance bottlenecks, copyright issues (easily decompilable byte code) and the requirement that Python be installed. After having considered, tried and discarded several alternatives, I decided to give D a try.
To see whether D was suitable for the task and to test its “usability”, I first re-coded one of the core modules in D. The resulting code consisted of less than 400 lines of code. I was surprised at how concise and neat the code looked and that it was basically no effort to write it. The basic functionality (including a second module) was implemented within three evenings. I went on to extend the program to make it fully functional. To achieve this it was necessary to interface to C libraries, which much to my surprise worked seamlessly. No wrappers, no additional tools needed. A big plus, given that C libraries are ubiquitous. The handling of UTF-8 also worked “out of the box”. All this was done on OS X. The next big test was Windows.
Windows DLL in D
If you come from a Linux or Unix background and have ever tried to run your programs on Windows, you might have had the feeling that you are in a strange world where things not only work differently, but everything is arranged in such a way as to thwart all your efforts. Thankfully, D was conceived with this in mind. I was able to compile the code into a Windows DLL, written purely in D, and plug it into an existing program. This was one of the main objectives of the whole exercise and it worked without any major issues or headaches on the D-side.
D was up to the task and did what it said on the box, i. e. that it’s a modern cross platform systems programming language that compiles to native code. Of course, being a relatively new language, D has the benefit of hindsight and addresses problems of modern software development such as concurrency, unit testing and meta programming (for more information on this see http://dlang.org/overview.html). However, as it doesn’t come with an ideology, you can use it in any way you want, functional or object-oriented and you decide which of the more advanced features you want to use.
D has a C-style syntax and features the convenient syntactic sugar one would expect nowadays, like for example foreach-loops. It has built-in UTF-8 support which makes it a perfect candidate, if your program has to deal with non-ASCII characters, which is probably true of most of the software written in and for the non-English speaking world.
Last but not least, the whole coding was done without an IDE. A text editor with the usual support for coding and a terminal application were sufficient to write the code.
Of course, as might be expected, not everything went smoothly. One of the difficulties I encountered was the lack of examples, because sometimes D does things differently. Luckily there are more resources for D available now and the D homepage http://dlang.org/ has improved a lot.
I got stuck with my project for a while, because there was a lot of “commotion” in the D standard library for a few months. Methods I used had become deprecated and were no longer supported by later versions of the compiler. Thus, I could not use the newer features, but at the same time I didn’t want to write new modules using an old compiler and hence producing more deprecated code. I had to update all of my code before I could go on, which took me a few hours. However, things have calmed down in the library now and there is much more “security”. Still, as D is constantly evolving, it is a good idea to always use the latest version of the compiler and check for deprecation warnings.
At this point, I would like to mention the D community. Albeit a relatively small community, its members are always there to help when you get stuck. You will find the D community at http://forum.dlang.org/.
I also recommend the book The D Programming Language by Andrei Alexandrescu. The author not only describes the language but also explains the reasoning behind the language design. Apart from learning about D, you learn a lot about programming in general, and believe it or not, it’s also fun to read.
Here is a brief overview of the features, because of which I chose D and the problems they solved.
|Compiles to native code||Performance|
|Better protection of source code|
|Stand alone executable (no dependencies)|
|Cross platform||Development on and for different platforms with little or no code changes|
|Good Windows support and integration|
|Interfaces directly to C (no wrappers)||Use of third party libraries in C (and C++)|
|Easy integration of D code into existing applications and frameworks etc.|
|Modern programming style and Garbage Collection||Fast development|
|Clean and concise code|
|Support for OOP||Reuse of well-established design-patterns|
There are many other features of D I discovered and learned to appreciate as I went along. A lot of thought has been put into the language as regards both theoretical and practical issues of programming. So is D perfect then, you might ask? No, of course not. No programming language is, because in software and language design there are always trade-offs. But D helped me to solve a real-world problem in a relatively short period of time. Furthermore, the fact that individual people and big companies feel the need to invent new languages (cf. Rust and Go) to cater for and adapt to the ever changing demands of software development shows that there are still a lot of issues that need to be addressed. D has already come a long way in addressing them and will hopefully continue to do so. My advice is to have a look at its features (cf. http://dlang.org/comparison.html) and see if and how these features can help you to solve some of the practical problems you face in your daily tasks as a programmer.