Java Coding Best Practices


1. Java error-handlingPublic methods should throw at most one checked exception
2. Java brain-overloadLoops should not contain more than a single "break" or "continue" statement
3. Java     conventionLong suffix "L" should be upper case
4. JavaDeprecated elements should have both the annotation and the Javadoc tag
5. Java bug"equals(Object obj)" and "hashCode()" should be overridden in pairs
6. Java java8Lamdbas containing only one statement should not nest this statement in a block
7. Java conventionLeft curly braces should be located at the end of lines of code
8. JavaExceptions should not be thrown in finally blocks
9. Java     error-handlingException classes should be immutable
10.Java  error-handlingException handlers should preserve the original exception
11. JavaEmpty arrays and collections should be returned instead of null
12.Java pitfallMethods named "equals" should override Object.equals(Object)
13. Java"public static" fields should always be constant
14.Java conventionClass names should comply with a naming convention
15.Java conventionModifiers should be declared in the correct order
16. Java pitfallSwitch cases should end with an unconditional break statement
17. Java conventionMethod names should comply with a naming convention
18.Java brain-overloadMethods should not be too complex
19. JavaEnumeration should not be implemented
20. Java brain-overloadSwitch cases should not have too many lines
21. Java pitfallOctal values should not be used
22.JavaStringBuilder and StringBuffer should not be instantiated with a character
23.Java"object == null" should be used instead of "object.equals(null)"
24.JavaDeclarations should use Java collection interfaces such as "List" rather than specific implementation classes such as "LinkedList"
25.Java brain-overloadControl flow statements "if", "for", "while", "switch" and "try" should not be nested too deeply
26.JavaString.valueOf() should not be appended to a String
27. JavaLabels should not be used
28. JavaCollection.isEmpty() should be used to test for emptiness
29. JavaCase insensitive string comparisons should be made without intermediate upper or lower casing
30.JavaPrimitive wrappers should not be instantiated only to perform a to String conversion
31. Java conventionLoggers should be "private static final" and should share a naming convention
32. Java securityIP addresses should not be hardcoded
33. Java error-handlingThrowable and Error classes should not be caught
34.JavaThe Object.finalize() method should never be overriden
35.Javasuper.clone() should be called when overriding Object.clone()
36.Java bugLoop counters should not be assigned to from within the loop body
37.Java pitfallLocal variables should not shadow class fields
38. Java pitfallMethod parameters, caught exceptions and foreach variables should not be reassigned
39.Java brain-overloadOverriding methods should do more than simply call the same method in the super class
40.JavaMethods should not be empty
41.JavaCollapsible "if" statements should be merged
42.JavaThe default unnamed package should not be used
43.Java brain-overloadExpressions should not be too complex
44. Java brain-overloadLambdas and anonymous classes should not have too many lines
45.Java pitfallMethods should not be named "hashcode"
46.Java unusedUnused private fields should be removed
47.Java pitfallNon-constructor methods should not have the same name as the enclosing class
48.Java bug, unusedConditions in related "if/else if" statements should not be duplicated
49.JavaConstants should be declared "final static" rather than merely "final"
50. Java conventionComments should not be located at the end of lines of code
51.JavaNon-static class initializers should not be used
52.Java unusedUnused method parameters should be removed
53.Java conventionThe Array.equals(Object obj) method should never be used
54.Java brain-overloadAvoid too complex class
55.Java bugExecution of the Garbage Collector should be triggered only by the JVM
56.Java java8Parentheses should be removed from a single lambda input parameter when its type is inferred
57.Java multithreadingThread.run() and Runnable.run() should not be called directly
58.Java"switch" statements should not contain non-case labels
59. JavaObject.finalize() should remain protected (versus public) when overriding
60. Java pitfallObject.finalize() should not be overloaded (by adding method parameters)
61.Java bug"equals(Object obj)" should be overridden along with the "compareTo(T obj)" method
62.JavaGeneric wildcard types should not be used in return parameters
63. Java conventionThe members of an interface declaration or class should appear in a pre-defined order
64. JavaConstants should not be defined in interfaces
65.Java error-handlingThrows declarations should not be redundant
66.Java bugsuper.finalize() should be called at the end of Object.finalize() implementations
67.Java unusedUnused local variables should be removed
68.JavaSwitch statements should end with a default case
69.JavaClass variable fields should not have public accessibility
70.Java conventionRight curly brace and next "else", "catch" and "finally" keywords should be located on the same line
71.JavaLiteral boolean values should not be used in condition expressions
72.JavaVariables should not be declared and then immediately returned or thrown
73.JavaReturn of boolean expressions should not be wrapped into an if-then-else statement
74. JavaUseless parentheses around expressions should be removed to prevent any misunderstanding
75.Java"enum" should no longer be used as a name
76.JavaClasses from "com.sun.*" and "sun.*" packages should not be used
77.JavaString literals should not be duplicated
78.Java unusedUnused private method
79.Java error-handlingException types should not be tested using "instanceof" in catch blocks
80.Java error-handling"java.lang.Error" should not be extended
81.Java unusedAvoid commented-out lines of code
82.Java conventionArray designators "[]" should be located after the type in method signatures
83. Java unusedUseless imports should be removed
84.JavaUtility classes should not have a public constructor
85.Java conventionArray designators "[]" should be on the type, not the variable
86.JavaNested code blocks should not be used
87. JavaCollections.emptyList(),emptyMap() and emptySet() should be used instead of Collections.EMPTY_LIST, EMPTY_MAP and EMPTY_SET
88.JavaPackage declaration should match source file directory
89.Java error-handlingGeneric exceptions Error, RuntimeException, Throwable and Exception should never be thrown
90.JavaSynchronized classes Vector, Hashtable, Stack and StringBuffer should not be used
91.Java brain-overloadA field should not duplicate the name of its containing class
92.JavaEmpty statements should be removed
93.Java brain-overloadMethods should not have too many parameters
94.JavaTry-catch blocks should not be nested
95. Java bugReturn statements should not occur in finally blocks
96.Java bugNested blocks of code should not be left empty
97.Java conventionRight curly braces should be located at the beginning of lines of code
98.JavaIf statement conditions should not always evaluate to "true" or to "false"
99. Java conventionTabulation characters should not be used
100.JavaSystem.exit(...) and Runtime.getRuntime().exit(...) should not be called
101. Java"switch" statements should have at least 3 cases
102. Java error-handlingThrowable.printStackTrace(...) should never be called
103.Java conventionif/else/for/while/do statements should always use curly braces
104. Java conventionPackage names should comply with a naming convention
105. JavaMagic numbers should not be used
106. JavaSystem.out and System.err should not be used as loggers
107.Java conventionStatements should be on separate lines
108.Java pitfallThe Object.finalize() method should never be called
109.Java conventionLocal variable and method parameter names should comply with a naming convention
110.JavaStrings literals should be placed on the left side when checking for equality
111.Java conventionType parameter names should comply with a naming convention
112.JavaAssignments should not be made from within sub-expressions
113.JavaDeprecated code should be removed eventually
114.Java conventionInterface names should comply with a naming convention
115.JavaFIXME tags should be handled
116.JavaTODO tags should be handled
117.Java conventionField names should comply with a naming convention
118.Java conventionConstant names should comply with a naming convention
119.JavaDuplicated blocks
120.JavaInsufficient branch coverage by unit tests

Comments

Popular posts from this blog

Docker to ELK