Code Smell
Quality Of Code
Question
1. What is Bug?
Introduction
Code smells are not bugs or errors. Instead, these are absolute violations of the fundamentals of developing
software that decrease the quality of code.
Having code smells does not certainly mean that the software won’t work, it would still give an output, but it
may slow down processing, increased risk of failure and errors while making the program vulnerable to bugs
in the future. Smelly code contributes to poor code quality and hence increasing the technical debt.
Any fool can write code that a computer can understand,
good programmers write code that every human can
understand. - Martin Fowler
Code Smell Example
Comments
Are Comments necessary?
1. Why?
2. What?
3. Comments are for people not for
machines.
4. Think about refactoring the code so
that comments are not needed.
Long Method
Try avoiding longer methods.
Short methods are easier to read &
understand for a developer who
looks at the code.
1. Ideal lines of code -20.
2. Methods with 5 lines of code,
indicate that you are breaking
up the code too much, making
it harder to understand,
Code Smell Example (Cont.)
Too Many Parameters
Avoid to many parameters in the
method signature.
1. Ideal number of parameters (as
per checkstyle static code
analyzer) -7
2. Keeping it less than 5 makes it
easy to read understand.
Type Embedded in Name
Avoid embedding types to the
variable names.
Some Bad Example
1. List<String> listofStudents;
2. String sStudent;
3. Int iAmmount
Code Smell Example (Cont.)
Inconsistent Names
Many developers have trouble with
naming the methods with the efficient
name.
Examples
1. Use lower case for packages.
2. Use camel case of variables and
method names.
3. Avoid using ‘_’
Dead Code
Unused code? Or commented
code? Delete is before push to
repository.
Code Smell Example (Cont.)
Solution Sprawl
If you have too many classes to
perform a solution, you might have
solution sprawl.
Time to refactor the code.
Application Level Smells
1. Duplicate Code - Similar code in more than
one location.
2. Shotgun Surgery - One change requires
altering many different classes.
3. Contrived Complexity - Using complex
design patterns where a simpler
uncomplicated design could be used.
Class Level Smells
1. Large Class - Class trying to do too much and has too many
instance variables.
2. Freeloader - Class doing too little
3. Feature Envy - Class with a method that seems more interested in
other class than the one it is in.
4. Divergent Code - A class that suffers many kinds of changes to
bring a change in a system.
5. Data Clump - Bunches of data that clump together in lots of
places.
6. Inappropriate Intimacy - A class that has dependencies on
implementation details of other class.
7. Middle Man - Class with lots of methods delegated to other class.
8. Downcasting - Typecast that breaks the abstraction model.
9. Parallel Inheritance Hierarchy - Every time you make a subclass
for a single class, you are needed to make subclass.
10. Refused Bequest - Subclass not using methods and data of
superclass.
11. Cyclomatic Complexity - Class with too many branches and
loops.
Method Level Smells
1. Long Method - Long procedures that are hard to
understand.
2. Speculative Generality - Methods whose only users
are test cases.
3. Message Chains - Method calling a different method
which calls a different method which calls a different
method… and on and on.
4. Too Many Parameters - A very long list of
parameters.
5. Oddball Solutions - When multiple methods are
used to solve the same problem in one program
creating inconsistency.
6. God Line - An excessively long line of code.
7. Excessive Returner - A method that returns more
data than what its caller needs.
8. Identifier Size - The identifier is excessively short or
long.

Code smells quality of code

  • 1.
  • 2.
  • 3.
    Introduction Code smells arenot bugs or errors. Instead, these are absolute violations of the fundamentals of developing software that decrease the quality of code. Having code smells does not certainly mean that the software won’t work, it would still give an output, but it may slow down processing, increased risk of failure and errors while making the program vulnerable to bugs in the future. Smelly code contributes to poor code quality and hence increasing the technical debt. Any fool can write code that a computer can understand, good programmers write code that every human can understand. - Martin Fowler
  • 4.
    Code Smell Example Comments AreComments necessary? 1. Why? 2. What? 3. Comments are for people not for machines. 4. Think about refactoring the code so that comments are not needed. Long Method Try avoiding longer methods. Short methods are easier to read & understand for a developer who looks at the code. 1. Ideal lines of code -20. 2. Methods with 5 lines of code, indicate that you are breaking up the code too much, making it harder to understand,
  • 5.
    Code Smell Example(Cont.) Too Many Parameters Avoid to many parameters in the method signature. 1. Ideal number of parameters (as per checkstyle static code analyzer) -7 2. Keeping it less than 5 makes it easy to read understand. Type Embedded in Name Avoid embedding types to the variable names. Some Bad Example 1. List<String> listofStudents; 2. String sStudent; 3. Int iAmmount
  • 6.
    Code Smell Example(Cont.) Inconsistent Names Many developers have trouble with naming the methods with the efficient name. Examples 1. Use lower case for packages. 2. Use camel case of variables and method names. 3. Avoid using ‘_’ Dead Code Unused code? Or commented code? Delete is before push to repository.
  • 7.
    Code Smell Example(Cont.) Solution Sprawl If you have too many classes to perform a solution, you might have solution sprawl. Time to refactor the code.
  • 8.
    Application Level Smells 1.Duplicate Code - Similar code in more than one location. 2. Shotgun Surgery - One change requires altering many different classes. 3. Contrived Complexity - Using complex design patterns where a simpler uncomplicated design could be used.
  • 9.
    Class Level Smells 1.Large Class - Class trying to do too much and has too many instance variables. 2. Freeloader - Class doing too little 3. Feature Envy - Class with a method that seems more interested in other class than the one it is in. 4. Divergent Code - A class that suffers many kinds of changes to bring a change in a system. 5. Data Clump - Bunches of data that clump together in lots of places. 6. Inappropriate Intimacy - A class that has dependencies on implementation details of other class. 7. Middle Man - Class with lots of methods delegated to other class. 8. Downcasting - Typecast that breaks the abstraction model. 9. Parallel Inheritance Hierarchy - Every time you make a subclass for a single class, you are needed to make subclass. 10. Refused Bequest - Subclass not using methods and data of superclass. 11. Cyclomatic Complexity - Class with too many branches and loops.
  • 10.
    Method Level Smells 1.Long Method - Long procedures that are hard to understand. 2. Speculative Generality - Methods whose only users are test cases. 3. Message Chains - Method calling a different method which calls a different method which calls a different method… and on and on. 4. Too Many Parameters - A very long list of parameters. 5. Oddball Solutions - When multiple methods are used to solve the same problem in one program creating inconsistency. 6. God Line - An excessively long line of code. 7. Excessive Returner - A method that returns more data than what its caller needs. 8. Identifier Size - The identifier is excessively short or long.