Active/Severity | Name [expand / collapse] | Sort by: | ||||
---|---|---|---|---|---|---|
As of Java 5, The following code snippet illustrates this rule: public void doSomething() { int enum = 42; // Non-Compliant }
Repository: squid
![]() ![]() |
||||||
The following code snippet: public class MyException extends Error { /* ... */ } // Non-Compliant should be refactored into: public class MyException extends Exception { /* ... */ } // Compliant
Repository: squid
![]() ![]() |
||||||
While waiting for support of closure in Java, anonymous classes is the most convenient way to inject a behavior without having to create a dedicated class. But those anonymous inner classes should be used only if the behavior can be accomplished in a few lines. With more complex code, a named class is called for. The following code snippet illustrates this rule with a threshold of 5: button.addActionListener(new ActionListener() { // Non-Compliant - 6 lines till closing '}' public void actionPerformed(ActionEvent e) { doSomething1(); doSomething2(); } });
Repository: squid
![]() ![]() |
||||||
According to the Java Language Specification: For compatibility with older versions of the Java SE platform, the declaration of a method that returns an array is allowed to place (some or all of) the empty bracket pairs that form the declaration of the array type after the formal parameter list. This obsolescent syntax should not be used in new code. The following code snippet illustrates this rule: public int getVector()[] { /* ... */ } // Non-Compliant public int[] getVector() { /* ... */ } // Compliant public int[] getMatrix()[] { /* ... */ } // Non-Compliant public int[][] getMatrix() { /* ... */ } // Compliant
Repository: squid
![]() ![]() |
||||||
Array designators should always be located on the type for better code readability. Otherwise, developers must look both at the type and the variable name to know whether or not a variable is an array. The following code snippet illustrates this rule: int matrix[][]; // Non-Compliant int[] matrix[]; // Non-Compliant int[][] matrix; // Compliant
Repository: squid
![]() ![]() |
||||||
Assignments within sub-expressions are hard to spot and therefore make the code less readable.
It is also a common mistake to write Noncompliant Code ExampleSystem.out.println(i = 42); Compliant SolutionSystem.out.println(i == 42); or: i = 42; System.out.println(i); ExceptionsAssignments enclosed in relational expressions are allowed. BufferedReader br = new BufferedReader(/* ... */); String line; while ((line = br.readLine()) != null) { /* ... */ }
Repository: squid
![]() ![]() |
||||||
Here are the main reasons why commented code is a code smell :
Repository: squid
![]() ![]() |
||||||
One might assume that new BigDecimal(.1) is exactly equal to .1, but it is actually equal to .1000000000000000055511151231257827021181583404541015625. This is so because .1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the long value that is being passed in to the constructor is not exactly equal to .1, appearances notwithstanding. The (String) constructor, on the other hand, is perfectly predictable: 'new BigDecimal(.1)' is exactly equal to .1, as one would expect. Therefore, it is generally recommended that the (String) constructor be used in preference to this one.
Repository: pmd
![]() ![]() |
||||||
Avoid throwing a NullPointerException - it's confusing because most people will assume that the virtual machine threw it. Consider using an IllegalArgumentException instead; this will be clearly seen as a programmer-initiated exception.
Repository: pmd
![]() ![]() |
||||||
The Cyclomatic Complexity is measured by the number of (&&, ||) operators and (if, while, do, for, ?:, catch, switch, case, return, throw) statements in the body of a class plus one for each constructor, method (but not getter/setter), static initializer, or instance initializer in the class. The last return stament in method, if exists, is not taken into account. Even when the Cyclomatic Complexity of a class is very high, this complexity might be well distributed among all methods. Nevertheless, most of the time, a very complex class is a class which breaks the Single Responsibility Principle and which should be re-factored to be split in several classes.
Repository: squid
![]() ![]() |
||||||
Boolean expressions should be as compact as possible to improve readability. Useless operators should be removed. Noncompliant Code Exampleif (booleanVariable == true) { /* ... */ } if (booleanVariable != true) { /* ... */ } if (booleanVariable || false) { /* ... */ } doSomething(!false); Compliant Solutionif (booleanVariable) { /* ... */ } if (!booleanVariable) { /* ... */ } if (booleanVariable) { /* ... */ } doSomething(true);
Repository: squid
![]() ![]() |
||||||
Using The following code: boolean result1 = foo.toUpperCase().equals(bar); // Non-Compliant boolean result2 = foo.equals(bar.toUpperCase()); // Non-Compliant boolean result3 = foo.toLowerCase().equals(bar.LowerCase()); // Non-Compliant should be refactored into: boolean result = foo.equalsIgnoreCase(bar); // Compliant
Repository: squid
![]() ![]() |
||||||
Sharing some naming conventions is a key point to make it possible for a team to efficiently collaborate. This rule allows to check that all class names match a provided regular expression. The following code snippet illustrates this rule when the regular expression value is "^[A-Z][a-zA-Z0-9]*$": public class MyFirstClass { ... } // Compliant public class mySecondClass { ... } // Non-Compliant
Repository: squid
![]() ![]() |
||||||
Public class variable fields do not respect the encapsulation principle and has two main disadvantages:
The following code: public class MyClass { public static final int SOME_CONSTANT = 0; // Compliant - constants are not checked public String firstName; // Non-Compliant } should be refactored into: public class MyClass { public static final int SOME_CONSTANT = 0; // Compliant - constants are not checked private String firstName; // Compliant public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } }
Repository: squid
![]() ![]() |
||||||
Classes in The following code snippet illustrates this rule: import com.sun.jna.Native; // Non-Compliant import sun.misc.BASE64Encoder; // Non-Compliant
Repository: squid
![]() ![]() |
||||||
Ensure that resources (like Connection, Statement, and ResultSet objects) are always closed after use. It does this by looking for code patterned like : Connection c = openConnection(); try { // do stuff, and maybe catch something } finally { c.close(); }
Repository: pmd
![]() ![]() |
||||||
Using The following code: if (myCollection.size() == 0) { // Non-Compliant /* ... */ } should be refactored into: if (myCollection.isEmpty()) { // Compliant /* ... */ }
Repository: squid
![]() ![]() |