The purpose of the Java Coding Standards is to create a collaboration baseline. It will be helpful for scenarios where many people are creating, modifying, and contributing to the same project.
Do NOT blindly obey these guidelines, use them (after understanding) where they make sense.
All changed code, must obey these coding standards. As we have huge legacy code we will have code which defers Coding Standards. Eclipse code formatter mssql-jdbc_formatter.xml must be used to format the changed code, except for enum types.
We should give appropriate java comments in code. Please find ideal example of java comments. We do not expect to have full javadocs for common methods like getters / setters.
Correct:
/**
* Get property-only names that do not work with connection String
*
* @param name
* to normalize
* @param logger
* @return the normalized property name
*/
static String getPropertyOnlyName(String name, Logger logger) {
if (null == name) {
return name;
}
... some complex logic
}
Incorrect:
// Get property-only names
static String getPropertyOnlyName(String name, Logger logger) {
if(null == name) {
return name;
}
...
}
All Java files should contain the appropriate copyright notice at the beginning of the file.
/*
* Microsoft JDBC Driver for SQL Server
*
* Copyright(c) Microsoft Corporation All rights reserved.
*
* This program is made available under the terms of the MIT License. See the LICENSE file in the project root for more information.
*/
Pay special attention to the most common style errors...
- classes too long
- methods too long
- little or no javadoc comments
- swallow exceptions
- multiple return statements
- Overuse of arrays in place of collections
- too much or no whitespace
Multiple return statements are hard and time consuming to debug.
Correct:
public class StringUtils {
public static boolean isEmpty(String string) {
return string == null || "".equals(string.trim());
}
}
or
public class StringUtils {
public static boolean isEmpty(String string) {
boolean empty = false;
if (string == null) {
empty = true;
} else if ("".equals(string.trim())) {
empty = true;
}
return empty;
}
}
Incorrect:
public class StringUtils {
public static boolean isEmpty(String string) {
if (string == null) {
return true;
} else if ("".equals(string.trim())) {
return true;
}
return false;
}
}
Mirroring the natural language "if the current state is not active" rather than "if active is not the current state"
Correct:
!active
Incorrect:
active == false
When iterating over iterable elements where the current index in the iteration is not important for-each loops are preferred. **Not compatible for JDK 1.4 **
Correct:
for (String name: names) {
doStuff(name);
}
Incorrect:
for (int i = 0; i < names.length; i++) {
doStuff(names[i]);
}
Avoid the use of + or += to concatenate strings. Use java standards designed for that purposes such as String.format, StringBuilder, etc. If you are doing thread safe operation then use StringBuilder instead of StringBuffer.
Correct:
log.debug(String.format("found %s items", amount));
Incorrect:
log.debug("found " + amount + " items");
Use the right collections for the right task.
Duplicates
Implementations Iteration Order
- HashSet - undefined
- ConcurrentHashMap - undefined
- HashMap - undefined
- LinkedHashSet - insertion order
- LinkedHashMap - insertion order of keys
- ArrayList - insertion order
- LinkedList - insertion order
- TreeSet - ascending order (Comparable / Comparator)
Avoid using raw types when using classes that support generics.
Correct:
List<String> people = Arrays.asList("you", "me");
Incorrect:
List people = Arrays.asList("you", "me");
Consider the use of common design patterns.
Constrain arguments by using type safe enumerations.
Correct:
public enum Options {
YES, NO
}
Incorrect:
String yes = "YES";
String no = "NO";
enum value with multiple constants must be alligned such that the constant of same type from all the values must be alligned in same column.
Correct:
public enum SqlType{
VARCHAR ("varchar", JDBCType.VARCHAR),
NVARCHAR ("nvarchar", JDBCType.NVARCHAR),
CHAR ("char", JDBCType.CHAR);
}
Incorrect
public enum SqlType {
VARCHAR("varchar", JDBCType.VARCHAR),
NVARCHAR("nvarchar", JDBCType.NVARCHAR),
CHAR("char", JDBCType.CHAR);
}
Consider private helper methods to break down complex flows and long methods into more readable code.
Java developers are expected to use tools that enforce some of these good practices and quality code in general.
- The de-facto Editor is the Eclipse IDE
- New developed code should pass SonarQube rules
- Strongly recommend Sonar Lint tool
- New developed code / Only changed code should be formatted by Eclipse formatter.