Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.
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 Cod...
CODE SMELL/ BAD SMELL
Motivation
Though Re-factoring is a old concept, developers are not
utilizing it.
To capture and wri...
CODE SMELL/ BAD SMELL
Introduction
Definition
Code smell :- Code smells are indications of poor coding and
design choices t...
CODE SMELL/ BAD SMELL
Introduction
Code re-factoring
”Refactoring is the process of changing a software system in
such a w...
CODE SMELL/ BAD SMELL
Types of Code Smell
Duplicate Code
Occurrence of Same code structure more than once.
When we have sa...
CODE SMELL/ BAD SMELL
Types of Code Smell
Duplicate Code
Example 1
extern int a[];
extern int b[];
int sumofa = 0;
for (in...
CODE SMELL/ BAD SMELL
Types of Code Smell
Duplicate Code
Example1 contd...
Extract method will give source code that has n...
CODE SMELL/ BAD SMELL
Types of Code Smell
Duplicate Code
Example 2
Another problem is when we have same code in two
subcla...
CODE SMELL/ BAD SMELL
Types of Code Smell
Long method
Brief
Analysis and the experience says that the object programs
whic...
CODE SMELL/ BAD SMELL
Types of Code Smell
Long method
Key points:
IF method has a lot of parameters and variables , then t...
CODE SMELL/ BAD SMELL
Types of Code Smell
Long method
Replace temp with Query
double basePrice
= quantity ∗ itemPrice;
if ...
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...
CODE SMELL/ BAD SMELL
Types of Code Smell
Large Class
1 Signs and symptoms:
When a single class is doing too much , it oft...
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
poin...
CODE SMELL/ BAD SMELL
Types of Code Smell
Divergent Change
Example:
class User
def full−name
”#{first − name}#{last −
name}...
CODE SMELL/ BAD SMELL
Types of Code Smell
Shortgun Surgery
Shortgun surgery is opposite to divergent change
This happens w...
CODE SMELL/ BAD SMELL
Types of Code Smell
Shortgun Surgery
Example:Move Method
class Project {
Person[] participants;
}
cl...
CODE SMELL/ BAD SMELL
Types of Code Smell
Shortgun Surgery
Example: Move Field
A field is, or will be, used by another clas...
CODE SMELL/ BAD SMELL
Types of Code Smell
Shortgun Surgery
Why ”Move Field”
Often fields are moved as part of the Extract C...
CODE SMELL/ BAD SMELL
Types of Code Smell
Feature Envy
The whole point of objects is that; they are kind of technique
that...
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...
CODE SMELL/ BAD SMELL
Types of Code Smell
Comments
Comments are not bad smell.
Author has named them a Sweet Smell
When yo...
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 ...
CODE SMELL/ BAD SMELL
Advanced....
Two Level Dynamic Approach for Feature Envy Detection
Advantage Over Static Techniques
...
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...
CODE SMELL/ BAD SMELL
Conclusion
Conclusion
Code Smell detection is a challenging task.
it can be said that use of dynamic...
CODE SMELL/ BAD SMELL
References
References
1 M. Fowler, K. Beck, J. Brant, W. Opdyke, and D. Roberts,
Refactoring: Improv...
CODE SMELL/ BAD SMELL
References
Thank you !
Próxima SlideShare
Cargando en…5
×

Code Smells and Its type (With Example)

4.304 visualizaciones

Publicado el

Different Code smells are described with different examples.

Publicado en: Ingeniería

Code Smells and Its type (With Example)

  1. 1. CODE SMELL/ BAD SMELL CODE SMELL/ BAD SMELL Anshul National institute of Technology ,Kurukshetra April 10, 2016
  2. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 13. CODE SMELL/ BAD SMELL Types of Code Smell Long method New Parameter Object
  14. 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. 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. 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. 17. CODE SMELL/ BAD SMELL Types of Code Smell Large Class Extract Class
  18. 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. 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. 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. 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. 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. 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. 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. 25. CODE SMELL/ BAD SMELL Types of Code Smell Feature Envy Move method
  26. 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. 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. 28. CODE SMELL/ BAD SMELL Advanced.... Two Level Dynamic Approach for Feature Envy Detection
  29. 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. 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. 31. CODE SMELL/ BAD SMELL Advanced.... Two Level Dynamic Approach for Feature Envy Detection Purposed Work
  32. 32. CODE SMELL/ BAD SMELL Advanced.... Two Level Dynamic Approach for Feature Envy Detection Working of feature envy detection mechanism
  33. 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. 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. 35. CODE SMELL/ BAD SMELL References Thank you !

×