r/ProgrammingLanguages • DQ • Jun 12 '26

Code Readability Comparison

I'm developing the programming language DQ. I'm not doing this just because (with AI help) I can. I started developing my own language because I couldn't find one that had all the critical features I need. One of those critical features is human readability.

My LLVM-based DQ compiler, although some important parts are still missing, is already usable to some extent. I wanted to check its performance, so I created some simple benchmarks. I decided to compare DQ with a few other languages, so I implemented these benchmarks in those languages in exactly the same way.

I find it very helpful and thought-provoking to look at exactly the same solutions in different languages, so I'd like to share my impressions on them.

Note: Please look at the following code snippets side by side, without syntax highlighting.

Please share your thoughts.

Python

darr = []

def FillArray(maxval):
    global darr
    darr.clear()
    for i in range(maxval):
        darr.append(i)

def FillArrayPtr(maxval):
    global darr
    darr = [0] * maxval
    for i in range(maxval):
        darr[i] = i

def CalcSum():
    result = 0
    arrlen = len(darr)
    for i in range(arrlen):
        result += darr[i]
    return result

def CalcSumPtr():
    result = 0
    arrlen = len(darr)
    for i in range(arrlen):
        result += darr[i]
    return result

My Impressions:

  • I think Python is the winner in pure readability. It is close to the absolute minimum.
  • In the FillArray versions, global darr may not be obvious to beginners.
  • In for i in range(maxval), it is not immediately obvious that i starts at 0 and ends at maxval - 1.
  • darr = [0] * maxval is compact, but it looks very similar to 0 * maxval while doing something very different. Still, it is not far from natural human thinking: take this [0] value maxval times.
  • If you only look from a distance, you cannot easily tell which functions return values and which do not.

DQ

var darr : [*]int32;

function FillArray(maxval : int32):
    darr.Clear();
    for i : int32 = 0 count maxval:
        darr.Append(i);
    endfor
endfunc

function FillArrayPtr(maxval : int32):
    darr.SetLength(maxval);
    var pi32 : ^int32 = &darr[0];
    for i : int32 = 0 count maxval:
        pi32[i]^ = i;
    endfor
endfunc

function CalcSum() -> int64:
    result = 0;
    var arrlen : int32 = darr.length;
    for i : int = 0 count arrlen:
        result += darr[i];
    endfor
endfunc

function CalcSumPtr() -> int64:
    result = 0;
    var arrlen : int32  = darr.length;
    var pi32   : ^int32 = &darr[0];
    for i : int = 0 count arrlen:
        result += pi32[i]^;
    endfor
endfunc

My Impressions:

  • DQ requires more text than Python because it is more explicit. Type annotations are mandatory everywhere.
  • The block closers make it clearer where blocks end, and they also indicate what kind of block is ending.
  • In the for loop, it is obvious where i starts, and count means it will be incremented maxval times. I find this fairly natural. (The for in DQ also has to and while variants.)
  • The semicolons add some noise.
  • The implicit result variable shortens some functions nicely.

Pascal

var
    darr: array of int32;

procedure FillArray(maxval: int32);
var
    i : int32;
    len, cap : int32;
begin
    SetLength(darr, 0);
    len := 0;
    cap := 0;
    for i := 0 to maxval - 1 do
    begin
        if len >= cap then
        begin
            if cap = 0 then cap := 1 else cap := cap * 2;
            SetLength(darr, cap);
        end;
        darr[len] := i;
        Inc(len);
    end;
    SetLength(darr, len);
end;

procedure FillArrayPtr(maxval: int32);
var
    i    : int32;
    pi32 : ^int32;
begin
    SetLength(darr, maxval);
    pi32 := @darr[0];
    for i := 0 to maxval - 1 do
    begin
        pi32[i] := i;
    end;
end;

function CalcSum : int64;
var
    i, arrlen : int32;
begin
    result := 0;
    arrlen := Length(darr);
    for i := 0 to arrlen - 1 do
    begin
        result += darr[i];
    end;
end;

function CalcSumPtr : int64;
var
    i, arrlen : int32;
    pi32      : ^int32;
begin
    result := 0;
    arrlen := Length(darr);
    pi32   := @darr[0];
    for i := 0 to arrlen - 1 do
    begin
        result += pi32[i];
    end;
end;

My Impressions:

  • Unfortunately, to get comparable performance in FreePascal, FillArray becomes fairly long because of the allocation handling. That makes this part less comparable, although the rest still is.
  • There are semicolons everywhere.
  • Local variables are defined in a separate block. That has both advantages and disadvantages. For example, you know where to look for a local variable first.
  • In the for loop, you can see clearly where i starts and where it ends, not "one less than the end."
  • Length(darr) is not especially comfortable to use.
  • Some people think end is much longer than }. To me, it still feels like a single token, and I can read it about as quickly as the single-symbol versions.
  • It also has the convenient implicit result variable.

C++

vector<int32_t>  darr;

void FillArray(int32_t maxval) {
    darr.clear();
    for (int32_t i = 0; i < maxval; ++i) {
        darr.push_back(i);
    }
}

void FillArrayPtr(int32_t maxval) {
    darr.resize(maxval);
    int32_t *  pi32 = darr.data();
    for (int32_t i = 0; i < maxval; ++i) {
        pi32[i] = i;
    }
}

int64_t CalcSum() {
    int64_t  result = 0;
    int32_t  arrlen = darr.size();
    for (int32_t i = 0; i < arrlen; ++i) {
        result += darr[i];
    }
    return result;
}

int64_t CalcSumPtr() {
    int64_t    result = 0;
    int32_t    arrlen = darr.size();
    int32_t *  pi32   = darr.data();
    for (int32_t i = 0; i < arrlen; ++i) {
        result += pi32[i];
    }
    return result;
}

My Impressions:

  • For these tasks, I find the C++ version fairly readable too.
  • I find it unnatural when the type precedes the identifier. I don't read that form easily. I always align variables into columns in C++, and that helps.
  • C++ has a good and fast toolkit for FillArray, so it is almost as compact as Python.
  • If you look at the C-style for from a distance, a lot of things are packed into one expression. When reading it, I slow down to verify every piece.
  • Here too, the semicolons add some noise.

Rust

#[allow(non_upper_case_globals)]

static mut darr: Vec<i32> = Vec::new();

fn fill_array(maxval: i32) {
    unsafe {
        darr.clear();
        for i in 0..maxval {
            darr.push(black_box(i));
        }
    }
}

fn fill_array_ptr(maxval: i32) {
    unsafe {
        darr.resize(maxval as usize, 0);
        let ptr = darr.as_mut_ptr();
        for i in 0..maxval {
            *ptr.add(i as usize) = i;
        }
    }
}

fn calc_sum() -> i64 {
    let mut result: i64 = 0;
    unsafe {
        for i in 0..darr.len() {
            result += black_box(darr[i] as i64);
        }
    }
    result
}

fn calc_sum_ptr() -> i64 {
    let mut result: i64 = 0;
    unsafe {
        let ptr = darr.as_ptr();
        for i in 0..darr.len() {
            result += black_box(*ptr.add(i) as i64);
        }
    }
    result
}

My Impressions:

  • To get exactly the same behavior as the others, unfortunately unsafe blocks are required here because of the global darr. Try to ignore those for the readability discussion.
  • The code may be short, but I read it slowly. You have to concentrate on small differences, and the symbol density is high.
  • The variable identifiers do not align naturally into columns, and I find that unpleasant.
  • A large amount of noise is added to the actual code: mut, as, and additional type hints.
  • In for i in 0..darr.len(), there are a lot of dots grouped together. The interval end is exclusive, and that is not something I would necessarily infer at a glance.
  • I find the way return values are signaled easy to miss.
1 Upvotes

23 comments sorted by

View all comments

Show parent comment

2

u/Mean-Decision-3502 DQ Jun 13 '26

I find it interesting to discuss such things with others that care about such things.

I also find nice to share constructive thoughts.

As you explained the concepts, now it makes sense when you are using : when not.

I've checked the bau-lang site on the github. You have a nice collection of different language implementations at test/resources/org/bau/benchmarks. This allows actually a better comparison of different languages than in my initial post.

To be honest at "binaryTrees" I like Nim most. At bau i have a bad feeling.

fun Tree.nodeCount() int
    result := 1
    l : &left
    if l
        result += l.nodeCount()
    r : &right
    if r
        result += r.nodeCount()
    return result

why r : &left is necessary?

Does this work too?

fun Tree.nodeCount() int
    result := 1
    if &left
        result += &left.nodeCount()
    if &right
        result += &right.nodeCount()
    return result

About int[n] vs [n]int.

I was using C long time, and I got use to int[n]. In DQ I started with `int[n]'. In DQ I'm using Pascal pointer notation for types and dereference. Then this expression become very ambigous:

var pia3 : ^int[3];

Is it a pointer to an array or a pointer array of integers?

Changing the array designator position the rules became clear:

var p3ia : ^[3]int;
var a3pi : [3]^int;

In Pascal, the array designator was also before the type:

var intarr : array[1..3] of integer;

In bau really miss the separator between the var_id and type. I would vote always requiring the : after var_id. You can keep := for type inference. I might consider this for DQ.

C "Evilness" / 1

if (3 / 2 * 10 == 10 * 3 / 2) {
    printf("The language is friendly.\n");
} else {
    printf("The language is evil.\n");
}

What is the output currently for this in bau?

C "Evilness" / 2

if (0xFF & (4 < 1) != 0) {
    printf("The language is very evil.\n");
} else {
    printf("The language is ok.\n");
}

What is the output currently for this in bau?

1

u/Tasty_Replacement_29 Bau Jun 13 '26

> To be honest at "binaryTrees" I like Nim most. 

Yes, I also find the syntax of Nim very nice. It is very close to Python. (Nim is statically types and has explicit declarations, like my language). I made a comparative on how concise the syntax is, and Nim is very concise. Bau is more concise due to not using "const", "var", "let", and ":".

The binaryTree example: In my language, you can write it like this:

fun Tree.nodeCount() int
    result := 1
    if left
        result += left.nodeCount()
    if right
        result += right.nodeCount()
    return result

But currently the compiler optimizes this better if "owned types" are used, which then means & needs to be used (a bit like in Rust, but simpler). This is actually more a deficiency of the current compiler: this could be inferred. I'm rewriting the compiler to make this possible. Basically, my language has two options: a simpler syntax mode that is ref-counted, and a second mode that is more verbose, but can result in faster code.

Does this work too?

No, I think this currently doesn't work. It looks like a bug to me actually, I'll check.

Changing the array designator position the rules became clear

I see! Yes, in your language this makes sense. My language does not have pointers in this sense.