r/Compilers • u/Mean-Decision-3502 • Jun 12 '26
Code Readability Comparison
/r/programmer/comments/1u3x0z8/code_readability_comparison/
0
Upvotes
r/Compilers • u/Mean-Decision-3502 • Jun 12 '26
3
u/matthieum Jun 12 '26
Everything is readable in small examples.
What matters is that a language remain mutable at scale:
Your Rust code is completely unidiomatic, so it's a bogus comparison:
black_boxfor???Rewritten, with inline comments explaining the choices for folks not used to Rust:
There is no pointer version, because nobody sane would use a pointer version. Pointers are to be used sparingly in Rust, as each use must be carefully annotated with comments discharging soundness obligations.
(I mean, ideally it should be the same in C or C++, but they're so omnipresent it would be untenable...)
1
u/ChiveSalad Jun 12 '26 edited Jun 12 '26
I'll bite, although I can't match the code implementations very closely, just the results
I don't have loops, mutation or global variables in yo yet, so I can't do the ptr vs non ptr distinction, and needed to add arguments to the functions. CalcSum comes off as very pretty and readable, which was tricky as type annotations in lisps typically don't look great:
yo has optional type annotations for arguments, and currently always infers a return type. dumping the AST gives
so it really is fully typed.
Omitted argument types turn a function into a template that's monomorphized for each call site's argument types.
The sum function in the standard library is
which works for anything that acts like a list, whose elements act like numbers.
FillArray is less beautiful, doing an extra reverse
Ordinarily you'd just use range (the above code for fillArray is just the code for range in the stdlib with the name swapped.) You can get away without the reverse with a little bookkeeping but I think it's clearer with the reverse
1
u/[deleted] Jun 12 '26
[removed] — view removed comment