r/ProgrammingLanguages • u/Mean-Decision-3502 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 resultMy Impressions:
- I think Python is the winner in pure readability. It is close to the absolute minimum.
- In the
FillArrayversions,global darrmay not be obvious to beginners. - In
for i in range(maxval), it is not immediately obvious thatistarts at 0 and ends atmaxval - 1. darr = [0] * maxvalis compact, but it looks very similar to0 * maxvalwhile doing something very different. Still, it is not far from natural human thinking: take this[0]valuemaxvaltimes.- 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
endfuncMy 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
forloop, it is obvious whereistarts, andcountmeans it will be incrementedmaxvaltimes. I find this fairly natural. (Theforin DQ also hastoandwhilevariants.) - The semicolons add some noise.
- The implicit
resultvariable 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,
FillArraybecomes 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
forloop, you can see clearly whereistarts and where it ends, not "one less than the end." Length(darr)is not especially comfortable to use.- Some people think
endis 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
resultvariable.
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
forfrom 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
unsafeblocks are required here because of the globaldarr. 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.
View all comments
Show parent comment
1
u/Tasty_Replacement_29 Bau Jun 13 '26 edited Jun 13 '26
I find it interesting to discuss such things with others that care about such things.
> Sometimes you have colon between the var_id and type, sometimes not.
Unlike Python, your language is statically typed, the same as mine. Another aspect is whether you distinguish between declaration and assignment (in Python you do not).
For statically typed languages, type inference avoids redundancies. You are using
var arrlen : int32 = darr.length;In my language this isarrlen : darr.len(if arrlen is constant) andarrlen := darr.len(if arrlen later changes). But the type is inferred. My language also allows to specify the type, that would then bearrlen i32 : darr.len.In my language, the colon is to initialize:
:defines a constant.:=defines a variable (yourvar). The colon is only needed if there is a value or initialization at that position. For example:If there is no colon, then it is only a declaration, without initialization. It is also possible to have a declaration in code.
> If you put types after the var_id, then you have to move the array specifier to front: []int otherwise you will got problems later.
No, this is not needed. For example, Typescript and Nim also use
int[]. I find this easier to read than[]int. Go (and other languages) use that style for other reasons, not because the other way is a parser problem.> The parser error recovery is hard when you don't have proper delimiters.
Why? I'm not aware of such issues, and even true, I don't think it's useful to optimize for error recovery. Likely the best error recovery is to log an error, guess what was likely meant, and then recover as soon as possible (e.g. on the next line).