2008-08-29

A new brace style for C-like languages

Today I came up with a brand new brace style, heavily influenced by Python. It is like K&R, but with the closing brace appended to the last line of the block. It looks like this (the example code is C# that targets CLR v 1.1):

using System;
using System.Collections;
using System.Reflection;

namespace Utils.Documentation {

public class TypeDocumenter {
   private Type _type;
   private MethodInfo[] _methods;

   public TypeDocumenter(Type type) {
       Debug.Assert(type != null);
       _type = type;
       _methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly); }
  
   public MethodDocumenter[] Methods {
       get {
           ArrayList retval = new ArrayList();
           foreach (MethodInfo m in _methods) {
               if (! m.IsSpecialName) {
                   retval.Add(new MethodDocumenter(m)); }}
           return (MethodDocumenter[]) retval.ToArray(typeof(MethodDocumenter)); }}

   public DocumentationParser XmlDocumentation(DocumentationReader docreader) {
       Debug.Assert(docreader != null);
       return new DocumentationParser(docreader.GetXmlDocumentationForType(_type)); }}}

This style feels and looks very natural to me, and I am going to start using it for all C# code I write from now on (which is very little - I do 90% of my development in Python).

I use a combination of VIM and Visual Studio .NET when developing C# code. The VIM editor handles this brace style very naturally. I haven't had time to tinker with VS.NET's indentation settings yet.

Notice that the namespace declaration has the same indent level as the class declaration. I have always done this when programming C#, since the horizontal whitespace used for indenting a class does not add any information.

Previously I used the Allman/Emacs brace style, BTW.