SlideShare a Scribd company logo
CODE SMELL/ BAD SMELL
CODE SMELL/ BAD SMELL
Anshul
National institute of Technology ,Kurukshetra
April 10, 2016
CODE SMELL/ BAD SMELL
Overview
1 Motivation
2 Introduction
Definition
Code re-factoring
3 Types of Code Smell
Duplicate Code
Long method
Large Class
Divergent Change
Shortgun Surgery
Feature Envy
Data Clumps
Comments
4 Advanced....
Two Level Dynamic Approach for Feature Envy Detection
5 Conclusion
CODE SMELL/ BAD SMELL
Motivation
Though Re-factoring is a old concept, developers are not
utilizing it.
To capture and write down the situation which make sense to
re-factoring.
Re factoring is directly related to Economics
CODE SMELL/ BAD SMELL
Introduction
Definition
Code smell :- Code smells are indications of poor coding and
design choices that can cause problems during the later phase of
software development.
Code smells are considered as flags to the developer that
some parts of the design may be inappropriate.
Such design flaw that can be solved by re-factoring.
According to MARTIN FOWLER ”IF it stinks change it.”
CODE SMELL/ BAD SMELL
Introduction
Code re-factoring
”Refactoring is the process of changing a software system in
such a way that it does not alter the external behavior of the
code yet improves its internal structure.
Refactoring is a form of program transformation which
preserves the semantics of the program.
Improving the design after it has been written.
It is an iterative process.
CODE SMELL/ BAD SMELL
Types of Code Smell
Duplicate Code
Occurrence of Same code structure more than once.
When we have same expression in two or more methods of
same class.
Solution for this code smell is to do Extract method and
then invoke the code from both the places.
CODE SMELL/ BAD SMELL
Types of Code Smell
Duplicate Code
Example 1
extern int a[];
extern int b[];
int sumofa = 0;
for (int i = 0; i < 4; i + +)
sum += a[i];
int averageofa= sum/4;
—————-
int sumofb = 0;
for (int i = 0; i < 4; i + +)
sum += b[i];
int averageofb = sumofb/4;
Extract method
int calc-average(int* array)
int sum= 0;
for (int i = 0; i < 4; i + +)
sum + =array[i];
return sum/4;
CODE SMELL/ BAD SMELL
Types of Code Smell
Duplicate Code
Example1 contd...
Extract method will give source code that has no loop duplication
extern int a[];
extern int b[];
int averageofa = calcAverage(a[]);
int averageofb = calcAverage(b[]);
CODE SMELL/ BAD SMELL
Types of Code Smell
Duplicate Code
Example 2
Another problem is when we have same code in two
subclasses.
Solution:-(Extract method + pull up method ) extract
similar codes from both the classes in form a method and
then put this method in the superclass.
CODE SMELL/ BAD SMELL
Types of Code Smell
Long method
Brief
Analysis and the experience says that the object programs
which lives best and longest are those with short methods.
To shorten a method all we have to do is to Extract a new
method
CODE SMELL/ BAD SMELL
Types of Code Smell
Long method
Key points:
IF method has a lot of parameters and variables , then these
things comes in the form of obstacles to Extract method.
Even if we try to do so , we will end up passing so many
parameters and variables to newly Extracted method.
Solution is to replace temp with query to eliminate temps.
And to slim down the long parameters we may introduce a
new parameter object And then preserving whole object.
CODE SMELL/ BAD SMELL
Types of Code Smell
Long method
Replace temp with Query
double basePrice
= quantity ∗ itemPrice;
if (basePrice > 1000)
return basePrice ∗ 0.95;
else return basePrice ∗ 0.98;
if (basePrice() > 1000) return
basePrice() ∗ 0.95;
else return basePrice() ∗ 0.98;
...———————
double basePrice()
return quantity ∗ itemPrice;
CODE SMELL/ BAD SMELL
Types of Code Smell
Long method
New Parameter Object
CODE SMELL/ BAD SMELL
Types of Code Smell
Long method
Question: How to decide the clumps of code that we
want to extract ?
CODE SMELL/ BAD SMELL
Types of Code Smell
Long method
Answer see comments ” . Even a single line is worth
extracting if it needs explanations.
CODE SMELL/ BAD SMELL
Types of Code Smell
Large Class
1 Signs and symptoms:
When a single class is doing too much , it often shows up too
many variables and instances
OR can say ; A class having many fields/methods/lines of code
is called as LARGE CLASS.
2 Reasons for the Problem:
Classes usually start small. But over time, they get bloated as
the program grows.
Programmers usually find it mentally less taxing to place a
new feature in an existing class than to create a new class for
the feature.
3 Treatment
Extract Class helps if part of the behavior of the large class
can be spun off into a separate component.
Extract SubClass helps if part of the behavior of the large
class can be implemented in different ways or is used in rare
cases.
CODE SMELL/ BAD SMELL
Types of Code Smell
Large Class
Extract Class
CODE SMELL/ BAD SMELL
Types of Code Smell
Divergent Change
When we make a change,we want to be able to a single clear
point in the system and make the change.
And if you can not do this, you are smelling one of the two
closely related pungencies.
Divergent change occurs when one class is commonly changed
in different ways for different reasons.
Split up the behavior of the class viaExtract Class
Payoff
Improves code organization.
Reduces code duplication.
Simplifies support.
CODE SMELL/ BAD SMELL
Types of Code Smell
Divergent Change
Example:
class User
def full−name
”#{first − name}#{last −
name}”
end
def authorized?(action)
is−admin?
end
end
class User
class UserPresenter
<SimpleDelegator
def full−name
”#{first − name}#{last −
name}”
end
end
class ActionAuthorizer def
authorized?(action, user)
user.is−admin?
end
end
# nothing here?
end
CODE SMELL/ BAD SMELL
Types of Code Smell
Shortgun Surgery
Shortgun surgery is opposite to divergent change
This happens when, you want to make some kind of
change, you are forced to make a lot of changes to a lot
of different classes
And when changes are all over the place, they are hard to find
,and it’s easy to miss an important change
Use MOVE METHOD and MOVE FIELD to pull all the
changes to a single class.
CODE SMELL/ BAD SMELL
Types of Code Smell
Shortgun Surgery
Example:Move Method
class Project {
Person[] participants;
}
class Person
{
int id;
boolean participate(Project p)
{ for(int i = 0; i < p.
participants .length ; i + +) {
if (p.participants[i].id ==id)
return(true);
} return(false); } }
... if (x.participate(p)) ...
class Project {
Person[] participants;
boolean participate(Person x) {
for(int i= 0;
i<participants.length; i++){
if (participants[i].id == x.id)
return(true); }
return(false);
}
}
class Person
{int id;}
... if (p.participate(x)) ...
CODE SMELL/ BAD SMELL
Types of Code Smell
Shortgun Surgery
Example: Move Field
A field is, or will be, used by another class more than the class on
which it is defined.
CODE SMELL/ BAD SMELL
Types of Code Smell
Shortgun Surgery
Why ”Move Field”
Often fields are moved as part of the Extract Class technique
Deciding which class to leave the field in can be tough.
Here is our rule of thumb: put a field in the same place as the
methods that use it (or else where most of these methods are).
This rule will help in other cases when a field is simply located
in the wrong place.
CODE SMELL/ BAD SMELL
Types of Code Smell
Feature Envy
The whole point of objects is that; they are kind of technique
that package data with the processes used on that data.
When a method seems more interesting in a class, other than
the one in actually it is.
The most common of focus of envy is data
For example; we have a method that invokes half a dozen
getting methods on another object to calculate some value.
The indication is obvious that the methods wants to be
elsewhere, so we can simply use MOVE METHOD to give
the method ,its dream home.
We are reducing the coupling and enhancing the cohesion
CODE SMELL/ BAD SMELL
Types of Code Smell
Feature Envy
Move method
CODE SMELL/ BAD SMELL
Types of Code Smell
Data Clumps
Data items enjoy hanging around in groups together.
Often we see the same three or four data items together in
lots of places like : fields in a couple of classes, parameters in
method signatures
These bunches of data ought to be made into their own object
Then apply Introduce parameter Object re-factoring ”
CODE SMELL/ BAD SMELL
Types of Code Smell
Comments
Comments are not bad smell.
Author has named them a Sweet Smell
When you feel the need to write comment,first try to re-factor
the code so that any comment becomes superfluous.
Comments are often used as Deodorant to the bad smell.
For example if we need a comment to explain what a block of
code does; try Extract method,if extract method is already
done , then Rename Method.
The purpose of Comments should be only ”Why you are
Doing Something(To help future modifiers)” rather than
”What code is doing ”
CODE SMELL/ BAD SMELL
Advanced....
Two Level Dynamic
Approach for Feature Envy
Detection
CODE SMELL/ BAD SMELL
Advanced....
Two Level Dynamic Approach for Feature Envy Detection
To refactor the code, it must be known which part of code
needs to be refactored.
For this purpose code smells are used.
In this field dynamic analysis is still in dark, which is an
opportunity to explore in this direction also.
To tackle this problem, we have devised a two level
mechanism
. First level filters out the methods which are suspects of being
feature envious
Second level analyzes those suspects to identify the actual
culprits.
CODE SMELL/ BAD SMELL
Advanced....
Two Level Dynamic Approach for Feature Envy Detection
Advantage Over Static Techniques
Static Techniques analyze the source code statically, which
is less accurate in object oriented environment.
The detection mechanism is applied over complete code
blindly, irrespective of whether they are completely free of any
kind of smell or not, which is an overhead
Dynamic analysis is a technique that analyzes the data
gathered during program executions , instead of analysing its
source code.
By executing a program, we can obtain an actual program
behaviour , which cannot be obtained from only analysing its
source code.
CODE SMELL/ BAD SMELL
Advanced....
Two Level Dynamic Approach for Feature Envy Detection
Purposed Work
CODE SMELL/ BAD SMELL
Advanced....
Two Level Dynamic Approach for Feature Envy Detection
Working of feature envy detection mechanism
CODE SMELL/ BAD SMELL
Conclusion
Conclusion
Code Smell detection is a challenging task.
it can be said that use of dynamic analysis can be
advantageous in detection of other types of code smells also
and will be a useful and efficient approach for software
maintainers.
CODE SMELL/ BAD SMELL
References
References
1 M. Fowler, K. Beck, J. Brant, W. Opdyke, and D. Roberts,
Refactoring: Improving the Design of Existing Code.
Addison-Wesley, 1999.
Swati, Jitender Kumar Chhabra,Two Level Dynamic Approach
for Feature Envy Detection;ICCCT [2014].
https://www.youtube.com/watch?v=vqEg37e4Mkw; a
seminar by Martin Fowler on ”Workflows of Refactoring”
CODE SMELL/ BAD SMELL
References
Thank you !

More Related Content

What's hot (20)

PPTX
Delegates and events in C#
Dr.Neeraj Kumar Pandey
 
PPTX
daa-unit-3-greedy method
hodcsencet
 
PPTX
Code Smell, Software Engineering
Anshul Vinayak
 
PDF
Symbol table in compiler Design
Kuppusamy P
 
PPTX
Syntax Analysis in Compiler Design
MAHASREEM
 
PDF
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Philip Schwarz
 
PPTX
Function overloading and overriding
Rajab Ali
 
PPT
Refactoring Tips by Martin Fowler
Igor Crvenov
 
PPTX
Java abstract class & abstract methods
Shubham Dwivedi
 
PPTX
Applet
swapnac12
 
PPT
1.Role lexical Analyzer
Radhakrishnan Chinnusamy
 
PDF
Syntax directed translation
Akshaya Arunan
 
PPTX
Specification-of-tokens
Dattatray Gandhmal
 
PPS
Interface
kamal kotecha
 
PPT
friend function(c++)
Ritika Sharma
 
PPT
FUNCTIONS IN c++ PPT
03062679929
 
PPTX
Basis path testing
Hoa Le
 
PPTX
Code Smells
Mrinal Bhattacaharya
 
PPT
C# Exceptions Handling
sharqiyem
 
PPT
Design Patterns
Anuja Arosha
 
Delegates and events in C#
Dr.Neeraj Kumar Pandey
 
daa-unit-3-greedy method
hodcsencet
 
Code Smell, Software Engineering
Anshul Vinayak
 
Symbol table in compiler Design
Kuppusamy P
 
Syntax Analysis in Compiler Design
MAHASREEM
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Philip Schwarz
 
Function overloading and overriding
Rajab Ali
 
Refactoring Tips by Martin Fowler
Igor Crvenov
 
Java abstract class & abstract methods
Shubham Dwivedi
 
Applet
swapnac12
 
1.Role lexical Analyzer
Radhakrishnan Chinnusamy
 
Syntax directed translation
Akshaya Arunan
 
Specification-of-tokens
Dattatray Gandhmal
 
Interface
kamal kotecha
 
friend function(c++)
Ritika Sharma
 
FUNCTIONS IN c++ PPT
03062679929
 
Basis path testing
Hoa Le
 
C# Exceptions Handling
sharqiyem
 
Design Patterns
Anuja Arosha
 

Similar to Code Smells and Its type (With Example) (20)

PPTX
Software Craftsmanship - 2
Uri Lavi
 
PDF
Bad Smell In Codes 1
A Walk in the World
 
PPTX
Baroda code smell and refactoring
Mamata Gelanee
 
PPTX
Code smells quality of code
Hasan Al Mamun
 
PPTX
Code smells and Other Malodorous Software Odors
Clint Edmonson
 
PDF
Code smells
Mina Tafreshi
 
PDF
Refactoring: Improve the design of existing code
Valerio Maggio
 
PDF
PhD Symposium 2014
Fabio Palomba
 
PPT
Bad Smell in Codes - Part 1
Shaer Hassan
 
PDF
When and Why Your Code Starts to Smell Bad
Fabio Palomba
 
PPTX
Bad smells in code
Đới Học viện Agile
 
PDF
Webcast: Identify and Correct Common Code Smells
Serge Baranovsky
 
PDF
Bade Smells in Code
Will Shen
 
PPTX
Code Smells and Refactoring - Satyajit Dey & Ashif Iqbal
Cefalo
 
ODP
Automated Refactoring
Janeve George
 
PPTX
Bad Smells in Code
Özge Nur KOÇ
 
PPTX
Code smells
Narayann Swaami
 
DOCX
A Study on Code Smell Detection with Refactoring Tools in Object Oriented Lan...
ijcnes
 
PDF
Refactoring
Xavier Fornés Arrabal
 
PPTX
Software Craftsmanship - Code Smells - Dispensables
Rajat Singla
 
Software Craftsmanship - 2
Uri Lavi
 
Bad Smell In Codes 1
A Walk in the World
 
Baroda code smell and refactoring
Mamata Gelanee
 
Code smells quality of code
Hasan Al Mamun
 
Code smells and Other Malodorous Software Odors
Clint Edmonson
 
Code smells
Mina Tafreshi
 
Refactoring: Improve the design of existing code
Valerio Maggio
 
PhD Symposium 2014
Fabio Palomba
 
Bad Smell in Codes - Part 1
Shaer Hassan
 
When and Why Your Code Starts to Smell Bad
Fabio Palomba
 
Bad smells in code
Đới Học viện Agile
 
Webcast: Identify and Correct Common Code Smells
Serge Baranovsky
 
Bade Smells in Code
Will Shen
 
Code Smells and Refactoring - Satyajit Dey & Ashif Iqbal
Cefalo
 
Automated Refactoring
Janeve George
 
Bad Smells in Code
Özge Nur KOÇ
 
Code smells
Narayann Swaami
 
A Study on Code Smell Detection with Refactoring Tools in Object Oriented Lan...
ijcnes
 
Software Craftsmanship - Code Smells - Dispensables
Rajat Singla
 
Ad

Recently uploaded (20)

DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
PDF
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
PPTX
Depth First Search Algorithm in 🧠 DFS in Artificial Intelligence (AI)
rafeeqshaik212002
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PPTX
Introduction to Design of Machine Elements
PradeepKumarS27
 
PPTX
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
PDF
GTU Civil Engineering All Semester Syllabus.pdf
Vimal Bhojani
 
PPTX
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
PDF
Zilliz Cloud Demo for performance and scale
Zilliz
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PDF
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
PDF
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
PPTX
Green Building & Energy Conservation ppt
Sagar Sarangi
 
PDF
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
PDF
Unified_Cloud_Comm_Presentation anil singh ppt
anilsingh298751
 
PPTX
Evaluation and thermal analysis of shell and tube heat exchanger as per requi...
shahveer210504
 
PPTX
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
PPTX
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PPTX
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
Depth First Search Algorithm in 🧠 DFS in Artificial Intelligence (AI)
rafeeqshaik212002
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
Introduction to Design of Machine Elements
PradeepKumarS27
 
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
GTU Civil Engineering All Semester Syllabus.pdf
Vimal Bhojani
 
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
Zilliz Cloud Demo for performance and scale
Zilliz
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
Green Building & Energy Conservation ppt
Sagar Sarangi
 
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
Unified_Cloud_Comm_Presentation anil singh ppt
anilsingh298751
 
Evaluation and thermal analysis of shell and tube heat exchanger as per requi...
shahveer210504
 
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
Ad

Code Smells and Its type (With Example)

  • 1. CODE SMELL/ BAD SMELL CODE SMELL/ BAD SMELL Anshul National institute of Technology ,Kurukshetra April 10, 2016
  • 2. CODE SMELL/ BAD SMELL Overview 1 Motivation 2 Introduction Definition Code re-factoring 3 Types of Code Smell Duplicate Code Long method Large Class Divergent Change Shortgun Surgery Feature Envy Data Clumps Comments 4 Advanced.... Two Level Dynamic Approach for Feature Envy Detection 5 Conclusion
  • 3. CODE SMELL/ BAD SMELL Motivation Though Re-factoring is a old concept, developers are not utilizing it. To capture and write down the situation which make sense to re-factoring. Re factoring is directly related to Economics
  • 4. CODE SMELL/ BAD SMELL Introduction Definition Code smell :- Code smells are indications of poor coding and design choices that can cause problems during the later phase of software development. Code smells are considered as flags to the developer that some parts of the design may be inappropriate. Such design flaw that can be solved by re-factoring. According to MARTIN FOWLER ”IF it stinks change it.”
  • 5. CODE SMELL/ BAD SMELL Introduction Code re-factoring ”Refactoring is the process of changing a software system in such a way that it does not alter the external behavior of the code yet improves its internal structure. Refactoring is a form of program transformation which preserves the semantics of the program. Improving the design after it has been written. It is an iterative process.
  • 6. CODE SMELL/ BAD SMELL Types of Code Smell Duplicate Code Occurrence of Same code structure more than once. When we have same expression in two or more methods of same class. Solution for this code smell is to do Extract method and then invoke the code from both the places.
  • 7. CODE SMELL/ BAD SMELL Types of Code Smell Duplicate Code Example 1 extern int a[]; extern int b[]; int sumofa = 0; for (int i = 0; i < 4; i + +) sum += a[i]; int averageofa= sum/4; —————- int sumofb = 0; for (int i = 0; i < 4; i + +) sum += b[i]; int averageofb = sumofb/4; Extract method int calc-average(int* array) int sum= 0; for (int i = 0; i < 4; i + +) sum + =array[i]; return sum/4;
  • 8. CODE SMELL/ BAD SMELL Types of Code Smell Duplicate Code Example1 contd... Extract method will give source code that has no loop duplication extern int a[]; extern int b[]; int averageofa = calcAverage(a[]); int averageofb = calcAverage(b[]);
  • 9. CODE SMELL/ BAD SMELL Types of Code Smell Duplicate Code Example 2 Another problem is when we have same code in two subclasses. Solution:-(Extract method + pull up method ) extract similar codes from both the classes in form a method and then put this method in the superclass.
  • 10. CODE SMELL/ BAD SMELL Types of Code Smell Long method Brief Analysis and the experience says that the object programs which lives best and longest are those with short methods. To shorten a method all we have to do is to Extract a new method
  • 11. CODE SMELL/ BAD SMELL Types of Code Smell Long method Key points: IF method has a lot of parameters and variables , then these things comes in the form of obstacles to Extract method. Even if we try to do so , we will end up passing so many parameters and variables to newly Extracted method. Solution is to replace temp with query to eliminate temps. And to slim down the long parameters we may introduce a new parameter object And then preserving whole object.
  • 12. CODE SMELL/ BAD SMELL Types of Code Smell Long method Replace temp with Query double basePrice = quantity ∗ itemPrice; if (basePrice > 1000) return basePrice ∗ 0.95; else return basePrice ∗ 0.98; if (basePrice() > 1000) return basePrice() ∗ 0.95; else return basePrice() ∗ 0.98; ...——————— double basePrice() return quantity ∗ itemPrice;
  • 13. CODE SMELL/ BAD SMELL Types of Code Smell Long method New Parameter Object
  • 14. CODE SMELL/ BAD SMELL Types of Code Smell Long method Question: How to decide the clumps of code that we want to extract ?
  • 15. CODE SMELL/ BAD SMELL Types of Code Smell Long method Answer see comments ” . Even a single line is worth extracting if it needs explanations.
  • 16. CODE SMELL/ BAD SMELL Types of Code Smell Large Class 1 Signs and symptoms: When a single class is doing too much , it often shows up too many variables and instances OR can say ; A class having many fields/methods/lines of code is called as LARGE CLASS. 2 Reasons for the Problem: Classes usually start small. But over time, they get bloated as the program grows. Programmers usually find it mentally less taxing to place a new feature in an existing class than to create a new class for the feature. 3 Treatment Extract Class helps if part of the behavior of the large class can be spun off into a separate component. Extract SubClass helps if part of the behavior of the large class can be implemented in different ways or is used in rare cases.
  • 17. CODE SMELL/ BAD SMELL Types of Code Smell Large Class Extract Class
  • 18. CODE SMELL/ BAD SMELL Types of Code Smell Divergent Change When we make a change,we want to be able to a single clear point in the system and make the change. And if you can not do this, you are smelling one of the two closely related pungencies. Divergent change occurs when one class is commonly changed in different ways for different reasons. Split up the behavior of the class viaExtract Class Payoff Improves code organization. Reduces code duplication. Simplifies support.
  • 19. CODE SMELL/ BAD SMELL Types of Code Smell Divergent Change Example: class User def full−name ”#{first − name}#{last − name}” end def authorized?(action) is−admin? end end class User class UserPresenter <SimpleDelegator def full−name ”#{first − name}#{last − name}” end end class ActionAuthorizer def authorized?(action, user) user.is−admin? end end # nothing here? end
  • 20. CODE SMELL/ BAD SMELL Types of Code Smell Shortgun Surgery Shortgun surgery is opposite to divergent change This happens when, you want to make some kind of change, you are forced to make a lot of changes to a lot of different classes And when changes are all over the place, they are hard to find ,and it’s easy to miss an important change Use MOVE METHOD and MOVE FIELD to pull all the changes to a single class.
  • 21. CODE SMELL/ BAD SMELL Types of Code Smell Shortgun Surgery Example:Move Method class Project { Person[] participants; } class Person { int id; boolean participate(Project p) { for(int i = 0; i < p. participants .length ; i + +) { if (p.participants[i].id ==id) return(true); } return(false); } } ... if (x.participate(p)) ... class Project { Person[] participants; boolean participate(Person x) { for(int i= 0; i<participants.length; i++){ if (participants[i].id == x.id) return(true); } return(false); } } class Person {int id;} ... if (p.participate(x)) ...
  • 22. CODE SMELL/ BAD SMELL Types of Code Smell Shortgun Surgery Example: Move Field A field is, or will be, used by another class more than the class on which it is defined.
  • 23. CODE SMELL/ BAD SMELL Types of Code Smell Shortgun Surgery Why ”Move Field” Often fields are moved as part of the Extract Class technique Deciding which class to leave the field in can be tough. Here is our rule of thumb: put a field in the same place as the methods that use it (or else where most of these methods are). This rule will help in other cases when a field is simply located in the wrong place.
  • 24. CODE SMELL/ BAD SMELL Types of Code Smell Feature Envy The whole point of objects is that; they are kind of technique that package data with the processes used on that data. When a method seems more interesting in a class, other than the one in actually it is. The most common of focus of envy is data For example; we have a method that invokes half a dozen getting methods on another object to calculate some value. The indication is obvious that the methods wants to be elsewhere, so we can simply use MOVE METHOD to give the method ,its dream home. We are reducing the coupling and enhancing the cohesion
  • 25. CODE SMELL/ BAD SMELL Types of Code Smell Feature Envy Move method
  • 26. CODE SMELL/ BAD SMELL Types of Code Smell Data Clumps Data items enjoy hanging around in groups together. Often we see the same three or four data items together in lots of places like : fields in a couple of classes, parameters in method signatures These bunches of data ought to be made into their own object Then apply Introduce parameter Object re-factoring ”
  • 27. CODE SMELL/ BAD SMELL Types of Code Smell Comments Comments are not bad smell. Author has named them a Sweet Smell When you feel the need to write comment,first try to re-factor the code so that any comment becomes superfluous. Comments are often used as Deodorant to the bad smell. For example if we need a comment to explain what a block of code does; try Extract method,if extract method is already done , then Rename Method. The purpose of Comments should be only ”Why you are Doing Something(To help future modifiers)” rather than ”What code is doing ”
  • 28. CODE SMELL/ BAD SMELL Advanced.... Two Level Dynamic Approach for Feature Envy Detection
  • 29. CODE SMELL/ BAD SMELL Advanced.... Two Level Dynamic Approach for Feature Envy Detection To refactor the code, it must be known which part of code needs to be refactored. For this purpose code smells are used. In this field dynamic analysis is still in dark, which is an opportunity to explore in this direction also. To tackle this problem, we have devised a two level mechanism . First level filters out the methods which are suspects of being feature envious Second level analyzes those suspects to identify the actual culprits.
  • 30. CODE SMELL/ BAD SMELL Advanced.... Two Level Dynamic Approach for Feature Envy Detection Advantage Over Static Techniques Static Techniques analyze the source code statically, which is less accurate in object oriented environment. The detection mechanism is applied over complete code blindly, irrespective of whether they are completely free of any kind of smell or not, which is an overhead Dynamic analysis is a technique that analyzes the data gathered during program executions , instead of analysing its source code. By executing a program, we can obtain an actual program behaviour , which cannot be obtained from only analysing its source code.
  • 31. CODE SMELL/ BAD SMELL Advanced.... Two Level Dynamic Approach for Feature Envy Detection Purposed Work
  • 32. CODE SMELL/ BAD SMELL Advanced.... Two Level Dynamic Approach for Feature Envy Detection Working of feature envy detection mechanism
  • 33. CODE SMELL/ BAD SMELL Conclusion Conclusion Code Smell detection is a challenging task. it can be said that use of dynamic analysis can be advantageous in detection of other types of code smells also and will be a useful and efficient approach for software maintainers.
  • 34. CODE SMELL/ BAD SMELL References References 1 M. Fowler, K. Beck, J. Brant, W. Opdyke, and D. Roberts, Refactoring: Improving the Design of Existing Code. Addison-Wesley, 1999. Swati, Jitender Kumar Chhabra,Two Level Dynamic Approach for Feature Envy Detection;ICCCT [2014]. https://www.youtube.com/watch?v=vqEg37e4Mkw; a seminar by Martin Fowler on ”Workflows of Refactoring”
  • 35. CODE SMELL/ BAD SMELL References Thank you !