Home > Blog > Coding standards

Coding standards

December 30th, 2004

Each developer has its own view on coding standards. What about you ?

Do you prefer this syntax :
[java]int arrayIndex = 1 ;
boolean isOk = true ;

if (isOk)
{
doSomething() ;
}
else if (1 == arrayIndex)
{
doSomethingElse() ;
}

public static boolean contains (int[] anArray, int aValue)
{
for (int i = anArray.length – 1; i >= 0; i–)
{
if (anArray [i] == aValue)
{
return true ;
}
}
return false ;
}[/java]

or this one :
[java]int array_index=1;
boolean isOk;
isOk=true;
if (isOk == true) {
do_something();
} else if (array_index==1) {
do_something_else();
}

public
static
boolean
contains (int[] array, int value) {
boolean found;
for (int i=anArray.length-1; i>=0; –i) {
if (aValue==anArray [i]) {
found=true;
break;
}
return found ;
}[/java]
I’ve been using the former for a long time now (on JAVA and C/C++) and I couldn’t change my habits so easily.

  1. Anonymous
    December 30th, 2004 at 16:01 | #1

    A Combination of both.

    public
    static
    boolean
    contains (int[] array, int value) {

    Is just plain scary. Why put all those all on a separate line?

  2. December 30th, 2004 at 16:18 | #2

    Er.. use a tool like IDEA which can auto-format the code according to your preferences. If you don’t like the coding style, just do a re-format.

  3. David Gageot
    December 30th, 2004 at 16:31 | #3

    I’v seen this syntax:

    public
    static
    boolean
    contains (int[] array, int value) {

    with one modifier on each line on some open-source projects. It frigthened me also! This was a reason to put this example in my post.

  4. David Gageot
    December 30th, 2004 at 16:50 | #4

    An IDE is not enough to enforce coding standards. If you look at the contains method, you can have multiple return or only one. A formatting tool won’t change this. It is commonly accepted that single return point is better but I sometimes find it more difficult to read.

  5. January 4th, 2005 at 00:17 | #5

    Maybe you saw something like that multi-line signature syntax on my open-source project :-) .


    public static
    boolean
    contains(
    int[] array,
    int value
    )
    {
    ...
    }

    I find this much easier to read than the one-line form. First line is modifiers, second is return type, third is name. Params each on their own line like a bullet list; no wondering about how to format a long param list.

  6. January 5th, 2005 at 13:28 | #6

    Sorry, it is not the case that “It is commonly accepted that single return point is better”. I have blogged about it here:

    http://www.jroller.com/page/matsh/20031216

  7. David Gageot
    January 6th, 2005 at 18:20 | #7

    I really think it is commonly accepted that single return point is better but as I said in the post, I prefer the first code with multiple return points. :-)

Comments are closed.