4 ms·
While I agree that you learn to treat them as symbols, it certainly elevates the barrier of entry. I know what “while” means in English, so when I’m reading cod
by e-clinton 6y ago
While I agree that you learn to treat them as symbols, it certainly elevates the barrier of entry. I know what “while” means in English, so when I’m reading code, there is less of a mental load to interpret that code. As a beginner, if I had to translate “while” or “throw” or “parse”, I’d make learning more difficult.
- mort96 6y agoIt's not that you would translate them though. You don't have to know what "while" means in english to use it. You just learn that the arbitrary symbol "while" is how you repeat something as long as a condition holds. There's not really anything language-specific about that, except that "while" happens to be a word in English with a meaning which somewhat correlates with what the symbol does in the programming language.
- jgwil2 6y agoCounterexample: as a beginner, I think knowing English made "for" harder to understand: for loops in C don't seem to bear much relation to the meaning of "for" in common usage: yes, there is a condition that initiates the loop, but what about the other two statements? For me it was actually something I had to de-program from my brain before I could understand how they actually worked.
- whatshisface 6y agoI think it's rooted in mathematical phrases like "for all x in the set S..."
- jgwil2 6y agoInteresting. I suppose you are probably right, but I would still argue it's confusing and doesn't actually line up with the semantics of the C-style for loop. If it were a language like Python, wherein collections can actually be iterated over with a "for ... in ..." statement, then it might make more sense. But the C for loop just has an initializer, a break condition, and a statement executed between loops, so there's not necessarily a connection to the mathematical notation, even if it's often used to execute some function over a collection.
- whatshisface 6y agoI was curious so I did a bit of historical research. The "for" loop comes from ALGOL[0]. In ALGOL, the FOR statement supports syntax like "FOR V:= AEXP1, AEXP2,... DO". That's like the higher-level iterator syntax, except it's assigning V via iteration over a sequence of expressions (AEXP N) that is fixed at compile time. That's the context where FOR makes sense as a term. (Step 1: thesis) Now, ALGOL also happens to support a FOR-STEP-WHILE format, which works the same way as the C for-loop[0]. That makes less sense from a mathematical nomenclature perspective, but it is justified through analogy to the original FOR definition. When C, which was based in part on ALGOL, implemented for, it dropped the original meaning and kept the special case meaning because the latter was more useful, being dynamic instead of fixed at compile time. Hence, the for loop. (Step 2: antithesis) Eventually other languages implemented for with iterators, returning to the original ALGOL meaning of for, but synthesizing it with the dynamism of the C for loop. (Step 3: synthesis) [0] https://public.support.unisys.com/aseries/docs/clearpath-mcp-18.0/86000098-516/section-000026445.html https://public.support.unisys.com/aseries/docs/clearpath-mcp... Disclaimer: references to the Hegelian dialectic are for entertainment purposes only.
- Y_Y 6y agoThat's the problem though, a C for loop doesn't really work like that. We only recently got something close with the "range-based for" in C++, and while python's list comprehensions do pretty much exactly what I think mathematical "for" should, it re-uses the keyword in different ways elsewhere.
- btilly 6y agoHow recent an innovation this is depends on your language. It was added in C++11 so it looks recent there. But the syntax was in Perl 4 back in 1991. And someone who knows programming history can probably find older examples still in other languages.
- moistbar 6y agoI think "for" works better linguistically when taken in the context of a "for-each" loop, where my mental pseudocode becomes "for each of these, do this." It's a touch Yodalike, but it gives me a clearer expectation of what's happening and how long it's going to happen than I get from a C-style for loop.
- dkarl 6y ago"while" describes itself particularly poorly for native English speakers. It implies a duration that continues until a condition changes, but really the condition is only checked at the beginning of every loop. English speakers have to get over the incongruity using a word for a contiguous period of time to indicate repeated checks at distinct points in time.
- nmeofthestate 6y agoJust speaking personally as a native English speaker, I don't remember ever having expended any effort to get over that incongruity.
- dkarl 6y agoI think it's something that only makes sense after the imperative model clicks. There are a lot of different ways that a beginner might think about what code means, and some beginners write code like this: while guessing == true: ... if is _correct(answer): guessing = false // This stops execution and jumps out of the loop, right? answer = next_guess() ... // answer should be correct now print('Correct answer is %s' % answer) The program says this block of code executes "while" guessing is true. How does it make sense that code inside the loop is still executing after guessing is set to false? For you and me, since our brains have been molded to the the imperative model of programming (execute this line, then this line, then this line,) we understand that "while guessing == true:" is a line of code that only has effect at specific times, "when the program gets to it." That is not what "while" means in ordinary language, so it's confusing for a beginner until the imperative model is really locked into their heads.