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.
6
u/nebbly Jun 12 '26 edited Jun 12 '26
Agree that Python has done very well with Readability, though I'd argue you're undercutting Python a bit:
I would expect your example to look more like this in the wild:
Anyway, readbility is a chief concern for my language, blorp, as well. The top two things I usually keep in mind:
If I was to apply these ideas to DQ, I'd probably highlight the following for consideration:
Just food for thought. My bias would push you toward a language that looks like blorp, of course, because that's what I like.
3
u/Inconstant_Moo 🧿 Pipefish Jun 13 '26
You say that human readability is "critical" and yet I can't really see how DQ is more readable than Python.
It may be that you're the only human you have in mind here, and that you're just designing for your own idiosyncratic preferences. After all, all the other language developers could have gone with
endforandendfunc, but they chose not to because they thought that sort of thing sucks.2
u/Tasty_Replacement_29 Bau Jun 12 '26
I think it is good to optimize for readability. In my language this would be something like this:
2
u/EggplantExtra4946 Jun 13 '26
What tf is thought-provoking about implementing the same damn for loops in several languages?
Code readability of a programs depends hugely on how it is written and architectured, but if you want to focus strictly on the readability of a language, it depends on many aspects: the syntax, scoping rules, type system, memory management, presence of closures, type polymorphism, classes, type classes, metaprogramming features, etc...
8
u/tiajuanat Jun 12 '26
You need to look at languages like J. Yes. Not immediately readable, but that's because each glyph is an algorithm.
I think you should also look at Halstead complexity and how Operators and Operands play together, because it quickly becomes apparent what makes Python, Rust and C++ feel "easy to read"
Maybe there's some inspiration there for you
4
u/L8_4_Dinner (Ⓧ Ecstasy/XVM) Jun 12 '26
Instead of
I prefer:
3
u/deaddyfreddy Jun 18 '26
The problem is that what is readable to one person is unreadable to another. So, in general, people still have to learn syntax, considerations, etc.
Another one is that most languages designed to be written (and read) by non-programmers ended up in the hands of programmers. Take SQL, HTML and Gherkin, you name it.
I'm guilty of this too: once I implemented a DSL to be used by non-techies, and within a few months, I had become one of only two people using it (the other being our QA).
So, in my opinion, a language should be as simple as possible, but not simpler than that, and by "simple" I mean simple for both the programmers who write it and the compilers/interpreters (or programmers who implement the language itself). You could say that one contradicts the other, but let's compare Pascal and C++, the former is definitely simpler in terms of syntax, but at the same time the implementation is also much simpler. My favorite example is comparing the size of the TurboC++ and TurboPascal distributions of the late 1980s - early 1990s, as I recall, C++ took up twice as much space. So it definitely "depends".
4
u/binarycow Jun 13 '26
You say python has the best readibility. I think python's readability is horrible.
Readability is a matter of opinion.
-1
u/teerre Jun 12 '26
Python is by far the most unreadable one. You have to painstakingly read every line of the function to even know what's the argument type
This is also nonsensical code, nobody writes this and, specially the Rust one, is not even idiomatic
This is the classic confusion between simple and easy. You should watch Simple made Easy's talk. They are not the same and in fact are often opposites
0
u/[deleted] Jun 13 '26
[deleted] — view deleted comment