Lerc
10 hours ago
Having done a fair degree of programming in Wirthwhile languages, I think the only main design decision that I think was a mistake was the variables at the top.
I'm not sure of the value of seeing all of the variables used listed in one place, it has certainly led to me encountering a identifier scrolling up to determine the type then scrolling back down. When the variable is only used on a few consecutive lines it's just adding work to read and adding work to create. I daresay I have written harder to read code than I intended because I didn't want to go up and declare another variable for short term use. The temptation to inline the expression is always there, because you know what all the parts mean when you write it. It's only when you come back later that you get the regret.
It's possible it could be mitigated by defining something like (not sure if this is a valid grammar)
stmt_sequence = {decl_sequence}. stmt {";" stmt} [";"].
and bring in scoping on statement sequences. maybe call it stmt_block so that stmt_sequence can be a component that really is just a sequence of statements.munificent
7 hours ago
> I'm not sure of the value of seeing all of the variables used listed in one place
It means the compiler knows how much memory the function's activation frame will take and the offset into that for every variable before it encounters any code in the function.
Basically, it makes it easier to write a single-pass compiler. That was important in the 70s but is less important these days.
Rochus
3 hours ago
This design decision makes compiler implementation easier and especially enables single-pass compilation. Later Oberon versions at least supported more than one declaration section in arbitrary order, but still no in-place declarations.
gingerBill
4 hours ago
When I write the backend (this repo isn't even 24 hours old yet), you'll find out why variable declarations are at the top of a procedure. (Hint: it has something to do with the stack).
foldl2022
9 hours ago
A nice side-effect of "variables at the top": you keep your functions short.
Turskarama
8 hours ago
"Functions should always be short" is also one of those guidelines that people treat like a hard rule. There are occasions when a 100 line function is easier to read than 5 20 line functions, or god forbid 20 5 line functions.
Stop being overly dogmatic, it ALSO leads to worse code.
lenkite
28 minutes ago
There are occasions => There are only rare occasions.
troupo
3 hours ago
Wirth was obsessed with the idea of creating the absolutely minimal useful language, and many of his languages' warts come from that.
Variables are at the top because:
- you immediately see them (so, perhaps, easier to reason about a function? I dunno)
- the compiler is significantly simplified (all of Wirths' languages compile superfast and, if I'm not mistaken, all are single-pass compilers)
However, I feel that Wirth was overly dogmatic on his approaches. And "variables must always be at the top" is one of those.
gingerBill
an hour ago
This has nothing to do with "dogma" and something simpler. It has nothing to do with "immediately see them".
Hint: This about this from a single pass compiler basis and how much memory needs to be reserved from the procedure's stack frame.