Take Control, Control Code Complexity

Post on 16-Feb-2017

177 views 5 download

Transcript of Take Control, Control Code Complexity

Take Control –Control Code Complexity

Fan Yang1 Oct 2016

2

Sponsors

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

4

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

6

Code Complexity MetricsClean Code

Object-Oriented Programming

Polymorphism

Functional Programming

Control Flow

Higher-Order FunctionRefactoring

Software Engineering Papers

Cyclomatic Complexity

7

8

Resharper Extensions

9

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

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

12

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

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

13

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

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

14

15

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/

17

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.

19

1st Business Case

20

0

2,500

1,250

0

0

3,750

21

22

23

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%

25

26

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)

28

Reduce Cyclomatic Complexity…

29

Generalizing Patterns for Arguments

30

Common in Calculation, Differ in Parameter

3%

5%

8%

13%

15%

31

The Parameter

32

The Process

33

The Consumer

34

When Types AppearProperty

Residential Property

Non-Residential Property

First Home Second Home Leasehold Freehold

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.

36

Base Class

37

Subclasses

38

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

The Cost of Building SoftwareThe Overall Cost of the System

$250K$2 M

1960s

?50 K

1980s

40

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)

42

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.”

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)

45

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

1991 - Java

2001 - C#

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

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

48

Interface and Implementations (I)

49

Interface and Implementations (II)

50

Interface and Implementations (III)

51

Class - Property

Property

52

Class - Quote

Quote

53

Class - Calculator

Calculator

54

Service Factory

55

Test

56

VS Code Metrics Result

57

Reduce Cyclomatic Complexity?

58

Generalizing Patterns for Process

59

Higher-order function

60

61

Supporting Function1. Send Email2. Get Properties3. Calculate Quote

62

Function Factory

63

Code Metrics Result

64

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.

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

67

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)

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

70

Thank you!

fan.yang@live.co.uk

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

https://yangfan2010.wordpress.com/