On 07/23/2013 09:47 AM, H. S. Teoh wrote:

> On Tue, Jul 23, 2013 at 06:24:14PM +0200, Jonathan A Dunlap wrote:
>> Check it out: D is listed rather favorably on the chart..
>> http://redmonk.com/dberkholz/2013/07/23/are-we-getting-better-at-designing-programming-languages/
>
> I'm skeptical about using LOC/commit as a metric for measuring
> expressiveness.

Agreed. How many lines of code is the following?

import std.algorithm;
import std.range;
import std.traits;
import std.conv;

auto evensJoined(R)(R r)
    if (isNumeric!(ElementType!R))
in {
    assert(r.length > 3);

} out {
    // I don't know...

} body {
    return r
        .filter!(a => !(a % 2))
        .map!(a => a.to!string)
        .joiner(",");
}

unittest
{
    assert(evensJoined([ 0, 1, 2, 3, 4 ]).array == "0,2,4");
}

void main()
{}

My answer is 1 line of code.

Also, comparing C, C++, and D, the three languages that I know well, I am shocked that C does not get at least twice as many lines as the others. My hunch is that that C code is buggy code.

First of all, C does not have exceptions so every single thing must be done as two lines:

    err = do_something();
    goto_finally_if_error(err);

    err = do_something_else();
    goto_finally_if_error(err);

Additionally, C does not have constructors and destructors so every object and variable must be properly and explicitly initialized and cleaned up.

Further, since the return value must be reserved for the error code, the results must be returned as an out-parameter:

// (Sorry, I haven't compiled this C code)
int foo(int arg, int * ret_result)
{
    int err = 0;
    int * p = NULL;
    MyType * m = NULL;

    // Function precondition checks
    if (arg < 42) {
        goto finally;
    }

    goto_finally_if_null(ret_result);
    *ret_result = 0;

    p = calloc(/* ... */);
    goto_finally_if_null(p);

    err = make_MyType(arg, p, &m);
    goto_finally_if_error(err);
    goto_finally_if_null(m);

    // use m...

    *ret_result = m.some_member;

finally:

    free(p);
    destroy_MyType(&m);
    return err;
}

Yeah, I think the article is looking at suboptimal C code...

Ali

Reply via email to