The Case Against Curly Braces {} Languages

C, C++, Java, C#, Go, JavaScript and many more languages use curly braces to group statements or declarations. But is this a good choice?

Image for post
Image for post

kay, what the hell. In this story I will argue against one of the most popular syntax conventions by far in the programming world. Programmers love their curly braces. And few new programming languages dear break with this tradition firmly established with the C programming language.

C gained massive popularity early and C++ piggy backed on this popularity. Then came Java which wanted to also piggy back on this success and thus adopted a similar syntax. The huge success of Java caused Microsoft to make their own Java clone, which later became C#. And over at Netscape they where making a LISP or Scheme language, meaning lots of parenthesis, but for marketing reasons it got a last minute remake and got curly braces too.

Bottom line is that curly braces is a bit of an accident of history. I will here try to make the case that using keywords to denote scope like Pascal, Algol, Lua, Ada, Basic and Matlab to name a few examples is better.

In many of these languages begin end marks a scope.

Image for post
Image for post

Blocks and their indentation level is easier to identify because begin end is lager and more visible markers than { and }.

But this is not really the main issue. It is that braces must be able to make a case for why they are needed. Why? Because different forms of brackets and braces are of outmost importance in programming languages and we are in limited supply of them. We need them for the following:

  • Grouping arithmetic expression. Most use ( and ). (2 + x) * 4
  • Grouping array elements and perform array access. Most use [ and ]. x = array[4]
  • Specifying type parameter. vector<int> items.

In the latter case all the curly braces language have been forced to adopt < and > despite these brackets being a terrible choice. Why?

These symbols are already used in mathematical expression. In e.g. Julia this would be a no-go, because type parameters are specified as expressions in which the < and > operators are perfectly valid.

The code below creates an empty array where the elements are of type Int.

xs = Vector{Int}()

The type parameter is inside curly braces. However in Julia it is perfectly valid to make the type parameter a more complex expression:

ys = Vector{x > 0 ? Int : Float64}()

What we can see from this is that using < and > for the type parameter like C++ would fall flat on its face.

ys = Vector<x > 0 ? Int : Float64>() # BAD choice!

Try figuring out from the mess above where the type parameter begins and ends. My point with this example is that you can have many reasons to need braces for something in your language, thus we should not waste them where they are not needed.

Parenthesis are naturally a good fit for mathematics compared to begin and end.

(3 + 2) * (4 - 2(2 + 1)) 
begin 3 + 2 end * begin 4 - 2 begin 2 + 1 end end

Yes nobody would want to use keywords for nested calculations. What about square brackets?

xs[2 + ys[3]]
xs begin 2 + ys begin 3 end end

Okay, I know, I am pointing out the bloody obvious. You really want your parenthesis in these cases.

However for scope we are in an entirely different situation. We almost never nest on the same line. Quite the contrary be almost alway prefer to mark the start and end of a scope on a separate line.

Thus in your typical C and JavaScript code we end up lots of lines with only a single character. This is not prime real estate. We don’t need curly braces to cut down on typing. In fact they tend to waste typing.

while i < 10
print(i)
i += 1
end

Let us compare the Julia code above to the C code below in terms of typing efficiency.

while (i < 10) {
print(i);
i += 1;
}

Ironically the C example requires more typing on the keyboard. Let us tally up:

  • Each brace requires holding down a shift and a braces key. Since we have two braces that is 4 keys you need to touch. In Julia you only type end , as while also marks the beginning of the block. Julia wins with 1 keystroke.
  • Each line needs a semicolon at the end. 2 keystrokes Julia doesn’t need.
  • Expressions in if, for and while-statements need parenthesis. Each require holding down shift. 4 keystrokes Julia doesn’t need.

Thus we end up with 7 more keystrokes on the C solution. And does that gain us any clarity or clear benefit? I would say no.

The space saving at the last line is of no value as there are no other characters there. In fact the single character makes the end of the code less visible.

Recap

Syntax choices have to have rational reasons for existing. Benefits must be weighed against downsides. The way modern software is written cannot demonstrate any advantages.

  1. They don’t save keystrokes, nor do they save any precious screen real-estate as they typically have a whole line to occupy all by themselves.

2. They don’t enhance clarity of code.

However they to cause serious disadvantages. We have very limited number of braces to choose from, thus they must be rationed for the most important cases such as mathematical expressions, array and dictionary access. Finally a lot of modern languages have some form of type parameterized types. Julia is one example of this.

Using < and > is somewhat of a desperate measure, when you have run out of choices.

I know this is an unpopular opinion as many people are in love with C/C++ syntax. They want curly braces for scope and they want < > for parameterized types. Yet apart from familiarity do these choices offer any advantages? In my opinion they have a number of clear disadvantages which should carry more weight than familiarity.

After all we have large family of languages using the Pascal style block marking.

While I love feedback, keep in mind that this opinion of mine is no threat to your current most beloved curly braces programming language. Rather I hope to convince you to not think of language which don’t use curly braces as somehow being second rate and not worthy of your attention. In fact these langauges most likely made a more intelligent choice 😉.

Written by

Geek dad, living in Oslo, Norway with passion for UX, Julia programming, science, teaching, reading and writing.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store