Experienced Points

Experienced Points
Undertale and Curly Braces

Shamus Young | 29 Feb 2016 12:00
Experienced Points - RSS 2.0

Languages like C use braces for defining code blocks, languages like Python just use indentation. My question is: what is the purpose of braces? Is it just historical? If you were designing a language from the ground up would you use them? Same question applies when it comes to semi colons. I really don't see why this...

[pre]if(thing > 10) {
print("Thing is bigger than 10.");
}[/pre]

...would be better than this:

[pre]if thing > 10:
print("Thing is bigger than 10.")
[/pre]

Frank

For the non-coders: The C family of programming languages are probably the most common and influential in the world. Most of the software you use in your day-to-day life is written in some variant or descendant of C.

programming

In C, you put an open curly brace { to begin a block of code and the close brace } to end it. Some modern languages have abandoned this convention. In a curly-brace language, the braces are for the compiler, because it doesn't care about the spaces. The spacing is entirely for the benefit of the programmer: We indent things so they're easier to read. So it kind of makes sense to do away with this system: Why have one convention for the computer and another for the human? Why don't they both use the same information?

Second thing non-coders need to know: There's an ever-lasting debate on the use of tabs versus spaces. If you need one block of code to be indented a bit, then you can hit the spacebar a few times, or you can press the tab key. Then you save your source code (which is just a text file) and another programer opens it. But they are from the other side of the tabs vs. spaces war, and so the code is super-annoying to navigate. Your cursor goes through spaces one at a time, but "jumps" through tabs. If you're editing code using the "wrong" system, you'll constantly be making typos and navigation errors.

First, the historical reasons for this mess: The C language is a descendant of the now-dead B programming language, which descended from BCPL, which was the first language to use curly braces. I imagine braces were simply convenient for the person who invented the language and wrote the compiler. These languages can all ignore "whitespace." Tabs, spaces, line breaks, and all other non-printing characters can be treated the same. Having one space between two words is the same as having a hundred spaces, five tabs, and two line breaks. As long as there's something there, we know we're dealing with two words and not one.

I know we think of "coding" as something we do in one of these sexy modern editors with moveable windows, autocomplete, code highlighting, auto-indent, auto-format, code lookup, and a dozen other conveniences. But back when BCPL was invented in 1966, our text editors were abominable. Without auto-indent, the only way to make sure the code lines up is to count as you smack the spacebar over and over. Or you could use tabs, which brings in all sorts of terrifying questions about how tabs will look and behave on different systems (an important point when every company used a different operating system) and how the compiler will interpret them.

And I think this ambiguity is a big reason the convention is still with us. What if one line of code begins with a two tabs, the next with four spaces and a tab, and the next with eight spaces? One text editor will show that as a block of perfectly aligned code, while another will show the lines as "ragged." What if the compiler interprets things differently than your text editor of choice? It will claim there's an error that you literally can't see with your fleshy human eyeballs.

You could say to the programmers "don't use tabs" or "only use tabs" or "only use this text editor," but then your language is going to have a very hard time gaining users. Coders will gladly learn a whole new language, but they'd sooner sell their children than give up their text editor of choice. And no matter where you stand on the spaces-vs-tabs debate, once you've been doing it for a couple of decades, you're going to have a very hard time teaching your fingers to do something different.

(Have a question for the column? Ask me!)

Shamus Young is a programmer, critic, comic, and survivor of the dot-com bubble.

RELATED CONTENT
Comments on