pio3 - Fotolia

Why are curly braces in programming languages important?

Curly braces play a big role in code structure within popular programming languages such as Java, C++ and more. Here's how to properly line up code with curly brackets.

At their core, all programming languages share similarities. In their most basic form, they all break down into the same common sets of functions:

  • data variable declarations
  • conditional logic
  • iterative functions

Computer programs can use data, evaluate if-then conditions on the data and use extremely fast iterative loops to perform these functions. In software programs, these functions get organized into methods. Object-oriented languages further organize these functions into classes or objects. Regardless of whether a given programming language is object-oriented or procedural, these fundamental concepts still apply.

However, one major difference hinges on how developers use curly braces in programming languages.

Curly-brace code blocks

Different programming languages have various ways to delineate the start and end points of a programming structure, such as a loop, method or conditional statement. For example, Java and C++ are often referred to as curly brace languages because curly braces are used to define the start and end of a code block.

public void flagTest()
{
  boolean flag = true;
  int i = 10;
  if (flag == true)
  {
    for( int i=0; i<10; i++ ) 
	{
	   System.out.print("flag is true");
	}
  }
  flag = false;
}

10 popular curly-brace languages

The use of curly braces in programming languages dates back to 1966 with the Basic Combined Programming Language (BCPL). As BCPL grew in popularity, it then inspired the C programming language and its successor C++. C and C++ inspired Java, and now the entire ecosystem of peripheral JVM languages incorporate curly braces into their syntax. Some of the most prominent languages that use curly braces in programming include:

  1. Java
  2. C++
  3. JavaScript
  4. Rust
  5. Groovy
  6. Kotlin
  7. Perl
  8. PHP
  9. Scala
  10. Swift

Instead of curly braces in programming, some languages -- such as Python -- use tabs, indentation and carriage returns to delineate blocks of code.

Curly-brace code controversy

Junior software developers new to curly braces in programming usually prefer to have the opening bracket occupy an entire line of code minus the use of other syntactical elements. Students learning to code tend to find code structured with this 'new-line' approach easier to understand because the start and finish of individual code blocks are easier to identify.

New developers who place too much white space around curly braces telegraph their inexperience.

However, experienced developers prefer a 'same-line' approach, which means the curly bracket goes on the same line of code that initiates the new code block. Popular Java IDEs such as Eclipse, NetBeans and IntelliJ use this same-line approach as the default setting for code formatters. As such, to pass a standard code review, the 'new-line' code snippet above would need to be reformatted into the following 'same-line' syntax:

public void flagTest() {
  boolean flag = true;
  int i = 10;
  if (flag == true) {
    for( int i=0; i<10; i++ ) {
	   System.out.print("flag is true");
	}
  }
  flag = false;
}

The spacing, tabs and carriage returns that format the appearance of curly braces in programming have no impact on compilation. All that matters is that the curly braces exist and are well formed, which means that every open bracket has a matching and corresponding closed bracket. However, modern coding conventions dictate a more compact code structure.

New developers who place too much white space around curly braces, or choose to place open braces on independent lines of code, not only telegraph their inexperience, but their syntactical faux pas will likely raise the ire of other programmers on the team.

Next Steps

A guide to common variable naming conventions

Kebab vs. camel case: How these naming conventions differ

A quick look at the Carbon programming language

Dig Deeper on Core Java APIs and programming techniques