+ All Categories
Home > Internet > Android Refactoring

Android Refactoring

Date post: 21-Jan-2018
Category:
Upload: godfrey-nolan
View: 451 times
Download: 0 times
Share this document with a friend
53
Refactoring Godfrey Nolan
Transcript
Page 1: Android Refactoring

RefactoringGodfrey Nolan

Page 2: Android Refactoring

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand.”

- Martin Fowler

Page 3: Android Refactoring

Android Activities

9/25/17 Refactoring 3

Page 4: Android Refactoring

Refactoring (noun): a change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior.

"Refactor (verb): to restructure software by applying a series of refactorings without changing its observable behavior".

- Martin Fowler

Page 5: Android Refactoring

Goals

9/25/17 Refactoring 5

Page 6: Android Refactoring

"Technical debt ..[is]... the implied cost of additional rework caused by choosing an easy solution now instead of using a better approach that would take longer".

- Wikipedia

Page 7: Android Refactoring

9/25/17 Refactoring 6

Page 8: Android Refactoring
Page 9: Android Refactoring

9/25/17 Refactoring 7

Page 10: Android Refactoring

Keep it Objective

9/25/17 Refactoring 10

Page 11: Android Refactoring

Keep it Objective

9/25/17 Refactoring 11

Page 12: Android Refactoring

9/25/17 Refactoring 6

Page 13: Android Refactoring

SonarCloud.io

9/25/17 Refactoring 13

Page 14: Android Refactoring

SonarCloud.io

9/25/17 Refactoring 14

Page 15: Android Refactoring

Metrics Reloaded

9/25/17 Refactoring 15

Page 16: Android Refactoring

Metrics Reloaded

9/25/17 Refactoring 16

Page 17: Android Refactoring

What’s the Process?

9/25/17 Refactoring 17

Page 18: Android Refactoring

Refactor

9/25/17 Refactoring 18

Page 19: Android Refactoring

Refactor

9/25/17 Refactoring 19

Page 20: Android Refactoring

Rename

9/25/17 Refactoring 20

Page 21: Android Refactoring

Move

9/25/17 Refactoring 21

Page 22: Android Refactoring

Extract Method

9/25/17 Refactoring 22

Page 23: Android Refactoring

Extract Layout

9/25/17 Refactoring 23

Page 24: Android Refactoring

Pull Members Up

9/25/17 Refactoring 24

Page 25: Android Refactoring

Pull Members Down

9/25/17 Refactoring 25

Page 26: Android Refactoring

Convert Anonymous to Inner

9/25/17 Refactoring 26

Page 27: Android Refactoring

Replace Temp with Query

9/25/17 Refactoring 27

Page 28: Android Refactoring

Lets Begin

9/25/17 Refactoring 28

Page 29: Android Refactoring

Lets Begin

9/25/17 Refactoring 29

Page 30: Android Refactoring

Lets Begin – Kent Beck Rules

9/25/17 Refactoring 30

• Passes all the tests

• Reveals Intent

• Don’t repeat yourself

• Fewer Parts

Page 31: Android Refactoring

Example 1

9/25/17 Refactoring 31

Page 32: Android Refactoring

Example 1 – Extract Method

9/25/17 Refactoring 32

Page 33: Android Refactoring

Example 1 – Rename

9/25/17 Refactoring 33

Page 34: Android Refactoring

Example 1 – Move Method

9/25/17 Refactoring 34

Page 35: Android Refactoring

Example 1 – Replace Temp With Query

9/25/17 Refactoring 35

Page 36: Android Refactoring

Example 1 – Extract Method

9/25/17 Refactoring 36

Page 37: Android Refactoring

Example 1 – Replace Temp With Query

9/25/17 Refactoring 37

Page 38: Android Refactoring

Example 1 – Replace Temp With Query

9/25/17 Refactoring 38

Page 39: Android Refactoring

Example 1 – Measure Once, Cut Twice

9/25/17 Refactoring 39

Page 40: Android Refactoring

9/25/17 Refactoring 40

public String statement() {

double totalAmount = 0;

int frequentRenterPoints = 0;

Enumeration rentals = _rentals.elements();

String result = "Rental Record for " + getName() + "\n";

while (rentals.hasMoreElements()) {

double thisAmount = 0;

Rental each = (Rental) rentals.nextElement();

//determine amounts for each line

switch (each.getMovie().getPriceCode()) {

case Movie.REGULAR:

thisAmount += 2;

if (each.getDaysRented() > 2)

thisAmount += (each.getDaysRented() - 2) * 1.5;

break;

case Movie.NEW_RELEASE:

thisAmount += each.getDaysRented() * 3;

break;

case Movie.CHILDRENS:

thisAmount += 1.5;

if (each.getDaysRented() > 3)

thisAmount += (each.getDaysRented() - 3) * 1.5;

break;

}

// add frequent renter points

frequentRenterPoints++;

// add bonus for a two day new release rental

if ((each.getMovie().getPriceCode() == Movie.NEW_RELEASE) &&

each.getDaysRented() > 1) frequentRenterPoints++;

//show figures for this rental

result += "\t" + each.getMovie().getTitle() + "\t" +

String.valueOf(thisAmount) + "\n";

totalAmount += thisAmount;

}

//add footer lines

result += "Amount owed is " + String.valueOf(totalAmount) + "\n";

result += "You earned " + String.valueOf(frequentRenterPoints) +

" frequent renter points";

return result;

}

Page 41: Android Refactoring

9/25/17 Refactoring 41

public String statement() {

String result = "Rental Record for " + getName() + "\n";

for (Rental each : rentals) {

// show figures for this rental

result += "\t" + each.getMovie().getTitle() + "\t" + each.getCharge() + "\n";

}

// add footer lines

result += "Amount owed is " + getTotalCharge() + "\n";

result += "You earned " + getTotalFrequentRenterPoints() + " frequent renter points";

return result;

}

private double getTotalCharge() {

double totalCharge = 0;

for (Rental each : rentals) {

totalCharge += each.getCharge();

}

return totalCharge;

}

private int getTotalFrequentRenterPoints() {

int totalFrequentRenterPoints = 0;

for (Rental each : rentals) {

totalFrequentRenterPoints += each.getFrequentRenterPoints();

}

return totalFrequentRenterPoints;

}

Page 42: Android Refactoring

9/25/17 Refactoring 42

double getCharge(int numberOfDaysRented) {

double result = 2;

if (numberOfDaysRented > 2) {

result += (numberOfDaysRented - 2) * 1.5;

}

return result;

}

public void setPriceCode(int priceCode) {

switch (priceCode) {

case REGULAR:

price = new RegularPrice();

break;

case CHILDRENS:

price = new ChildrensPrice();

break;

case NEW_RELEASE:

price = new NewReleasePrice();

break;

default:

throw new IllegalArgumentException("illegal price code");

}

}

Page 43: Android Refactoring

Example 2 – Convert app to MVP

9/25/17 Refactoring 43

Page 44: Android Refactoring

Example 2 – Extract Delegate

9/25/17 Refactoring 44

Page 45: Android Refactoring

Example 2 – Create Interface

9/25/17 Refactoring 45

Page 46: Android Refactoring

Example 2 – Fix Activity

9/25/17 Refactoring 46

Page 47: Android Refactoring

Example 2 – Fix Presenter

9/25/17 Refactoring 47

Page 48: Android Refactoring

Example 3

9/25/17 Refactoring 48

Page 49: Android Refactoring

Example 4

9/25/17 Refactoring 49

Page 50: Android Refactoring

Lessons Learned

9/25/17 Refactoring 50

• Default Android design pattern is Ball of Mud

• Always create unit tests

• Use the metrics as a guide not as a mandate

• Measure early, measure often

• Think twice before you do a complete rewrite

Page 51: Android Refactoring

Resources

• https://dev.to/rly

• http://www.mikamantyla.eu/BadCodeSmellsTaxonomy.html

• https://sonarcloud.io

• https://plugins.jetbrains.com/plugin/93-metricsreloaded

• https://github.com/LiushuiXiaoxia/AndroidStudioRefactor

• https://github.com/mroderick/refactoring-day

• https://www.safaribooksonline.com/library/view/developing-high-quality/9781771375580/

• https://medium.com/google-developers/viewmodels-a-simple-example-ed5ac416317e

9/25/17 Refactoring 51

Page 52: Android Refactoring

Resources

9/25/17 Refactoring 52

Page 53: Android Refactoring

Contact Info

[email protected]

@godfreynolan


Recommended