-
Notifications
You must be signed in to change notification settings - Fork 0
Code Style
The HappyTree API is characterized by being a simple and flexible project, in which collaborators have some freedom to help us. To maintain a certain standard and direction, below are just a few recommendations of what the project's compliant code would look like.
The most important thing about code style is the exposure. The HappyTree API has a specification indicating that the API only publicly exposes classes and methods when a feature for the API client is made available, that is:
Do not expose to the API client what they don't need to know.
There are two main packages for implementing HappyTree API functionality. The happytree root package, of which the API client uses the functionalities through the interfaces and the core package, which represents the internal package and which must not be exposed to the API client.
This is the only standard that collaborators must follow strictly in order not to violate the HappyTree API specifications.
In a class, the order of its members is:
- Static constants;
- Attributes;
- Constructors;
- Methods;
- Inner classes.
In a class, the order of the access modifiers is:
- public;
- protected;
- default;
- private.
The HappyTree API has 4 Java class packages:
Represents the main package that will serve as an interface for the API client. In this package it is only allowed to have interfaces, which represent the HappyTree API functionalities.
When, and only when, new functionality is assigned to the HappyTree API.
Represents the package of Java annotations that indicate to a class whose objects are liable to be transformed into a node tree. Also, this package is public to the API client.
When there is a need to write a new annotation for a new feature.
Package that stores the HappyTree API exception classes. In addition, this package is public to the API client so that the API client can handle exceptions.
When there is a need to designate a new exception.
Represents the most important package of the HappyTree API. This is where all the implemented specifications of the HappyTree API are contained.
Everything in this package must not be accessible to the API client (except the HappyTree class).
The core package consists of:
- Validators;
- Pipeline;
- Repository;
- Factory;
- Utils & Helpers.
The collaborator has the freedom to develop or correct any problem at any time, as long as they do not expose the details to the API client and write legible and easy-to-understand code.
This package is inside the Core Package. It aims to separate from the Core Package what is only related to the API Transformation Process (ATP).
The same as for the Core Package.
The encoding used in the HappyTree API is UTF-8.
For the most part, the HappyTree API follows the Oracle Code Conventions
- Names of classes or interfaces in the HappyTree API represent nouns and each spelled word has its first letter capitalized;
- There are no "$", "_", or numbers.
- Method names usually indicate a verb (not necessarily);
- The first letter must always be lowercase;
- After the first spelled word, all words have the first letter capitalized;
- There are no "$", "_", or numbers.
- Variable names must represent the context they have in a method or class;
- The first letter must always be lowercase;
- After the first spelled word, all words have the first letter capitalized;
- There are no "$", "_", or numbers.
- Method constants must follow the same rules as variables;
- Class constants are all capitalized;
- If this is a class constant, each spelled word is separated by "_"
e.g.
DEFAULT_VERSION; - There are no "$", "_", or numbers.
Each hierarchy of code blocks must have the indentation of a single tab. The model follows:
public class Example {
private Integer foo;
private String[] bars;
public Example() {
}
public boolean doIt() {
String helloWorld = null;
for (String bar : bars ) {
if (!bar.equals("")) {
switch (bar.length()) {
case 1: {
return true;
}
default: {
return false;
}
}
}
}
return false;
}
} The braces are used next to the class instead of the variable.
Compliant:
public static void main(String[] args) {
}String[] names = new String[size-1];Non-Compliant:
public static void main(String args[]) {
}String names[] = new String[size-1];There is no space between the parentheses and arguments or the parentheses and the name of the method.
Compliant:
public static void main(String[] args) {
}Non-Compliant:
public static void main( String args[] ) {
}public static void main (String args[]) {
}There is only one space between the parentheses and a key word.
Compliant:
if (!foo) {
}Non-Compliant:
if(!foo) {
}Braces are always used on the same line as a class or interface, method bodies and constructor bodies, if statements, for-do-while loops, switch, etc.
Compliant:
public static void main(String[] args) {
}Non-Compliant:
public static void main(String[] args)
{
}Comments are only required on API client interfaces. The collaborator can feel free to elaborate line comments in internal methods, if the collaborator thinks it is necessary.
In public interfaces, the HappyTree API adopts Javadoc as a comment convention.
The HappyTree API is coded with a margin of 80 characters.