Skip to end of metadata
Go to start of metadata
You are viewing an old version of this page. View the current version. Compare with Current ·  View Page History

Best practices

Developers of Sonar core should :

  • build with maven before pushing commits to GIT
  • push frequently into GIT
  • do not commit incomplete changes (GIT is not a backup system)
  • do not commit IDE files (.settings, .project, .classpath, .idea, *.iml, *.ipr, *.iws)
  • use meaningful comments when doing a commit, always with the JIRA key and title (e.g. "SONAR-6 Apply license LGPL") - see Note About Git Commit Messages
  • subscribe to the mailing list dev@sonar.codehaus.org
  • overuse unit tests (JUnit 4), with the help of hamcrest matchers and Mockito(mock objects)
  • think IoC (Inversion of Control pattern)
  • don't reinvent the wheel : use standard libraries like commons-lang, commons-io and google-collections (commons-collections is deprecated since Sonar 1.10)
  • use comments and javadocs only when necessary
  • API Javadoc - use "@since <version>" and "@deprecated in <version>"
  • do not forget the license header on all Java and Ruby files
  • respect the code formatting described below
  • eat your own food : have a look at Sonar on Sonar.

Source code encoding

Source code encoding should be set to UTF-8 to prevent cross-platform encoding issues.

UTF-8 is specified in parent POMs, so it is used by Maven when building the projects. However, it may not be set by default in your prefered IDE. The best practice is to force it for all your source code.

  • For Eclipse : go to "Preferences -> General -> Workspace" and force the "Text file encoding" to "UTF-8"
  • For IntelliJ IDEA : go to "Settings -> File Encodings" and force the "IDE Encoding" to "UTF-8"

Code formatting

  • Replace tabulations by 2 whitespaces
  • Classes with acronyms are CamelCase : for example prefer PmdPlugin to PMDPlugin, or JavaNcssHandler to JavaNCSSHandler
  • Follow this template :
  • Replace :
    by :

The following sections show how to set up the code style for Sonar in IDEA and Eclipse. It is strongly preferred that patches use this style before they are supplied.

Configure IntelliJ IDEA

Download sonar-idea-codestyle.xml
Copy it to ~/.IntelliJIDEA/config/codestyles (on Windows, try C:\Documents and Settings\<username>\.IntelliJIDEA\config\codestyles).
After this, restart IDEA and open the settings to select the new code style.

Configure Eclipse

Download sonar-eclipse-codestyle.xml and sonar-eclipse-cleanup.xml
After this, select Window -> Preferences, and open up the configuration for Java -> Code Style:

  1. Open Formatter. Click on the button labeled "Import..." and select the first file you downloaded. Give the style a name, and click OK.
  2. Open Clean Up. Click on the button labeled "Import..." and select the second file you downloaded. Give the style a name, and click OK.

Tips and tricks

Connect to Derby from command line

  • download derby (the same version as used by sonar)
  • set the DERBY_HOME property
  • execute the command $DERBY_HOME/bin/ij
  • connect to the database :

Compare XML files in JUnit

Use XMLUnit. The dependency is already defined into maven.

Warning : There's a bug on the similar() method. Contrary to the documentation, it fails if nodes are not in the same order.

Freeze Ruby gems

Ruby gems are used in the webapp (directory src/main/webapp of the sonar-server module). They are delivered with the application. To install or upgrade a gem :

  • copy the gem from local ruby installation into WEB-INF/vendor/gems.
  • execute "rake gems:refresh_specs" from src/main/webapp. It will generate files .specification
  • commit the directory WEB-INF/vendor/gems
Labels
  • None