Bloerg
       

Writing C in the 21st century

The Internet Systems Consortium, or ISC for short, is well known for developing and publishing Internet system software such as BIND and DHCP. Today, they commented on why they re-wrote BIND 10 in C++ and Python. I think this needs some discussion. They write

So when ISC started seriously thinking about BIND 10 – around 2006 or so – the question of what language to use for the new project came up.

The first question is of course, “Why not C?” Some answers are:

String manipulation in C is a tedious chore

String manipulation in C can be a tedious chore when you write something like this (taken from lib/isccc/cc.c)

len = strlen(_frm) + strlen(_to) + strlen(_ser) + strlen(_tim) + 4;
key = malloc(len);
if (key == NULL)
         return (ISC_R_NOMEMORY);
snprintf(key, len, "%s;%s;%s;%s", _frm, _to, _ser, _tim);

all the time and don’t follow the DRY rule. Not only is

key = g_strdup_printf ("%s;%s;%s;%s", _frm, _to, _ser, _tim);

shorter and easier to grasp, it is also a lot less dangerous than calculating the final string length by hand. Even if ISC decides not to use a portable C library such as GLib or Apache APR, they could still write something like that on their own instead of relying on C’s standard library.

Error handling is optional and cumbersome

Yes, error handling is optional and sometimes it is cumbersome but exception handling as used in C++ or Java is no silver bullet either. Whereas C1 and Go explicitly tell the developer “look, this might be a potentially dangerous call”, a C++ program might explode in places where you don’t expect that to happen.

Encapsulation and other object-oriented features must be emulated

Encapsulation must not be “emulated”, it is part of the language design. Thousands of robust and cleanly written libraries have shown how this works for decades now: provide an opaque data structure and let the user access it via public API functions. I wonder what the other ominous, object-oriented features are.

C lacks good memory management

What constitutes “good” memory management? Fast, predictable and extensible? Or safe and simple with a garbage collector? Both is possible with C. On the other hand, C++ is a lot better at resource management if RAII is used. But then, it must be implemented consequently.


I am not saying, that C++ is a bad choice nor that C does wonders for BIND. But, naive and untrue statements about C and strange reasons in favor of C++ like this

C++ is also a very popular language, and also has all of the features we are looking for. However, C++ is by no means an easy language to work with, so the idea is that we will avoid its complexity when possible.

deserves some clarification. C is not dead and even today, new projects can be written in it in a sound and safe way. But the people must be willing to dedicate a little bit of their precious time.

A lively discussion about ISC’s blog post is going on in this HN comment thread.

  1. Yes, I know you can ignore return values. But at least you know, that a function that returns an error code can fail. A C++ exception is handed up the stack to whoever catches it first.

Discussion

This post might also have some comments at Google+.