D Language
Apr 19, 2014I recently had to write a moderate amount of C around vendor libraries
and get things working in three days. The effort resulted in about 3200
lines of ugly, hastily written code that compiled under
gcc and clang with
-Wall -Werror -pedantic
and looked reasonably leak-free in
Valgrind. I whittled it down over the following
week as time permitted to a bit over 2000 lines.
Lines of code is a lousy metric, but it’s easy to extract using available tools. Here are the counts from cloc for the original two projects in C:
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
C 3 298 193 1869
C/C++ Header 3 32 0 178
-------------------------------------------------------------------------------
SUM: 6 330 193 2047
-------------------------------------------------------------------------------
Quick passes with grep
counts 11, 13, and 16 functions.
A better metric might be complexity. Running it through the cyclomatic
complexity checker
cyclo results
in scores of 79, 99, and 110. These seem completely off the charts
(10-15 is the recommended range for a “module”). Quick runs against some
code I had lying around shows that these numbers are high (OpenBSD’s
strncpy
and a naive CRC-16 implementation I had both score a 5,
SolarDesigner’s
concat
function scores a 12) but we can use them for comparison. Let’s torture
the numbers by averaging, 13.3 functions and a score of 96, for about
7.2 complexity per function.
I’m always on the look out for an advantage (I need them…) and saw a recent announcement of a release of the D Language compiler. The features piqued my interest: native compilation, garbage collection and dynamic memory, types, classes, interfaces, mixins, exceptions, extensive standard library, and an easy interface to C. Walter Bright, the language designer and writer of the reference compiler, is well known. There’s a decent website, a tutorial, and C[]{.underline} guru Andrei Alexandrescu wrote the book on the language. Cool, I thought, a better C[]{.underline}. I bought the book, started coding familiar tasks, and dug in during my little free time.
It went easily and I had a satisfactory result in a few days. The equivalent in D:
-------------------------------------------------------------------------------
Language files blank comment code scale 3rd gen. equiv
-------------------------------------------------------------------------------
D 2 109 96 709 x 1.70 = 1205.30
-------------------------------------------------------------------------------
SUM: 2 109 96 709 x 1.70 = 1205.30
-------------------------------------------------------------------------------
Notice that I’ve reduced the number of files, I’ll get to that. The pass
through grep
counts 8 and 13 functions, of which 3 of them in each
source file are two-line logging routines to replace the classic C
preprocessor macros dbgprintf
and errprintf
. cyclo
doesn’t
perfectly handle D, but it’s reasonably close in syntax to
C/C[]{.underline}, producing scores of 45 and 61. Twisting numbers again
gives 10.5 and 53 for a score of about 5 per function. The improvement
in complexity is on par with the reduction in lines of code.
The kicker is that not only are the D versions are shorter and easier to comprehend, they do more and have more robust error-checking and exception-handling. Overall I’m more confident they behave correctly. I replaced two independent programs with a single one. I replaced a throttled fork()-ing server with a fixed pool of workers. I leveraged associative arrays, JSON, and Base64 without rolling my own or borrowing from other Open Source libraries. None of this is stuff I couldn’t implement or find somewhere but I didn’t have to. Classic force multiplier of a programming language.