take control, control code complexity

70
Take Control – Control Code Complexity Fan Yang 1 Oct 2016

Upload: fan-yang

Post on 16-Feb-2017

178 views

Category:

Software


5 download

TRANSCRIPT

Page 1: Take Control, Control Code Complexity

Take Control –Control Code Complexity

Fan Yang1 Oct 2016

Page 2: Take Control, Control Code Complexity

2

Sponsors

Page 3: Take Control, Control Code Complexity

3http://www.dddnorth.co.uk/Sessions/Details/293

Page 4: Take Control, Control Code Complexity

4

Page 5: Take Control, Control Code Complexity

5

You• How long have you been coding?

A . Over 10 yearsB. 5 – 10 yearsC. 1 – 5 yearsD. Less than 1 year

• About Cyclomatic Complexity ?A. Never heard of itB. Heard of it, never use itC. Use it occasionallyD. Use it all the time

Page 6: Take Control, Control Code Complexity

6

Code Complexity MetricsClean Code

Object-Oriented Programming

Polymorphism

Functional Programming

Control Flow

Higher-Order FunctionRefactoring

Software Engineering Papers

Cyclomatic Complexity

Page 7: Take Control, Control Code Complexity

7

Page 8: Take Control, Control Code Complexity

8

Resharper Extensions

Page 9: Take Control, Control Code Complexity

9

Page 10: Take Control, Control Code Complexity

10

Cyclomatic complexity is a software metric (measurement), used to indicate the complexity of a program.

The Cyclomatic Complexity of a section of source code is the number of linearly independent paths within it.

Mathematical Representation

V(G) = E - N + 2E - Number of edgesN - Number of Nodes

V (G) = P + 1Where P = Number of predicate nodes (node that contains condition)

V (G) = 9 – 7 + 2 = 4

V (G) = 3 + 1 = 4

Page 11: Take Control, Control Code Complexity

V(G)= E – N + 2 = 4- 4 + 2= 2

V(G) = E – N + 2= 3 – 3 + 2 = 2

V(G) = E – N + 2= 3 – 3 + 2= 2

V(G) = E – N + 2= 8 – 6 + 2= 4

V(G) = E – N + 2= 4 – 4 + 2= 2

Cyclomatic Complexity Measures

11

Page 12: Take Control, Control Code Complexity

12

V(G)= E – N + 2 = 3 - 3 + 2= 2

V(G)= E – N + 2 = 9 - 8 + 2= 3

Page 13: Take Control, Control Code Complexity

13

V(G)= E – N + 2 = 13 - 10 + 2= 5 V(G)

= E – N + 2 = 31 - 23 + 2= 10

Page 14: Take Control, Control Code Complexity

14

Page 15: Take Control, Control Code Complexity

15

Page 16: Take Control, Control Code Complexity

16

Credit limit (which you want to stay far away from reaching) and not a Speed limit (which you tend to target as the norm).  

Cyclomatic Complexity Limit

http://www.wrightfully.com/thoughts-on-cyclomatic-complexity/

Page 17: Take Control, Control Code Complexity

17

Page 18: Take Control, Control Code Complexity

18

1945-1976 • 1945 – Alan Turing wrote code in binary • 1954 – 1st major programming language - Fortran• 1960s – COBOL, LISP, • 1970s – Pascal & C & BASIC

They are Procedural/Structural Languages.

The paper uses code in Fortran.

Page 19: Take Control, Control Code Complexity

19

1st Business Case

Page 20: Take Control, Control Code Complexity

20

0

2,500

1,250

0

0

3,750

Page 21: Take Control, Control Code Complexity

21

Page 22: Take Control, Control Code Complexity

22

Page 23: Take Control, Control Code Complexity

23

Page 24: Take Control, Control Code Complexity

24

0

2,500

1,250

0

0

3,750

3,750

6,250

2,000

0

0

12,000

3%

5%

8%

13%

15%

3%

5%

8%

13%

15%

Page 25: Take Control, Control Code Complexity

25

Page 26: Take Control, Control Code Complexity

26

Page 27: Take Control, Control Code Complexity

27

VS Code Metrics Result

Maintainability Index = MAX(0,(171 – 5.2 * log(Halstead Volume) – 0.23 * (Cyclomatic Complexity) – 16.2 * log(Lines of Code))*100 / 171)

Page 28: Take Control, Control Code Complexity

28

Reduce Cyclomatic Complexity…

Page 29: Take Control, Control Code Complexity

29

Generalizing Patterns for Arguments

Page 30: Take Control, Control Code Complexity

30

Common in Calculation, Differ in Parameter

3%

5%

8%

13%

15%

Page 31: Take Control, Control Code Complexity

31

The Parameter

Page 32: Take Control, Control Code Complexity

32

The Process

Page 33: Take Control, Control Code Complexity

33

The Consumer

Page 34: Take Control, Control Code Complexity

34

When Types AppearProperty

Residential Property

Non-Residential Property

First Home Second Home Leasehold Freehold

Page 35: Take Control, Control Code Complexity

35

Clean Code (2009), Robert C. Martin Chapter 17: Smells and Heuristics, page 299

G23: Prefer Polymorphism to If/Else or Switch/Case

This might seem a strange suggestion given the topic of Chapter 6. After all, in that chapter I make the point that switch statements are probably appropriate in the parts of the system where adding new functions is more likely than adding new types.

First, most people use switch statements because it’s the obvious brute force solution,not because it’s the right solution for the situation. So this heuristic is here to remind us to consider polymorphism before using a switch.

Second, the cases where functions are more volatile than types are relatively rare. Soevery switch statement should be suspect.

I use the following “ONE SWITCH” rule: There may be no more than one switch statement for a given type of selection. The cases in that switch statement must create polymorphic objects that take the place of other such switch statements in the rest of the system.

Page 36: Take Control, Control Code Complexity

36

Base Class

Page 37: Take Control, Control Code Complexity

37

Subclasses

Page 38: Take Control, Control Code Complexity

38

Page 39: Take Control, Control Code Complexity

39

Quiz• 1950s and 1960s, study shows that users would not use off-the-shelf

packages for Payroll, inventory control, accountants receivable etc.• Why?• The requirements were too specialized, the case-to-case variation too

high.• 1980s, Packages of Payroll, Accountants were in high demand and

widespread use.• Why?• Package are all generalized?• No• The needs are simpler?• No

Page 40: Take Control, Control Code Complexity

The Cost of Building SoftwareThe Overall Cost of the System

$250K$2 M

1960s

?50 K

1980s

40

Page 41: Take Control, Control Code Complexity

41

We hear desperate cries for a silver bullet – something to make software costs drop as rapidly as computer hardware costs do….

(Fred Brooks, 1986)

Page 42: Take Control, Control Code Complexity

42

Page 43: Take Control, Control Code Complexity

43

No Silver Bullet: Essence and Accident in Software Engineering

Fred Brooks, 1986Physics also deals with terribly complex objects, but in physics there is always the belief that there are unifying principles to be found.

A software engineer can harbour no such belief: “Much of the complexity that he must master is arbitrary complexity, forced without rhyme or reason by the many human institutions and systems to which his interfaces must conform.”

Page 44: Take Control, Control Code Complexity

44

Not only are there no silver bullets now in view, the very nature of software makes it unlikely that there will be any.

(Fred Brooks, 1986)

Page 45: Take Control, Control Code Complexity

45

1980s -1980 - Smalltalk – 1st OOP Language 1983 - C++

1991 - Java

2001 - C#

Page 46: Take Control, Control Code Complexity

46

Is a SOLID Object-Oriented solution complex?

•S – Single responsibility principle•O – Open-closed principle•L – Liskov substitution principle•I – Interface segregation principle•D – Dependency Inversion Principle

Page 47: Take Control, Control Code Complexity

47

2nd Business Case•Create a property conveyancing quote report•A list of properties obtained from a data source•Based on the different property information, provide different quote• Different property type: - residential & non-residential• Different output

• Send the output via email

Page 48: Take Control, Control Code Complexity

48

Interface and Implementations (I)

Page 49: Take Control, Control Code Complexity

49

Interface and Implementations (II)

Page 50: Take Control, Control Code Complexity

50

Interface and Implementations (III)

Page 51: Take Control, Control Code Complexity

51

Class - Property

Property

Page 52: Take Control, Control Code Complexity

52

Class - Quote

Quote

Page 53: Take Control, Control Code Complexity

53

Class - Calculator

Calculator

Page 54: Take Control, Control Code Complexity

54

Service Factory

Page 55: Take Control, Control Code Complexity

55

Test

Page 56: Take Control, Control Code Complexity

56

VS Code Metrics Result

Page 57: Take Control, Control Code Complexity

57

Reduce Cyclomatic Complexity?

Page 58: Take Control, Control Code Complexity

58

Generalizing Patterns for Process

Page 59: Take Control, Control Code Complexity

59

Higher-order function

Page 60: Take Control, Control Code Complexity

60

Page 61: Take Control, Control Code Complexity

61

Supporting Function1. Send Email2. Get Properties3. Calculate Quote

Page 62: Take Control, Control Code Complexity

62

Function Factory

Page 63: Take Control, Control Code Complexity

63

Code Metrics Result

Page 64: Take Control, Control Code Complexity

64

Page 65: Take Control, Control Code Complexity

65

It is now generally accepted that modular design is the key to successful programming… However, there is a very important point that is often missed.

When writing a modular program to solve a problem, one first divides the problem into sub-problems, then solves the sub-problems, and finally combines the solutions. 

The ways in which one can divide up the original problem depend directly on the ways in which one can glue solutions together.

Therefore, to increase one’s ability to modularize a problem conceptually, one must provide new kinds of glue in the programming language.

Page 66: Take Control, Control Code Complexity

66

“We shall argue in the remainder of this paper that functional languages provide two new, very important kinds of glue…

This is the key to functional programming’s power – it allows improved modularization.

It is also the goal for which functional programmers must strive – smaller and simpler and more general modules, glued together with the new glues we shall describe.”

And the two new glues are- Higher-order function- Lazy Evaluation

Page 67: Take Control, Control Code Complexity

67

Page 68: Take Control, Control Code Complexity

68

Summary• 2 Examples• Stamp Duty Example • Conveyancing Report Example

• 2 Types of Higher-order function• Generalizing the parameter• Generalizing the process

• 3 Software Engineering Papers• A Complexity Measure (1976)• No Silver Bullet (1986)• Why Functional Programming Matters (1990)

Page 69: Take Control, Control Code Complexity

69

References• Pluralsight.com

• Functional Programming with C# (David Fancher)• Tactical Design Patterns in .NET: Control Flow (Zoran Horvat)

• Papers• A Complexity Measure• No Silver Bullet• Why Functional Programming Matters

• Books• Refactoring (Martin Fowler, 1999)• Clean Code (Robert C. Martin, 2009)

• Web• The Morning Paper (Adrian Colyer)• http://www.martinfowler.com/• https://en.wikipedia.org/wiki/Cyclomatic_complexity

• Code• https://github.com/misssoft

Page 70: Take Control, Control Code Complexity

70

Thank you!

[email protected]

https://uk.linkedin.com/in/fanyang2010

https://yangfan2010.wordpress.com/