Formatted Version of C# Code

This page contains some Badly Formatted C# code, and its C# Formatted version. The output was produced by our C# Formatter.

Badly Formatted C# Code

This code is a sample taken from the C# graphics library. The original was was well formatted; we have edited the code to simulate formatting choices over time by a number of programmers with uneven aesthetics. Most programmers will have to reformat this code before they would be willing to work on it. There goes half an hour of productivity.


/*
 * BSD Licence:
 * Copyright (c) 2001, Lloyd Dupont (lloyd@galador.net)
 * 
 * All rights reserved.
 *
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the  nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */
using antlr;
using   antlr.collections;
      using System;
using System.Collections;
using System.Globalization;
using System.IO         ;
using System.Text;

public class GLType
{
 public string typeName;               public int stars;

	public                   GLType(string GLType     )
	{		typeName = GetCSType       (GLType);
                                 }

	/** return the generation specific level of this type.
	 *  roughly equal to the number of polymorphic type
	 * it could have. <br>
	 * <b>Note</b> a return type has a maximum of 3 level, anyway. */
           public int Level
    {
		get {
if(stars  ==  0)	return 1; if(stars==1 && typeName == "void") return 10;
		return 3;
		}
	}

	public
bool
IsVoid
{ get
{
return
 stars==
 0
&&
 typeName == "void"; } }

	/** return the C# representation of a type for a given level.
	 * @param isReturn tell wether or not it is a return argument as
	 * the representation are different
	 * @param codeFriendly to generate a compilable name for CFunction
	 * name. */
	public string ToString(int level, bool isReturn, bool codeFriendly)
	{
		if(stars==0)
			return typeName;
		if(level==0) // C - name
			return typeName+StarString     (codeFriendly ? 'P' : '*');
		if(stars>1 || isReturn || level==1)
			return "IntPtr";
		if(typeName=="void") switch(level) {
				case 2: return "byte[]";  case 3: return "sbyte[]";
				case 4: return "short[]"; case 5: return "ushort[]";
				case 6: return "int[]";		case 7: return "uint[]";
				case        8:
                                return "float[]";	case 9: return "double[]";
			}
		return typeName+"[]";
	}
	string StarString(char c)
	{
		StringBuilder sb = new StringBuilder();
		for(int i=0; i<stars; i++)
			sb.Append(c);
		return sb.ToString();
	}
	public bool IsPointer { get { return stars>0; } }
	public int Size
	{
		get {
			switch(typeName) {
				case "void":
					return 0;
		case "byte":
				case "sbyte":		return 1;
                                     case "short":
                                          	case "ushort":
					return 2;
                                        		case "int":
				case "uint":
					return 4;
				case "float":
					return 4;
				case "double":
					return 8;
				default:
					throw new ArgumentException("unknown base type");
			}
		}
	}
	static Hashtable typeTable;
	public static string GetCSType(string GLType)
	{
		if(typeTable == null)	typeTable = createTypeTable();
		string ret = (string)
                 typeTable[GLType];
		if(ret
                 ==
                  null) {
			Console.Error.WriteLine("warning: unknown type \""+GLType+"\" use as is.");
			typeTable[GLType] = GLType;
			ret = GLType;
		}
		return ret;
	}
	static Hashtable createTypeTable()
{
	Hashtable ret = new Hashtable();
		ret  ["void"] = "void";
ret[  "GLvoid"] = "void";
	ret["GLenum"  ] = "uint";
		ret["GLbyte"] = "byte";	ret["GLshort"] = "short";ret["GLint"] = "int";
ret["GLsizei"] = "int";	ret["GLubyte"] = "byte"; ret["GLuint"] = "uint"; ret["GLfloat"] = "float";
		ret["GLushort"] = "ushort";
		ret["GLclampf"] = "float";
		ret["GLdouble"] = "double";


		ret["GLclampd"] = "double";
		ret["GLboolean"] = "byte";
		ret["GLbitfield"] = "uint";
		return ret;
	}
}

C# Formatted Version

This is the result of using SD's C# Formatter tool on the sample badly formatted C#, using just the default settings. You can see that the formatter has chosen very different line breaks, based on the language structure. The block structure is now clearly visible. A programmer might actually be able to work on this version.


/*
 * BSD Licence:
 * Copyright (c) 2001, Lloyd Dupont (lloyd@galador.net)
 * 
 * All rights reserved.
 *
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the  nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */
using antlr;
using antlr.collections;
using System;
using System.Collections;
using System.Globalization;
using System.IO;
using System.Text;
public class GLType
  {
    public string typeName;

    public int stars;

    public GLType(string GLType)
      {
        typeName = GetCSType(GLType);
      }

    /** return the generation specific level of this type.
     *  roughly equal to the number of polymorphic type
     * it could have. <br>
     * <b>Note</b> a return type has a maximum of 3 level, anyway. */
    public int Level
      { get
          {
            if (stars == 0)
              return 1;
            if (stars == 1
                && typeName == "void")
              return 10;
            return 3;
          } }

    public bool IsVoid
      { get
          {
            return stars == 0
                   && typeName == "void";
          } }

    /** return the C# representation of a type for a given level.
     * @param isReturn tell wether or not it is a return argument as
     * the representation are different
     * @param codeFriendly to generate a compilable name for CFunction
     * name. */
    public string ToString(int level, bool isReturn, bool codeFriendly)
      {
        if (stars == 0)
          return typeName;
        if (level == 0) // C - name
          return typeName+StarString(codeFriendly ?
                                     'P' : '*');
        if (stars > 1
            || isReturn
            || level == 1)
          return "IntPtr";
        if (typeName == "void")
          switch (level)
          { case 2 :
              return "byte[]";
            case 3 :
              return "sbyte[]";
            case 4 :
              return "short[]";
            case 5 :
              return "ushort[]";
            case 6 :
              return "int[]";
            case 7 :
              return "uint[]";
            case 8 :
              return "float[]";
            case 9 :
              return "double[]"; }
        return typeName+"[]";
      }

    string StarString(char c)
      {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < stars; i++ )
          sb.Append(c);
        return sb.ToString();
      }

    public bool IsPointer
      { get
          {
            return stars > 0;
          } }

    public int Size
      { get
          {
            switch (typeName)
            { case "void" :
                return 0;
              case "byte" :
              case "sbyte" :
                return 1;
              case "short" :
              case "ushort" :
                return 2;
              case "int" :
              case "uint" :
                return 4;
              case "float" :
                return 4;
              case "double" :
                return 8;
              default :
                throw new ArgumentException("unknown base type"); }
          } }

    static Hashtable typeTable;

    public static string GetCSType(string GLType)
      {
        if (typeTable == null)
          typeTable = createTypeTable();
        string ret = (string)typeTable[GLType];
        if (ret == null)
          {
            Console.Error.WriteLine("warning: unknown type \""+GLType+"\" use as is.");
            typeTable[GLType] = GLType;
            ret = GLType;
          }
        return ret;
      }

    static Hashtable createTypeTable()
      {
        Hashtable ret = new Hashtable();
        ret["void"] = "void";
        ret["GLvoid"] = "void";
        ret["GLenum"] = "uint";
        ret["GLbyte"] = "byte";
        ret["GLshort"] = "short";
        ret["GLint"] = "int";
        ret["GLsizei"] = "int";
        ret["GLubyte"] = "byte";
        ret["GLuint"] = "uint";
        ret["GLfloat"] = "float";
        ret["GLushort"] = "ushort";
        ret["GLclampf"] = "float";
        ret["GLdouble"] = "double";
        ret["GLclampd"] = "double";
        ret["GLboolean"] = "byte";
        ret["GLbitfield"] = "uint";
        return ret;
      }

  }

For more information: info@semanticdesigns.com    Follow us at Twitter: @SemanticDesigns

C# Formatter
Example