Code Style

doob doobnet at gmail.com
Sat Jun 16 05:23:20 PDT 2007


This is what I use in eclipse now for java and it's slightly modified:

/**
 * Group imports by library with a 
 * blank line between and a blank line before 
 * the next statement
 */
import lib1.foo;
import lib1.foo2;

import lib2.foo3;
import lib2.foo4;

/**
 * Indentation
 * a space before and after colon when extending or implementing
 */
class Example : Object
{
	// group variables by type with a blank line between if many and many of the same type
	int[] myArray = [1, 2, 3, 4, 5, 6];

	int theInt = 1;
	int a;
	int b;

	char[] someString = "Hello";
	char[] str1;
	char[] str2;

	// use braces and not the colon style
	private
	{
		double aDouble = 3.0;
		double c;
		double d;
		
		// property variables ending with a underscore and then with the same name on the functions names except the underscore
		char[] text_;
		char[] text2_;
	}

	const FOO = 0;
	const E = 1;
	const F = 2;



	 /*
	  * Properties
	  */

	 char[] text ()
	 {
	 	 return text_;
	 }

	 char[] text (char[] text)
	 {
	 	 return text_ = text;
	 }

 	 char[] text2 ()
	 {
	 	 return text2_;
	 }

	 char[] text2 (char[] text2)
	 {
	 	 return text2_ = text2;
	 }



	 /*
	  * Methods/functions
	  */

	/*
	 * A space before opening parenthesis, a space after commas,
	 */
	void foo (int a, int b, int c, int d, int e, int f)
	{
		switch (a)
		{
			case 0:
				Other.doFoo();
			break;		// break on the same indentation as the case

			default:
				Other.doBaz();
		}
	}

	void bar (List v)
	{
		for (int i = 0; i < 10; i++)
		{
			v.add(new Integer(i));		// no spaces before opening parenthesis in function/method calls
		}
	}
}

enum MyEnum
{
	UNDEFINED (0)
	{
		void foo ()
		{
		}
	}
}

/**
 * If...else
 */
class Example
{
	void bar ()
	{
		do
		{
		} while (true);		// while on the same line as the closing brace


		/**
		 * Simple try, catch and final if possible
		 */
		try
		{
		}

		catch (Exception e)
			writefln(e.toString);

		final
		{
		}
	}

	/**
	 * Blank line after statements, simple if if possible
	 */
	void foo2 ()
	{
		if (true)
		{
			int i = 0;
			return;
		}

		if (true)
			return;

		else if (false)
			return;

		else
			return;
	}

	void foo (int state)
	{
		if (true)
			return;

		if (true)
			return;

		else if (false)
			return;

		else
			return;
	}
}

/**
 * This order in a module or class:
 * imports
 * blank line
 * class
 * variables
 * 3 blank lines
 * constructors
 * 3 blank lines
 * getters/setters/properties
 * 3 blank lines
 * functions/methods
 * 3 blank lines
 * listeners/inner classes
 * 
 * no line wrapping
 */

I have not commented every thing but the sample should give a quite clear idea



More information about the Digitalmars-d mailing list