C-Style Formatting

by msimpson 10/29/2009 4:54:00 AM
I figure what the world definitely needs is more discussion about whether, in C-style programming languages, to put braces on the same line or on the next line.  Or maybe I just don't have anything better to do today.  Regardless, without further ado let me describe my preferred formatting standard:
 
  1. Use a monospace font.
  2. Use tabs instead of spaces, with a tab size of 4.
  3. Separate methods, properties, functions, etc. with one blank line.
  4. Put opening braces on the same line as the construct, separated from it by one space.
  5. Put each closing brace on its own line.
  6. Always include braces for statements that potentially define code blocks, even if the code block is only one line.
  7. Place one blank line before a statement that opens a code block, except when the previous statement opens the containing code block.
  8. Place one blank line after the close of a code block, except when the following line closes the containing code block.
  9. Put one space between comment-declaration symbols and the actual comments.
  10. Put one space between for, if, and similar keywords and the parenthesized expressions that follow them.
  11. In a for expression, put one space after each semicolon.
Here's an admittedly contrived example:
 
using System;
 
namespace Simpson {
  public class Foo {
  public static void Main(string[] args) {
  var x = new Random().Next(3);

  if (x == 0) {
  Console.WriteLine("Foo!");
  } else {
  if (x == 1) {
Console.WriteLine("Baz!");
  } else if (x == 2) {
  Console.WriteLine("Bok!");
  } else {
  Console.WriteLine("Bug!");

  if (args.Length > 1) {
  Console.WriteLine("Arguments:");

  foreach (var arg in args) {
  Console.WriteLine("\t" + arg);
  }
  }
  }
  }
  }
  }
 
The biggest battle seems to be over placement of curly braces.  Here’s my justification for doing it the way I do:
  • Logically, the opening curly brace is not a statement.  It’s part of the statement that opens the code block, and shouldn’t go on a separate line by itself. 
  • Since I never put multiple statements on one line, there’s never any code to the right of a curly brace.  Therefore, putting it at the end of a line does not displace any other code.  By contrast, putting a brace on the next line pushes all the code after it down by a whole line.  Therefore, you will fit more code on the screen at once if you put braces on the same line.
  • Fitting more code on the screen at once makes it easier to see blank lines at a glance, which makes them more effective as tools for visually grouping logically-related code.
  • Putting braces on the same line makes the code wider and shorter, whereas putting them on the next line makes it narrower and taller.  The former style fits better on new widescreen monitors.
  • Code with lots of braces can get very tall and sparse if you consistently put braces on the next line.  So in these cases, sometimes people will break their own rules and put the braces on the same line.  It seems easier to me to be consistent if you always put braces on the same line, rather than do the opposite and be tempted to break the rule now and then.
  • Fitting more code on the screen at once can allow for a larger font size when you ‘zoom out’ to show the whole file (assuming your editor can do this).
  • Finally - in Javascript, putting opening braces on the next line can lead to errors, due to Javascript’s inference of statement termination from carriage returns.  For example, this:
return
{
    age: 12
}
 
produces different results than this:
 
return {
    age: 12
}
 
In a function, the first one returns ‘undefined’ (probably not the intended result), whereas the second one returns an object with an ‘age’ property set to 12.  In Javascript, it’s good defensive coding to always use semicolons to terminate lines, and to always put opening braces on the same line as the construct.  And you’ll benefit by adopting the same standard for all C-style languages – making it a habit will reduce the mental cycles you’ll have to spend thinking about it.
 
As far as I can tell, the only reason people put a curly brace on the next line is to align it with the closing brace, so as to save a few brain cycles when reading the code.  But the statement that actually opens the code block, and “owns” the opening brace, is still aligned with the closing brace.  All it takes is a little practice to retrain your brain to visually associate code (the opening statement) with the closing brace, and I maintain that the benefits of the same-line style far outweigh the effort needed to retrain yourself.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , , ,

Software

Related posts

Comments are closed

Powered by BlogEngine.NET 1.2.0.0
Theme by Mads Kristensen


Calendar

<<  August 2010  >>
MoTuWeThFrSaSu
2627282930311
2345678
9101112131415
16171819202122
23242526272829
303112345

View posts in large calendar

Pages

    Recent posts

    Recent comments

    Archive

      Disclaimer

      The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

      © Copyright 2010

      Sign in