2013-10-11

Co.Labs

Facebook Adds 5,000 Lines of D-Language Code--What's That Mean?

A tweet from a coder at Facebook notes that the first lines of code for the site have been written in D, rather than C++. Why?



Andrei Alexandrescu is, according to his website, a research scientist at Facebook, and he's just announced via a tweet that he's just completed the "First D language commit at Facebook. 5112 lines of sweet code!" What can we take from this?

In a post on the Dlang.org website, Alexandrescu elaborates a little more: The code project he's working on is in "heavy daily use at Facebook" and originally it was written in C++. Now that it's in D the team has "measured massive wins in all of source code size, build speed, and running speed." His post has spurred a lot of positive support on the forum, and if what he says is true then it would make sense for Facebook to take a critical tool and make it more efficient--because there will be an upshot on overall efficiency of operations at the site.

But D? D, if you're not familiar with it, is interesting all by itself. The language dates only from 2001, and is an attempt to deal with some of the issues that have affected C++, itself an expansion of 1972's C. The Dlang website rather tartly notes that:

D is not a scripting language, nor an interpreted language. It doesn't come with a VM, a religion, or an overriding philosophy. It's a practical language for practical programmers who need to get the job done quickly, reliably, and leave behind maintainable, easy to understand code.

It's been created because C++ had to maintain backwards compatibility with C, and that as C++ itself has expanded the language's new features have simply added to the complexity of the standard--a document that's now over 750 pages long. Hence the goal was to take the core powers of C++ and reshape them into a language that's simpler, better, makes the most of modern programming habits, is highly portable between different compilers, and can be aggressively optimized in a way that C++ just can't. D looks a lot like C, in layout, syntax, and more, but the language also takes ideas from Python, Java, Ruby, C#, and others.

Is it for you? The suggestion is that it's great for those who decide that C++'s promise isn't worth the complexity of programming in the language, projects "that need built-in testing and verification," and "teams who write apps with a million lines of code."

Now we see why Facebook coders may learn to love D. Facebook's site operations are constantly evolving and have to be hugely complex, not least due to the sheer scale of different apps, services, and offerings in the site and the size of its user database. Writing code more easily, and compiling it into systems that run more efficiently and speedily, has to be an attractive idea for its staff, and may even save Facebook some hard cash.

Should you be inspired enough by Facebook's experiment to be interested in D for your own purposes, the language's GitHub repository is linked here. According to some thinking, you should be able to get the hang of it in only about 21 minutes. ; )

[Image: Flickr user Ninha Morandini]