1. Trang chủ
  2. » Công Nghệ Thông Tin

Living with The Legacy Code: A very Short Guide

80 368 1
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Living with The Legacy Code: A very Short Guide
Trường học Plusnet Ibuildings
Định dạng
Số trang 80
Dung lượng 3,07 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Definition of Legacy Code: Code without tests. Code youve “inherited” Code noone understands. Technical debt Aim to understand the concepts and motivations. Get it into version control. Identify the inflection point. Create integration acceptance tests. Set up your continuous integration environment

Trang 1

Legacy Code

Trang 2

@rowan_m

Trang 3

Ibuildings Plusnet

Trang 4

What is

“Legacy Code”?

Trang 5

What is

“Legacy Code”?

Trang 6

Who has never created Legacy Code?

Trang 7

My own

story

little

Trang 9

Starting a project

Trang 10

Aim to understand

the concepts and motivations

Trang 11

Try using the application

Trang 12

Find and then read

any / all documentation

Trang 14

Talk to the original developers

Trang 15

Talk to the users

… especially the

“different” ones

Trang 16

the code

Trang 17

Catalogue the live platform

& environment

Trang 18

Recreate it!

Trang 19

Deploy the code

Trang 21

Time to enter

The Code

Trang 22

Reading Static analysis

Dynamic analysis

Time to enter

The Code

Trang 23

ph pd

oc

phpdoc -ti 'Sweet Application' \ -pp -o HTML:Smarty:PHP \ -d Libraries \

-t Docs

http://www.phpdoc.org/

Title

Style Code in here

Docs out here!

Trang 24

Beware of type-hiding!

Type-hinting

Type-hiding

/**

* @param array $opts Current options

* @return array Options with flag set

*/

function setFlag( array $ opts ) {

$ opts ['flag'] = true ;

return $ opts ;

}

/**

* @param int $fullPence Full price in pence

* @return float Discounted price in pence

*/

function applyDiscount( $ fullPence ) {

return ( $ fullPence * 0.8 ) ;

}

Trang 25

do xy

ge n

http://www.stack.nl/~dimitri/doxygen/

doxygen -s -g ~/doxy.conf vim ~/doxy.conf

# edit at least this OUPUT_DIRECTORY

# play with the rest

cd ~/dev/project doxygen ~/doxy.conf Docs out here Code in here

Trang 27

bo um

l http://bouml.free.fr/

Trang 28

bo um

l http://bouml.free.fr/

Trang 29

bo um

l http://bouml.free.fr/

Trang 30

bo um

l http://bouml.free.fr/

Trang 31

bo um

l http://bouml.free.fr/

Trang 32

bo um

l http://bouml.free.fr/

Trang 33

bo um

l

Trang 34

17 | WARNING | Line exceeds 80 characters; contains 87 characters

32 | WARNING | Line exceeds 80 characters; contains 87 characters

36 | ERROR | Opening class brace must be on a line by itself

36 | ERROR | Closing brace must be on a line by itself

-http://matrix.squiz.net/developer/tools/php_cs

Trang 35

Cont inuo

us Inte grat

ion

http://www.phpundercontrol.org/

http://hudson-ci.org/

Trang 36

n time!

Trang 37

t, te st

Trang 38

Ignore it, code anyway

Trang 39

Ignore it, code anyway

Please don't.

Trang 40

Ignore it, code anyway

Please don't.

however

Trang 41

Deadlines, clients,

money, etc.

Trang 42

Deadlines, clients,

money, etc.

Secure a follow-up project

Make everyone

aware of the risks

Trang 43

Why do you need the code?

Trang 44

Why do you need the code?

Library dependency Adding new behaviour Changing behaviour

simple

complex

Trang 45

Isolate legacy dependencies

Trang 46

Isolate legacy dependencies

Create an anti-corruption layer

Trang 47

Complete isolation

Create a legacy service

Trang 48

Partial isolation

Wrapper classes

or methods

Trang 49

function extract_brain( & $ person ) {

$ brain = $ person [' brain '];

unset( $ person [' brain ']);

return $ brain ; }

/**

* @param array $person

* @return bool living or not

*/

function create_life( $ person ) {

require_once( LIB_DIR ' /nuts_n_bolts.inc ');

:(

Wrapper class

Trang 50

public function extractBrain(Person $ p ) {

// format to legacy style

$ pLgcy = $this -> toArray( $ p ) ;

// run legacy code

$ bLgy = extract_brain( $ pLgcy ) ;

// format to new style

$ p = $this -> toPerson( $ pLgcy ) ;

$ b = $this ->t oBrain( $ bLgcy ) ;

return array ( $ p , $ b ) ; }

public function createLife(Person $ p )

{

// validate

if ( $ person-> isAlive()) throw new LivingException() ;

// format to legacy style

$ pLgcy = $this -> toArray( $ p ) ;

// run legacy code

$ pLgy = create_life( $ pLgcy ) ;

// format to new style

return $this -> toPerson( $ pLgcy ) ; }

}

Some code you can use

:) Wrapper class

Trang 51

Changing the code

Trang 52

Changing the code

Take an incremental approach

Commit to 1 day

at a time

Trang 53

Difficult to estimate Hidden dependencies Unknown behaviour

Trang 54

1 Get it into

version control

Trang 55

2 Identify the inflection point

Trang 56

3 Create integration

& acceptance tests

Trang 57

4 Set up your continuous integration

environment

Trang 58

5 Rewrite and refactor!

Trang 59

of changes

Trang 60

Mixed Procedural →

Trang 61

includes HTML PHP

PHP

HTML HTML PHP

HTML HTML

HTML PHP

PHP PHP

HTML

PHP PHP HTML

HTML

Trang 62

includes HTML PHP

PHP

HTML HTML PHP

HTML HTML

HTML PHP

PHP PHP

HTML echo

if if

HTML

PHP PHP HTML

Trang 63

Procedural OO →

Trang 65

static method static method static method

static method

static method includes

Trang 66

static method static method static method

static method

static method

includes

method method method

method

constructor includes

Trang 67

Sprout method / class

Trang 68

public function createInvoice(Account $ acc , array $ charges )

The existing code

“We just need to be able to give

each client their own personal

discount on certain charges.”

Trang 69

public function createInvoice(Account $ acc , array $ charges )

$ accDisc = new AccountDiscounter( $ acc ) ;

$ discountedCharge = $ accDisc-> calculate( $ chg ) ;

return $ discountedCharge ;

}

The new code

Trang 70

public function createInvoice(Account $ acc , array $ charges )

{

$ invoice = new Invoice() ;

foreach ( $ charges as $ chg ) {

// Sprout new behaviour!

$ chg = $ this-> calcDiscount( $ acc , $ chg ) ;

$ invoice-> addLine( $ chg-> getDesc() , $ chg-> getAmount()) ; }

return $ invoice ;

}

private function calcDiscount(Account $ acc , Charge $ chg )

{

$ accDisc = new AccountDiscounter( $ acc ) ;

$ discountedCharge = $ accDisc-> calculate( $ chg ) ;

return $ discountedCharge ;

}

Call it

Trang 71

Untestable OO testable OO →

Trang 72

Dependency Inversion / Extraction

Trang 73

The Problem

public function calcDiscount(Account $ acc , Charge $ chg )

{

$ accDisc = new AccountDiscounter( $ acc ) ;

$ discountedCharge = $ accDisc-> calculate( $ chg ) ;

return $ discountedCharge ;

Trang 74

The Problem

public function calcDiscount(Account $ acc , Charge $ chg )

{

$ accDisc = new AccountDiscounter( $ acc ) ;

$ discountedCharge = $ accDisc-> calculate( $ chg ) ;

// contact the database

// call a web service

}

}

Trang 75

Quick Solution

public function calcDiscount(Account $ acc , Charge $ chg )

{

$ accDisc = $ this-> getAccountDiscounter( $ acc ) ;

$ discountedCharge = $ accDisc-> calculate( $ chg ) ;

Trang 76

Dependency Injection Solution

public function calcDiscount(Account $ acc , Charge $ chg )

{

$ accDisc = $ this-> discounter ;

$ discountedCharge = $ accDisc-> calculate( $ chg ) ;

Trang 77

(v2) Dependency Injection Solution →

public function calcDiscount(Account $ acc , Charge $ chg )

{

$ accDisc = $ this-> discounter ;

$ discountedCharge = $ accDisc-> calculate( $ chg ) ;

Trang 79

Any questions?

Feedback to: http://joind.in/1242

@rowan_m

Ngày đăng: 10/07/2014, 16:03

TỪ KHÓA LIÊN QUAN