Remix.run Logo
alex_x 4 hours ago

I wonder why all these easy-to-learn languages use indentation to denote scope, not something like curly braces. Isn't it actually harder to explain?

Wowfunhappy an hour ago | parent | next [-]

Fifth grade teacher here. Significant whitespace is a major reason I prefer Python for teaching programming.

1. I want kids to indent their code anyway; they may not realize it (or won't admit it), but this makes the code much easier for them to read. Kids will not do this unless they have to.

2. Unbalanced brackets are a major source of mistakes and confusion for my students. Relying purely on indentation resolves this problem—at the real cost of introducing indentation mistakes, but since I want kids to indent their code anyway, this is okay.

By the way, an adjacent recommendation is to configure the editor to indent with tabs instead of spaces (regardless of how you feel about tabs vs spaces in production code). Otherwise, kids will invariably end up with lines indented by 3 or 7 or some other wacky number of spaces. If possible, highlight the tabs in a different color so the kids don't use spaces by accident.

talkingtab 12 minutes ago | parent | next [-]

Interesting point. I wonder if "easier for them to read" is too simple. I took "read" as in "read words" or "read a book". But "reading" a program is not I think the same as reading words. Reading words could be this:

for i = 0 i < 10 i++ if i = 7 printf("hello 7") else printf("who are you");

But with a more pictorial presentation, it is easier to read the program.

for i = 0 i < 10 i++ if i = 7 printf("hello 7")

I'm just wondering - if we had a more pictograph based programming language would it be easier to understand?

Wowfunhappy 3 minutes ago | parent [-]

I'm not sure what you mean by your example code, I wonder if some formatting got clobbered by HN? But—

> if we had a more pictograph based programming language would it be easier to understand?

Well, we have Scratch, and it's excellent. The question is what do you do as the next step after that. I happen to like Python as a first text-based language.

christophilus an hour ago | parent | prev [-]

This is the job of a tool like go fmt. Obviously, it’s good discipline to indent, but I wouldn’t choose this as the deciding factor for picking a first programming language.

eddieroger 17 minutes ago | parent | next [-]

Formatters and linters fix the mistakes made by people who know what they're doing. They do nothing to teach someone how to do something for the first time in a way that supports comprehension, only regurgitation.

Wowfunhappy an hour ago | parent | prev [-]

go fmt can fix #1, but not #2, and won't work if #2 is causing problems.

NooneAtAll3 an hour ago | parent | prev | next [-]

while everyone already pointed out that this time it's not the case, I want to literally answer the question you asked

"easy-to-learn languages" use indentation because otherwise newbies would not indent at all

I you try teaching programming, you'll find that indentation is one of things students "optimize out" - it is not important to the program, it is opposite of lazy and it's not noticeably harmful on the tiny scale of programs you learn programming from

Indentation discipline only starts to matter when you need to work on the same code for quite some time and code itself takes a lot of space - the "read more then written" situation. And most study paths do not encounter this regime

jim_lawless 3 hours ago | parent | prev | next [-]

It looks like MiniScript uses the keyword "end" followed by another keyword to denote the end of a specific type of block.

From the Quick Reference guide here:

https://miniscript.org/files/MiniScript-QuickRef.pdf

"Indentation doesn't matter (except for readability)."

graemep 3 hours ago | parent | prev | next [-]

I think indentation is more intuitive. Even people using languages that use braces or similar usually use indentation to make code readable. If doing that you end up explaining both ideas (use braces and indent).

Gormo an hour ago | parent | next [-]

There's an important form/function distinction here, though. Indentation is useful for human readability, but braces function to give unambiguous direction to the compiler or interpreter. I think conflating these two different purposes together is a mistake: you shouldn't risk altering or breaking the logic flow of a program simply by adjusting its visual formatting.

The fact that we use whitespace for layout is precisely why it's a bad idea to assign it semantic value. I'm a fan of both braces and semicolons for that reason.

Wowfunhappy an hour ago | parent [-]

I think this is probably correct for an experienced programmer but incorrect for someone who is new.

Gormo 38 minutes ago | parent [-]

I'm not sure I understand what you mean. How would the friction inherent in conflating layout and semantics together depend on the experience level the programmer? Different programmers might have different ways of dealing with that friction, but I'd think its existence would be a property of the language itself.

Wowfunhappy 33 minutes ago | parent [-]

The form/function distinction you're making requires the ability to hold two parallel representations of the same code in your head—the visual representation (what it looks like) and the syntactic representation (what it means to the parser), and to know that they're related but different. This is a higher level skill.

When you're starting out, the best form to express to other humans is probably the one you're expressing to the computer. This isn't literally true—I don't think beginners should write in assembly—but it's true enough that they probably shouldn't mess with indentation beyond what would naively follow from bracket placement.

latexr 3 hours ago | parent | prev [-]

I get why people like indentation for this. I don’t think there’s a right or wrong answer and it’s a matter of personal preference.

That said, my preference is curly braces (or whatever) because I’ve found indentation is often a bother. Yes, most of the time you use indentation together with braces, but not every time. There are many occasions where code is clearer without (or with custom) indentation. Furthermore, indentation-based parsing makes experimentation and finding issues more difficult. Sometimes you need to extract a small part of a larger block to bung in a REPL or something and now you’re fighting with stupid errors because of formatting, adding to the frustration.

Regarding intuitiveness, for beginners I have some doubts it makes much of a difference, and if it does I also doubt indentation wins. If you know how to write (which is a prerequisite), you know what parenthesis and quotation marks are, you understand they encapsulate something separate from the rest. Indentation is a different concept.

btreecat 2 hours ago | parent [-]

I get why people blame indentation like this. I don't think it's right or wrong to ignore the tooling that directly addresses minor issues with indentation or matching braces honestly.

That said, my preference is to use the tools built into my editor and available on the CLI or web to assist and fix formatting and syntax. You get instant feedback on incorrect formatting, and I generally find that synthetic scope mistakes (regardless of method) are eliminated.

layer8 2 hours ago | parent | prev | next [-]

The language (MicroScript) doesn’t require indentation, it’s only used for readability, like in BASIC, FORTRAN, PASCAL, and similar languages. Blocks are delimited by key words (“end if” etc.).

echoangle 4 hours ago | parent | prev [-]

It makes sure the thing you use to judge scope (indentation) matches the think the computer uses.

alex_x 3 hours ago | parent [-]

That's a fair point for students, but as a beginner who simply wants to tinker with fun stuff, you can go very far without knowing of a program stack.

I think I had the wrong audience in mind