Geomajas Community Documentation

2.1.3. Code layout

See the example below

/*
 * This file is part of Geomajas, a component framework for building
 * rich Internet applications (RIA) with sophisticated capabilities for the
 * display, analysis and management of geographic information.
 * It is a building block that allows developers to add maps
 * and other geographic data capabilities to their web applications.
 *
 * Copyright 2008-2010 Geosparc, http://www.geosparc.com, Belgium
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.geomajas.bladibla;

/**
 * Short description of the purpose of this class.
 *
 * @author Author's name
 * @author Another Author's name
 */
@Annotation(param1 = "value1", param2 = "value2")
public class Foo implements Serializable {

    int[] x = new int[] {1, 3, 5, 6, 7, 87, 1213, 2};

    /**
     * Do something
     *
     * @param x some data
     * @param y more data
     */
    public void foo(int x, int y) throws Exception {
        for (int i = 0; i < x; i++) {
            y += (y ^ 0x123) << 2;
        }
        do {
            try {
                if (0 < x && x < 10) {
                    while (x != y) {
                        x = f(x * 3 + 5);
                    }
                } else {
                    synchronized (this) {
                        switch (e.getCode()) {
                            //...
                        }
                    }
                }
            }
            catch (MyException e) {}
            finally {
                int[] arr = (int[]) g(y);
                x = y >= 0 ? arr[y] : -1;
            }
        }
        while (true);
    }
}
  • The code is written with the right margin at 120 characters and lines should not be longer than that if possible.
  • Tabs should be used for all indents. We assume a tab is four spaces for determining line length.
  • When lines are split because they are too long, a double indentation should be used.
  • Opening braces on the same line as the declaration/for/if..., so not aligned with the closing brace.
  • No spaces inside brackets.
  • Spaces around operators.
  • No wildcards allowed on import statements.
  • Always a space before braces.
  • Always use braces (and thus multiple lines) for if, while, do-while.
  • Array specifiers must be attached to the type not the variable.
  • Class variables should never be declared public.
  • Logical units within a block should be separated by one blank line.

We have both an eclipse and IntelliJ IDEA formatter which can be used. However, be careful not to change the entire formatting of a class.