S.No. Descriptions Arrays: Only in Reasonable Cases (E.g. Too Many Casts Would Have Been Necessary)
S.No. Descriptions Arrays: Only in Reasonable Cases (E.g. Too Many Casts Would Have Been Necessary)
Descriptions
ARRAYS
1 To simplify the usage of arrays and to reduce errors the
class org.apache.commons.lang3.ArrayUtils from Apache
Commons is used.
Its methods are null safe. Therefore
NullPointerExceptions are prevented
COMMONS
3 To prepare a timestamp for logging the method
com.forcam.na.logging.UTCFormatter.format() must be
called. The timestamp will be displayed in a readable
format with UTC-value then
DATABASE
15 Access on the database must be performed via JPA
repositories or via DAOs
16 DAOs will be used if the HQL statement needs to be build
dynamically or there is post processing required for the
results of the query.
17 If someone is making changes on Database objects
through JPA classes i.e. Creating a Table, Indexes,
Constraints or Altering the objects Respective DDL sql
statements should also be added in migrate folder of
DDL_COMMON directory with appropriate file name
conventions followed by FORCAM. Special care must be
practiced to write a DDL statement which is applicable
both for Oracle & SQL Server Database.
Exceptions
19 Exceptions are logged with the method
com.forcam.na.common.LogUtilities.logError(). This
method logs the first 10 exceptions of exception
hierarchy of the stacktrace.
Naming
40 Identifiers (names of classes, interfaces, enumerations,
methods, parameters and variables) must not be
random. The name of an identifier has to be composed in
such a way that they carry meaningful information about
their purpose and their role in the code. This makes the
code easier to read and understand
41 Variable naming
Spring/Services
58 The name of the service must always be declared in the
annotation for the service. Normally it is the class name
(e.g. @Service("AlarmingService")).
59 The class and/or interface of a service has to be fully
documented with JavaDoc. The following point are
important:
Strings
65 To simplify the usage of Strings and to reduce errors
org.apache.commons.lang3.StringUtils from Apache
Commons should be used. Methods of StringUtils must
be checked before manipulating strings manually.
Its methods are null safe. Therefore
NullPointerExceptions are prevented
COMMONS
public class ValueContainer {
public Long getValue() {
return null;
}
}
public class Sample {
Before:
List<String> listStrings = new ArrayList<String>();
List<Map<String, List<String>> listMapStrings = new ArrayList<Map<String, List<String>>();
After:
List<String> listStrings = new ArrayList<>();
List<Map<String, List<String>> listMapStrings = new ArrayList<>();
Before:
double x = 0.0;
double result = Math.cos(1 / x); // Returns NaN if input is infinity
if (result == Double.NaN) { // Is always false
System.out.println("result is NaN");
}
After:
double x = 0.0;
double result = Math.cos(1 / x); // Returns NaN if input is infinity
if (Double.isNaN(result)) {
System.out.println("result is NaN");
}
These problems come up with finalizers:
1. There is no definitely time when the finalizer will be executed.
2. There is no guarantee that all finalizer will be called on shutdown of the jvm. The same applies for calling
methods like System.gc(), System.runFinalization(), System.runFinalizersOnExit() and
Runtime.runFinalizersOnExit(). Some of these methods have already been marked as deprecated and must not be
used to prevent security risks and deadlocks.
3. There is no definitely processing order for finalizer methods.
4. Uncatched exceptions during execution of a finalizer will be ignored.
5. Usage of synchronization mechanism may lead to deadlocks.
The parallelization of jobs must only be implemented through one of these frameworks:
1. Akka (preferred for communication inside an application, http://akka.io/docs/):
2. Quartz (for scheduled jobs, http://quartz-scheduler.org/documentation)
3. JMS (preferred for communication between applications)
Akka provides an easier and much more robust and scalable approach than java.util.concurrent.
DATABASE
Exceptions
catch (IOException | SQLException ex) {
LOGGER.log(ex);
}
FORMATING
Java Doc
Invalid
/**
* <some description>
*
* @param parameter1 - <description of parameter 1>
* @param parameter2 - <description of parameter 2>
*/
Incorrect: Correct:
/** /**
* <some description> * <some description>
* *
* @return String * @return <return value description>
*/ */
/**
* This is a comment with a link to the {@link Workplace} class.
* Also a link to a method {@link Workplace#toString()} or constant {@link Constants#UTF_8}.
*/
/**
* This is a {@code null} value. Other samples, {@code true}, {@code false}.
* Here is markup of the member variable {@code mValue}.
*/
Naming
Good name are: SAPDownload, fileContent, mDateFormat.
Bad names are: a, a1, x55, refx, button1
1. reflexity: the object must be equal to itself, e.g.: a.equals(a) must be true.
2. symmetry: If two objects are equal this must be valid for both directions, e.g.: if a.equals(b) is true then
b.equals(a) must also be true.
3. transitivity: If one object equals to two other objects then the two other objects must also be equal, e.g. if
a.equals(b) and b.equals(c) is true then a.equals(c) must also be true.
4. not null: An object must not be null null, e.g. a.equals(null) must always be false.
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (o == null) {
return false;
} else if (getClass() != o.getClass()) {
return false;
} else {
Pair pair = (Pair) o;
return (mLeft == pair.mLeft && mRight == pair.mRight);
}
}
}
@Override
public String toString() {
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append(...).append(...).toString();
}
Resource Management
Many close() methods do throw exceptions, too. These exceptions must be catched also. No exception must be
thrown out of a finally block
finally {
IOUtils.closeQuietly(dos1); //DataOutputStream dos1 = null;
IOUtils.closeQuietly(dos2); //DataOutputStream dos2 = null;
}
Spring/Services
http://jiravm:8090/pages/viewpage.action?pageId=93225156
1. For writing:
@Transactional(rollBackFor = Throwable.class)
2. For Reading:
@Transactional(rollBackFor = Throwable.class, readOnly = true)
Strings
1. isEmpty() => Checks if string is null or empty.
2. trim() => Removes whitespace at the beginning and at the end of Strings.
3. equals() => Compares two strings null safe.
4. startsWith() => checks null safe if the string starts with a certain prefix.
5. substring()/left()/right()/middle() => Retrieves null safe parts of a String.
6. split()/join() => Splits a string into substrings respectivley merges them together.
7. defaultString() => Delivers the default string, if null is passed.
8. replaceOnce() => Replaces a part of a string with another string.
9. indexOf() => Finds the index of a substring in a string. If the substring is not found the value
StringUtils.INDEX_NOT_FOUND will be returned.