Curly Braces: New Line, Same Line, or Some Other Style

Dice-K
2 min readMay 30, 2021

Curly braces are important to group lines of code. With something important as grouping code comes many ways to style the curly braces. Always choose the styling that fits the situation. For example, if you are creating a project for yourself then you can use any style. If you are on a team then follow a style that the team agrees on. If a project already has a code base, use the style used in the code. In any style, be consistent!

It would look unorganized if the code looks like this even though the function bodies are one line of code.

I consistently use the new line method because of the readability.

If by chance someone looks at my code, I want my code to be readable and would help that someone understand the code easily.

The same line method will let programmers see more of the code without scrolling through the page.

The K&R method is where the curly braces of the function block is at the same indentation as the header. Anything inside, the curly braces will be on the same line.

I follow these rules when using curly braces:

  1. If on a team, agree on a style.
  2. If given a code base, follow the styling of the code.
  3. If doing a self project, choose any style.
  4. BE CONSISTENT!

The last rule particularly I follow. It would show you as being unorganized if you switch styles all over the code.

--

--