Skip to content

Java Guideline

Information

This is crucial. The best and easiest way to know how to code and contribute is simply to have a look at one of the java artefact. We suggest you have a look at pp-grok/punplatform-bootstrap-lib to start with.

Coding Conventions

Basic Naming and Strategy

  • interface are named with a starting 'I'
  • enums are named with a starting 'E'
  • for each artifact, put all the interface and classes that are publicly exposed into 'api' packages. Use 'impl' package for implementations.

Constructors

We favor the builder API style. An example is

 // Get a new empty settings map
 ISettingsMap map = new SettingsMapImpl().build();

 //  Get a new filled SettingsMap
 ISettingsMap map = new SettingsMapBuilder().add("port", 80).add("host", "localhost").build()

 // Get a SettingsMap from the content of a whole file directory
 ISettingsMap map = new SettingsMapBuilder().fromDirectory("/tmp/conf").build()

Unit Tests

We use only junit5. Please do not bring in junit 4 TestNG or any other. In addition we defined a few utility interfaces and classes to ease the rendering and tracking of tests. These are located in the pp-grok/punchplatform-test-lib artifact.

Start from an example. Here is a short one:

import static org.junit.jupiter.api.Assertions.assertFalse;

import org.junit.jupiter.api.Test;
import org.thales.punch.libraries.utils.api.UtilHttp;
import com.github.punch.api.test.PunchTest;

public class UtilHttpTest  implements PunchTest  {
    @Test
    public void getCsv() {
        String url = "https://w3c.github.io/csvw/tests/test001.csv";
        String content = UtilHttp.getBody(url);
        assertFalse(content.isEmpty());
    }
}

Code Formatting

Code formatting is one of the first guidelines a code should respect. More than a way to indent your classes, the format allows code sourcing tools, just like Git, to compare code versions from an environment to an other environment. The format should result from a team agreement and be shared among its members. The best code format is the one everybody agrees with !

image

Now we will learn how to format source code inside the most used IDEs in the java world : IntelliJ and Eclipse. Before starting, you should now that it is possible to import Eclipse format to IntelliJ, but not the other way.

How to format in IntelliJ

  1. Click on File > Settings ...
  2. Go to Editor > Code Style > Java

From there, two solutions are available : * The formatter can be edited manually by using the code style window in tabs Tabs and indents, Spaces, Wrapping and braces ... * Import a format by clicking on the gear (next to Scheme) then on Import Scheme .... You may import a formatter from an Eclipse IDE or from another IntelliJ IDE

How to format in Eclipse

Go to Window > Preferences > Java > Code Style > Formatter then click on Edit .... You may now change Line wrapping, Method declarations ...

Note that you can import an Eclipse code format but not an IntelliJ code format.