Day2 - Refactoring (Lecture SS 2015)

58
TDD + JavaScript Refactoring Wolfram Kriesing, uxebu @wolframkriesing

Transcript of Day2 - Refactoring (Lecture SS 2015)

Page 1: Day2 - Refactoring (Lecture SS 2015)

TDD + JavaScript

Refactoring

Wolfram Kriesing, uxebu @wolframkriesing

Page 2: Day2 - Refactoring (Lecture SS 2015)

Sche

dule

Intro Collect Dive in Crowd Start Solid

break

we hack

Page 3: Day2 - Refactoring (Lecture SS 2015)

What did you take away?

Page 4: Day2 - Refactoring (Lecture SS 2015)

ReminderSoftware

Craftsmanship

Apprentice Journeyman

Master

Page 5: Day2 - Refactoring (Lecture SS 2015)

no IFs

Page 6: Day2 - Refactoring (Lecture SS 2015)
Page 7: Day2 - Refactoring (Lecture SS 2015)

Refactoring

Page 8: Day2 - Refactoring (Lecture SS 2015)

Design is hard.The design of

reusable software is especially hard.

Reusable software usually is the result of many design iterations.

William F. Opdyke, 1992

it’s a

ll abo

ut d

esig

n

Page 9: Day2 - Refactoring (Lecture SS 2015)

Books, papers• „REFACTORING OBJECT-ORIENTED FRAMEWORKS“

William F. Opdyke, 1992 http://www.ai.univ-paris8.fr/~lysop/opdyke-thesis.pdf

• „Refactoring: Improving the Design of Existing Code“Martin Fowler, 1999

• „Working Effectively with Legacy Code“Michael Feathers, 2004

• „31 Days of Refactoring“http://lostechies.com/wp-content/uploads/2011/03/31DaysRefactoring.pdf

• „Subjective evaluation of software evolvability using code smells: An empirical study“ http://www.soberit.hut.fi/~mmantyla/ESE_2006.pdf

Page 10: Day2 - Refactoring (Lecture SS 2015)

http://www.testingeducation.org/pt/Refactoring-smells.pdf

Page 11: Day2 - Refactoring (Lecture SS 2015)

Our software …

Page 12: Day2 - Refactoring (Lecture SS 2015)

we plan first

Page 13: Day2 - Refactoring (Lecture SS 2015)

over time …

Page 14: Day2 - Refactoring (Lecture SS 2015)

https://www.flickr.com/photos/ronipothead/6294148752

it gets rusty …

Page 15: Day2 - Refactoring (Lecture SS 2015)

we should …

Page 16: Day2 - Refactoring (Lecture SS 2015)
Page 17: Day2 - Refactoring (Lecture SS 2015)

improve design after code was written

Page 18: Day2 - Refactoring (Lecture SS 2015)

Improve design after code was written

what?

isn’t it reverse?

Page 19: Day2 - Refactoring (Lecture SS 2015)

Tennis Let’s collect smells!

Page 20: Day2 - Refactoring (Lecture SS 2015)

Where does it come from?

Page 21: Day2 - Refactoring (Lecture SS 2015)
Page 22: Day2 - Refactoring (Lecture SS 2015)

smalltalk

Page 23: Day2 - Refactoring (Lecture SS 2015)

- made it popular - enables agility

get from the book: - code smells - refactoring techniques

Page 24: Day2 - Refactoring (Lecture SS 2015)
Page 25: Day2 - Refactoring (Lecture SS 2015)

Incremental Design

Improve structure all the timewas not possible with this tower, of course

Page 26: Day2 - Refactoring (Lecture SS 2015)

https://www.flickr.com/photos/jessicabee/897482687/

Baby Steps to

Stay Green

Page 27: Day2 - Refactoring (Lecture SS 2015)

Refactoring in action

Page 28: Day2 - Refactoring (Lecture SS 2015)

Refactoring in action

• monolithic

• >1 indention levels

• nested loops

• modular

• explicit function names

• tiny functions

• max. one level indent

• no nested loops

• composing

Page 29: Day2 - Refactoring (Lecture SS 2015)

• transparent responsibilities • separation of concerns • possible reusability • code structure • no unmaintained comments

Page 30: Day2 - Refactoring (Lecture SS 2015)

• transparent responsibilities • separation of concerns • possible reusability • code structure

Page 31: Day2 - Refactoring (Lecture SS 2015)
Page 32: Day2 - Refactoring (Lecture SS 2015)

What are code smells?

Page 33: Day2 - Refactoring (Lecture SS 2015)

http://james.padolsey.com/javascript/jquery-code-smells/

Page 34: Day2 - Refactoring (Lecture SS 2015)

$ prefixing

Artificial selectors

Inefficient jQuery usage

Hard-code dependencies

Chain explosion

No-caching

Page 35: Day2 - Refactoring (Lecture SS 2015)

It’s all automatically detectable!

Explore

Stabilize

Commoditize

e.g. with esprima

Page 36: Day2 - Refactoring (Lecture SS 2015)

:-)

Page 37: Day2 - Refactoring (Lecture SS 2015)

Common Code Smells

https://www.flickr.com/photos/awnisalan/352498081

Page 38: Day2 - Refactoring (Lecture SS 2015)

Common Code Smells

https://www.flickr.com/photos/awnisalan/352498081

1) Low-hanging fruit

Page 39: Day2 - Refactoring (Lecture SS 2015)

Comments• may be wrong

• they rot

• useless

• actual good (method) name

Page 40: Day2 - Refactoring (Lecture SS 2015)

Long methods• hard to read

• many contexts

• too many LOC

• too many statements

• define your own

Page 41: Day2 - Refactoring (Lecture SS 2015)

Duplicated code• duplicated bugs

• double the fixes

• more code to ship

• performance break

Page 42: Day2 - Refactoring (Lecture SS 2015)

Bad names

Page 43: Day2 - Refactoring (Lecture SS 2015)

Long parameter list• too many responsibilities

• bad API

• hard to use

• hard to maintain

• points to design defects

Page 44: Day2 - Refactoring (Lecture SS 2015)

Boolean trap• intention not clear

• easy to understand wrong

Page 45: Day2 - Refactoring (Lecture SS 2015)

Complexity

Page 46: Day2 - Refactoring (Lecture SS 2015)

Inconsistent names

Dead code

many more …

Page 47: Day2 - Refactoring (Lecture SS 2015)

Common Code Smells

https://www.flickr.com/photos/awnisalan/352498081

1) Low-hanging fruit 2) Boomerang

Page 48: Day2 - Refactoring (Lecture SS 2015)

Conditionals

Combinatorial Explosion

Type embedding

Data Clumpssets of variables that are always together, get passed in, etc. => extract into class

avoid placing types in method names; it's not only redundant, but it forces you to change the name if the type changes

You have lots of code that does almost the same thing

watch out for large conditional logic blocksparticularly blocks that tend to grow larger or change significantly over time

Page 49: Day2 - Refactoring (Lecture SS 2015)

Feature envya method making more use of another class than the one it is in

Speculative Generalitywrite code to solve today's problems

and worry about tomorrow's problems when they actually materialize

Page 50: Day2 - Refactoring (Lecture SS 2015)

Common Code Smells

https://www.flickr.com/photos/awnisalan/352498081

1) Low-hanging fruit 2) Boomerang 3) In tests

Page 51: Day2 - Refactoring (Lecture SS 2015)

Test smells

http://xunitpatterns.com/Test%20Smells.html

Page 52: Day2 - Refactoring (Lecture SS 2015)

Obscure testit is difficult to understand the test at a glance

Conditional test logic• tests take too long to run

• make developers not run the tests

• leads to untested code

• demotivates continuous testing

• make code rot

Page 53: Day2 - Refactoring (Lecture SS 2015)

Setup too complex

Unclear Act

Multiple asserts

No assert

Page 54: Day2 - Refactoring (Lecture SS 2015)

Logic in test

Duplication in tests

Dependent tests

Modification of unit under test

Page 55: Day2 - Refactoring (Lecture SS 2015)

General fixturea setUp method contains code only used by some tests

Lazy testusing the same fixture for multiple tests

Test run wartest can only be run by one person at a time, due to use of same external ressource

Page 56: Day2 - Refactoring (Lecture SS 2015)

JavaScript Code Smells

https://www.flickr.com/photos/brian-fitzgerald/3334353375

?

Page 57: Day2 - Refactoring (Lecture SS 2015)

Refactorings

Page 58: Day2 - Refactoring (Lecture SS 2015)

http://refactoring.com/catalog/