Java Development Guidelines for SAP Commerce Cloud

While there is support for Groovy and Scala, SAP Commerce Cloud is coded predominantly in Java. Follow the general guidelines for Java coding with SAP Commerce Cloud.

Regardless of the actual language in use, you need to install and configure a supported Java Development Kit, and download and configure SAP Commerce Cloud.

In addition, most developers will want the benefits of using an Integrated Development Environment (IDE). While many will prefer JetBrains IntelliJ IDEA, SAP Commerce Cloud ships with some built-in support for Eclipse-based IDEs.

Best Practice Guidelines

SAP recommends these guidelines for software developers extending SAP Commerce Cloud.

Follow established industry-recognised Java best practices.
Unless stated otherwise in the rules of the SAP Commerce Cloud SonarQube quality profile for Java or later on this page, follow all the usual Java design and coding best practices. For example, unless stated otherwise follow the standard Java Code ConventionsInformation published on non-SAP site and avoid Development antipatternsInformation published on non-SAP site and Architecture antipatternsInformation published on non-SAP site.
Comply with the rules in the SAP Commerce Cloud SonarQube Java profile.
Use SonarQube scanners and the SonarLint plugin for your IDE to check that code complies with the rules in the SAP Commerce Cloud SonarQube Java profile.
Many of the simpler SAP Commerce Cloud coding best practices, standards and conventions are defined by a SonarQube quality profile that ships with the SAP Commerce Cloud product. You can find the profiles in the build-tools/sonarqube directory of your SAP Commerce Cloud installation.
The rules included in the SAP Commerce Cloud SonarQube quality profile are not repeated in this section. Use a SonarQube server to view the entries and descriptions of the rules included in the SAP Commerce Cloud quality profile. Different releases of SAP Commerce Cloud may use different versions of the SonarQube server and rule definitions may vary slightly with these versions.
These best practice guidelines contain additional standards, rules and guidelines that are specific to working with SAP Commerce Cloud and not yet encoded into the SonarQube server.
Layout code according to the SAP Commerce Cloud Java code formatting conventions.
SAP Commerce Cloud Java code formatting conventions are defined by a particular set of Eclipse IDE settings. All extensions created from hybris template extensions are configured automatically with the correct settings. In addition, extensions created in this way are configured to auto-format the code when saving within the Eclipse IDE.
For example, SAP Commerce Cloud code aligns braces instead of using the more usual compact style of layout. Most other formatting is conventional.
To update the formatter settings of older extensions or extensions not created from templates, or make these settings the Eclipse workspace defaults, save the settings as a named profile in one of the extensions that has them already, and then switch to that profile in the relevant extensions or at the workspace level.
If using an alternative IDE, set the code formatting to be consistent with these settings.
Write all code and comments in English.
Express all the names of classes, interfaces, packages, services, methods, attributes, and variables in English.
Write all comments both in-line and Javadoc in English.
SAP Commerce Cloud is a global product. Standardising on English greatly eases the communication between teams located in different parts of the world including product support, professional services, and partner and customer development teams that may read parts of the code.
If English is not the team's first language, it may be useful to create and maintain a bilingual glossary to help developers find names in English.
Pay attention to typing errors and spelling mistakes. If unsure of a spelling, use the internet to find the correct spelling. Although typing errors are not fundamentally detrimental to the proper functioning of the code, they give an impression of poor quality and can be very annoying if there are too many. Modern IDE's can find many basic spelling mistakes. Nevertheless, only code walkthroughs, inspections and reviews can find other kinds of typing error. In general, it is best to fix a typing error as soon as possible; it is nearly always easier to correct them earlier than later.
Start all Java source code files with a standard intellectual property header.
Start all Java code files that form part of the shipping SAP Commerce Cloud product with this intellectual property comment block:
/*
 * [y] hybris Platform
 *
 * Copyright (c) 2017 SAP SE or an SAP affiliate company.  All rights reserved.
 *
 * This software is the confidential and proprietary information of SAP
 * ("Confidential Information"). You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the
 * license agreement you entered into with SAP.
 */
Start other code such as implementation-project-specific code, and code that is part of some generic, reusable asset or add-on, etc., with an equivalent intellectual property comment block defined by the appropriate SAP field, partner or customer organization.
Add value with in-line comments.
  • Add in-line comments that contain information that is not obvious from reading the code in that file.

  • Do not restate in in-line comments what is clear from reading the code.

  • What the code is doing should be clear from the code itself. If at all possible, rewrite tricky/spaghetti code instead of adding comments in an attempt to make the code clearer. Comments should be used to make clean code better, not as an excuse for messy or 'clever' code.

  • Comments may be needed to state why the code needs to do certain steps (e.g. if the platform needs a particular construct or order of execution), or why the code was written in a particular way (e.g. to optimise performance when a simpler, more obvious way does not provide the performance required).

  • Do not use comment generators such as JAutoDoc. Automatically generated comments typically provide no added insight, clutter the code with unhelpful commenting, and give the false impression that code has been thoughtfully commented by the developers.

  • Remove commented-out lines of code. Commented-out code makes code harder to read and suggests the developer was thinking, "I don't think we need these, but just in case, I shall leave it here"; source control keeps old code in case it is needed again.

Fully Javadoc all public and protected interfaces.
  • Provide all public and protected types, methods and fields with comprehensive, value-add Javadoc comments that comply with the standard Javadoc conventionsInformation published on non-SAP site.

  • If you want your code to be customised and extended easily by other developers, and it ships as a binary form rather than as source code, you need to ensure all developers have all the API information they need in Javadoc to configure, customize, and extend your interfaces and classes correctly.
  • To ensure the Javadoc is comprehensive, use design by contractInformation published on non-SAP site conventions for javadoc comments so that developers clearly understand what is required of them and what to expect in return.
  • Be wary of automatically generated java doc, but when code is supplied as a binary, then the generated javadoc may be all developers can see about a method. It is not that generated javadoc is bad, it is the fact that it often says nothing of value. Nevertheless, generated javadoc stating that a basic setter method for a name property is a standard setter method for the name property communicates that there are no unexpected side-effects. That has value, and if generated, costs next-to-nothing to provide.
  • When modifying a method, ensure the Javadoc is up to date. There is nothing worse than incorrect and out of date Javadoc.
  • Private methods should be avoided, and so should writing Javadoc for them.
  • Do not include @author tags unless you want to be blamed personally by colleagues, partners and customers.
  • If practical, instead of including examples of how to use, customise or extend the system as Javadoc comments, create an easily-understood, automated test to demonstrate and prove the example.
Comply with SAP Commerce Cloud naming and packaging conventions.
In general, follow the usual Java naming and packaging conventions as described in Java Code ConventionsInformation published on non-SAP site but with the additional SAP Commerce Cloud conventions for different types:
See rules for each in subsequent sections.
Use exceptions appropriately.
  • Use exceptions for exceptional cases that you do not expect. If you expect something to happen, handle it by return value.

  • Don't use checked exceptions unless you have a very good reason for it.
  • Define Exception classes for the various error cases you want to handle through Exceptions (it is easier to handle an OrderNotFoundException and display a nice error message than a plain BusinessException).
  • When you have to catch an exception that you don't know how to handle, simply wrap it in a RuntimeException and bubble it up. Never swallow any exceptions. If you need to ignore an exception, comment why you need to and add a log statement.
  • Use exceptions to express error codes:
    • don't let your function return an int error code;

    • don't let your function return a complex type containing error attributes;

    • remember exception types can be complex but think of situations where you might need serialization.

  • If you decide to throw RuntimeExceptions in your code, document this with a @throws statement in your operation's Javadoc comments but do not declare it in a throws clause of your operation signature. The following shows a compliant example:
/**
 * ...
 *
 * @throws MyRuntimeException when my exception occurs
 */
public void example()
{
    // ...
    throw new MyRuntimeException(arguments);
    // ...
}
The following displays a non-compliant example:
public void example() throws MyRuntimeException //Do not declare runtime exceptions here
{
    // ...
    throw new MyRuntimeException(arguments);
    // ...
    throw new RuntimeException(message); //Do not throw generic exceptions
    // ...
}