code smell - classes.cs.uchicago.edu · why are code smells bad? •they are clear signs that your...

31
Code smell

Upload: others

Post on 04-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Code smell - classes.cs.uchicago.edu · Why are code smells bad? •They are clear signs that your design is starting to decay •Long term decay leads to “software rot” 1-3

Code smell

Page 2: Code smell - classes.cs.uchicago.edu · Why are code smells bad? •They are clear signs that your design is starting to decay •Long term decay leads to “software rot” 1-3

What are code smells?

• Fowler: “... certain structures in the code that suggest (sometimes they scream for) the possibility of refactoring.”

• Wikipedia: “… symptom[s] in the source code of a program that possibly indicate a deeper problem. … usually not bugs... not technically incorrect and don't currently prevent the program from functioning. Instead, they indicate weaknesses in design that may be slowing down development or increasing the risk of bugs or failures in the future.”

1-2

Page 3: Code smell - classes.cs.uchicago.edu · Why are code smells bad? •They are clear signs that your design is starting to decay •Long term decay leads to “software rot” 1-3

Why are code smells bad?

• They are clear signs that your design is starting to decay

• Long term decay leads to “software rot”

1-3

Page 4: Code smell - classes.cs.uchicago.edu · Why are code smells bad? •They are clear signs that your design is starting to decay •Long term decay leads to “software rot” 1-3

Example code smells

• Duplicated code

• Long method

• Large class

• Long parameter list

• Message chain

• Switch statements

• Data class

• Speculative generality

• Temporary field

• Refused bequest

4

Page 5: Code smell - classes.cs.uchicago.edu · Why are code smells bad? •They are clear signs that your design is starting to decay •Long term decay leads to “software rot” 1-3

Duplicated code

• Duplicate methods in subclasses• ??

• Duplicate expressions in same class• ??

• Duplicate expressions in different classes• ??

5

Page 6: Code smell - classes.cs.uchicago.edu · Why are code smells bad? •They are clear signs that your design is starting to decay •Long term decay leads to “software rot” 1-3

Duplicated code

• Duplicate methods in subclasses• Lift to super class

• Duplicate expressions in same class• Create new member method (maybe private method)

• Duplicate expressions in different classes• Maybe create another class to offer the common computation

6

Page 7: Code smell - classes.cs.uchicago.edu · Why are code smells bad? •They are clear signs that your design is starting to decay •Long term decay leads to “software rot” 1-3

Long method

• Won’t fit on a page

• Can’t think of whole thing at once

• Extract function• Where to extract?

7

Page 8: Code smell - classes.cs.uchicago.edu · Why are code smells bad? •They are clear signs that your design is starting to decay •Long term decay leads to “software rot” 1-3

Long method

• Won’t fit on a page

• Can’t think of whole thing at once

• Extract function• Loop body

• Places where there is (or should be) a comment

8

Page 9: Code smell - classes.cs.uchicago.edu · Why are code smells bad? •They are clear signs that your design is starting to decay •Long term decay leads to “software rot” 1-3

9

Large class

• More than a couple dozen methods, or half a dozen variables

• How to make the class small?

Page 10: Code smell - classes.cs.uchicago.edu · Why are code smells bad? •They are clear signs that your design is starting to decay •Long term decay leads to “software rot” 1-3

10

Large class

• More than a couple dozen methods, or half a dozen variables

• Split into component classes

• Create superclass• If using switch statement, split into subclasses

Page 11: Code smell - classes.cs.uchicago.edu · Why are code smells bad? •They are clear signs that your design is starting to decay •Long term decay leads to “software rot” 1-3

11

Long parameter list

Page 12: Code smell - classes.cs.uchicago.edu · Why are code smells bad? •They are clear signs that your design is starting to decay •Long term decay leads to “software rot” 1-3

12

Long parameter list

• Introduce parameter object

• Only worthwhile if there are several methods with same parameter list, and they call each other

Page 13: Code smell - classes.cs.uchicago.edu · Why are code smells bad? •They are clear signs that your design is starting to decay •Long term decay leads to “software rot” 1-3

Message chain

• Long list of method calls:customer.getAddress().getState()window.getBoundingbox().getOrigin().getX()

13

Page 14: Code smell - classes.cs.uchicago.edu · Why are code smells bad? •They are clear signs that your design is starting to decay •Long term decay leads to “software rot” 1-3

Message chain

• Long list of method calls:customer.getAddress().getState()window.getBoundingbox().getOrigin().getX()

• How to change this?

1. box=window.getBoundingbox();

boxx=box.getOrigin().getX();

2. window.getBoundingbox().getXOrigin();

14

Page 15: Code smell - classes.cs.uchicago.edu · Why are code smells bad? •They are clear signs that your design is starting to decay •Long term decay leads to “software rot” 1-3

Message chain

• Long list of method calls:customer.getAddress().getState()window.getBoundingbox().getOrigin().getX()

• Replace with shorter calls:customer.getState()window.leftBoundary()

15

Page 16: Code smell - classes.cs.uchicago.edu · Why are code smells bad? •They are clear signs that your design is starting to decay •Long term decay leads to “software rot” 1-3

16

Data class

• Class has no methods except for getter and setters

• What to do:• ??

Page 17: Code smell - classes.cs.uchicago.edu · Why are code smells bad? •They are clear signs that your design is starting to decay •Long term decay leads to “software rot” 1-3

17

Data class

• Class has no methods except for getter and setters

• What to do:• Look for missing methods and move them to the class

• Merge with another class

Page 18: Code smell - classes.cs.uchicago.edu · Why are code smells bad? •They are clear signs that your design is starting to decay •Long term decay leads to “software rot” 1-3

18

Switch statement

• (Long) if-else

• Switch case case case

• How to change?

Page 19: Code smell - classes.cs.uchicago.edu · Why are code smells bad? •They are clear signs that your design is starting to decay •Long term decay leads to “software rot” 1-3

Library example

class Book : Element …

class Collection : Element …

int computeWords(Element e) {if (!e.hasChildren()) { // e instanceof Book

return ((Book)e).getBookWords();} else {

return ((Collection)e).getTotalWords();

}

1-19

Page 20: Code smell - classes.cs.uchicago.edu · Why are code smells bad? •They are clear signs that your design is starting to decay •Long term decay leads to “software rot” 1-3

Library example

int computeWords(Element e) {if (!e.hasChildren()) { // e instanceof Book

return ((Book)e).getBookWords();} else {

return ((Collection)e).getTotalWords();

• Replace with a new method:

int computeWord(Element e) {return e.getWord();

}

1-20

Page 21: Code smell - classes.cs.uchicago.edu · Why are code smells bad? •They are clear signs that your design is starting to decay •Long term decay leads to “software rot” 1-3

Speculative generality

• What are the examples?

1-21

Page 22: Code smell - classes.cs.uchicago.edu · Why are code smells bad? •They are clear signs that your design is starting to decay •Long term decay leads to “software rot” 1-3

Speculative generality

• Interfaces/abstract classes that are implemented only one class

• Unused parameters

1-22

Page 23: Code smell - classes.cs.uchicago.edu · Why are code smells bad? •They are clear signs that your design is starting to decay •Long term decay leads to “software rot” 1-3

23

Temporary field

• Instance variable is only used during part of the lifetime of an object

• Move variable into another object (perhaps a new class)

Page 24: Code smell - classes.cs.uchicago.edu · Why are code smells bad? •They are clear signs that your design is starting to decay •Long term decay leads to “software rot” 1-3

24

Refused bequest

• A is a subclass of B

• A • Overrides inherited methods of B

• Does not use some variables of B

• Does not use some methods of B

Page 25: Code smell - classes.cs.uchicago.edu · Why are code smells bad? •They are clear signs that your design is starting to decay •Long term decay leads to “software rot” 1-3

25

Refused bequest

• A is a subclass of B

• A • Overrides inherited methods of B

• Does not use some variables of B

• Does not use some methods of B

• What should we do?

Page 26: Code smell - classes.cs.uchicago.edu · Why are code smells bad? •They are clear signs that your design is starting to decay •Long term decay leads to “software rot” 1-3

26

Refused bequest

• A is a subclass of B

• A • Overrides inherited methods of B

• Does not use some variables of B

• Does not use some methods of B

• Give A and B a common superclass and move common code into it

Page 27: Code smell - classes.cs.uchicago.edu · Why are code smells bad? •They are clear signs that your design is starting to decay •Long term decay leads to “software rot” 1-3

27

Other smells

• Non-localized plans

• Too many bugs

• Too hard to understand

• Too hard to change

Page 28: Code smell - classes.cs.uchicago.edu · Why are code smells bad? •They are clear signs that your design is starting to decay •Long term decay leads to “software rot” 1-3

28

Too many bugs

• If one part of the system has more than its share of the bugs, there is probably a good reason

• Redesign, rewrite, refactor

Page 29: Code smell - classes.cs.uchicago.edu · Why are code smells bad? •They are clear signs that your design is starting to decay •Long term decay leads to “software rot” 1-3

29

Too hard to understand

• Hard to fix bugs because you don’t understand

• Hard to change because you don’t understand

Page 30: Code smell - classes.cs.uchicago.edu · Why are code smells bad? •They are clear signs that your design is starting to decay •Long term decay leads to “software rot” 1-3

30

Too hard to change

• Because of lack of tests

• Because of dependencies• Global variables

• Very large modules

• Importing too many classes

• Because of duplication or non-localized plans

Page 31: Code smell - classes.cs.uchicago.edu · Why are code smells bad? •They are clear signs that your design is starting to decay •Long term decay leads to “software rot” 1-3

Summary

• Code smells are code pieces with potentially bad design

• Fairly subjective• Fowler: “You will have to develop your own sense of how many instance

variables are too many instance variables and how many lines of code in a method are too many lines.”

1-31