Thursday, September 3, 2020

CodeSmells-SonarQube

CodeSmells are structures in code that violate design principles and negatively impact quality.

 Here are some of the bad smells in Java code. 

Constant Interface:Constant interfaces have only static final data members declared in them without any methods. Suggested refactoring for this smell depends on kind of constants present in the constant interface: the constants can get added as members in the class or can be rewritten as enums.

 The interface java.io.ObjectStreamConstant is an example for this smell. 

Global Variable Class:The class has one public static (non-final) field. Since its freely available for modification by anyone, it becomes an equivalent to a C-like global variable, with only difference that the variable name is prefixed by the class name, thus avoiding name-clashes. Here is an example:

class Balls {

public static long balls = 0;

Global Function Class:It is a public class that has only one static public method and no other fields or methods; it can have an optional private constructor to disallow instantiation. It is equivalent to a C-like global function, except that the function needs to be prefixed by the class name to access the function. 

Publicly Exposed Fields:It is a public class with public non-final, non-static data members and no methods (with an optional constructor). It is difficult to maintain public, C-like classes, as Effective Java notes: "Several classes in the Java platform libraries violate the advice that public classes should not expose fields directly. .

Forgotten Interface:A class implements all the methods with the same signatures as the methods listed in an interface. It is a likely mistake that the class intended to implement an interface, but forgot to list the interface as its base type. A consequence of this smell is that the objects of the class cannot be treated as subtype of the interface and hence the benefit of subtyping and runtime polymorphism is not exploited. 

Clone Class: A Clone class is an exact replica of another class (unrelated by inheritance). Essentially, only the name of the class is different, but all its members, their signature, accessibility, etc. are the same..

No comments:

Post a Comment

ES12 new Features