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.
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 Conventions
and avoid Development antipatterns
and Architecture antipatterns
.
- 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.
- 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.
- Write all code and comments in English.
- Express all the names of classes, interfaces, packages, services, methods, attributes, and variables in English.
- 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. */ - 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 conventions
. - 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 contract
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 Conventions
but with the additional SAP Commerce Cloud conventions for different types:
- Define and Implement Services
- Create Facades
- Data Access Objects
- Work with Events
- Create Interceptors
- Add Dynamic Attributes
- Data Transfer Objects
- Use WCMS Components
- Create or Update Controller Classes
- Exceptions
- Models
- Provide Automated Tests
- Packages
- 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 // ... } -