Whitespace is Significant


While I was in my first couple years of college, Python was gaining traction as a hot new programming language. I had been programming in C since high school, but was curious about new languages. I took an interest in learning more about Python. A classmate told me that whitespace (at least indentation) was significant in Python. I changed my mind.

The Holy Wars of Indentation Style


// From https://en.wikipedia.org/wiki/Indent_style
// K & R
while (x == y) {
something();
somethingelse();
}
// 1TBS
if (x < 0) {
puts("Negative");
} else {
nonnegative(x);
}
// Stroustup
if (x < 0) {
puts("Negative");
negative(x);
}
else {
puts("Non-negative");
nonnegative(x);
}
// Allman
while (x == y)
{
something();
somethingelse();
}
// GNU
while (x == y)
{
something();
somethingelse();
}
// Whitesmiths
while (x == y)
{
something();
somethingelse();
}
// Horstmann
while (x == y)
{ something();
somethingelse();
}
// Pico
while (x == y)
{ something();
somethingelse(); }
// Ratliff
while (x == y) {
something();
somethingelse();
}
// LISP
while (x == y) {
something();
somethingelse(); }

view raw

indent.c

hosted with ❤ by GitHub

It’s interesting that would’ve been so turned off by a language that placed significance on whitespace and indentation, when I had fantasized about holy wars revolving around C indentation style (Horstmann is the only way!) and tabs vs. spaces (tabs, tab width 4!). I wretched at anything in the GNU C coding style (okay, I still do…), but the funny thing was, I never was able to program in a consistent style: There were other co-workers who had a slightly different preferred style. I was also learning myself, so relative importance of information in code changed, and my opinions on bracing style evolved.

Non-C Languages that Use Curly Braces for Things

It was a combination of Java, JavaScript, Perl, and Ruby that altered my perception of “proper” bracing style (BUT NOT SPACES OVER TABS!). A new line after a conditional or function declaration/call before the brace either took up unnecessary amounts of space, or changed the meaning of the code altogether (e.g., blocks in ruby, for which adding a newline = syntax error). And so, I evolved into declaring that 1TBS style was the only proper way to write C (WITH TABS!)

Ruby

Ruby has a generally accepted coding style that is enforced by no one except for code reviewers and maybe draconian RuboCop build configurations. I’d link to the coding style, if I had any certainty that the one I grabbed was 99% correct. Even RubyMine doesn’t format code to the same standards as RuboCop.  (Continuation indent and spacing around braces were two settings I had to tweak.)

Small inconsistencies aside, Ruby code has ended up being the most consistent—visually—to support and add code to. I’ve introduced tighter RuboCop configurations on newer projects to try to adhere more closely to generally accepted coding style for Ruby.

Ruby also introduced me to an acceptance of significant whitespace. While C code often gets sloppy with spacing around punctuation markers in code, Ruby developers take more care in proper whitespace around the conditionals, braces, etc. Some C/C++ projects like Mozilla do similar standards. However, Ruby is the first language that I’ve seen a community-wide interest in code that presents in the same manner. Of course, most of this code isn’t syntactically significant, but it is of human significance–and humans have their own mental compilers that optimize on standard patterns.

Still, this isn’t the largest impact Ruby has had on my opinion of significant whitespace. Through Ruby, I’ve been exposed to YAML, HAML, and Slim. (I have dibs on forming a band of Rubyists with that name!) All three languages are unforgiving about inconsistent indentation. They take out explicit expression of boundaries and create visually implicit boundaries via hierarchies of indents. You don’t need to look for an end or closing brace to know that the context has moved up a level. The indentation tells you where that happens.

(And yes, because of the generally accepted coding style in Ruby, I use 2 spaces instead of tabs now.)

Golang

And yet, Ruby coding style is informal. You can code in improper style and still commit your code and/or run it. There’s something about having the freedom to write improper style and being able to be a good “citizen” on your own that is comforting.

Go does not do this. Writing Go in any editor configured to code in it (I’ve been using the vim-go plugin) automatically formats via gofmt.  It’s pretty unsettling. I longed for the ability to become dictator over coding styles, and then was okay when Ruby dictated one, but gave us the autonomy to do the right thing. This auto-formatting? This is tyranny!

But, perhaps, it’s time. We can mentally chunk pieces into larger concepts for storage, if code that does the same thing looks the code. Instead of having to remember every brace as an individual piece, we can put “for loop that iterates and prints” as a single chunk.

A stray brace isn’t going to permanently prevent distillation into larger chunks; it will just take a higher cognitive load to get there. This in turn will require more experience with the language before a programmer can make that leap. Being able to break things into large chunks helps us discover the what instead of dwelling on the how.


%d bloggers like this: