Complexity Resolution Control for Context Based on Metromaps
Take Control, Control Code Complexity
Transcript of Take Control, Control Code Complexity
![Page 1: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/1.jpg)
Take Control –Control Code Complexity
Fan Yang1 Oct 2016
![Page 2: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/2.jpg)
2
Sponsors
![Page 4: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/4.jpg)
4
![Page 5: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/5.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/6.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/7.jpg)
7
![Page 8: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/8.jpg)
8
Resharper Extensions
![Page 9: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/9.jpg)
9
![Page 10: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/10.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/11.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/12.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/13.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/14.jpg)
14
![Page 15: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/15.jpg)
15
![Page 16: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/16.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/17.jpg)
17
![Page 18: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/18.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/19.jpg)
19
1st Business Case
![Page 20: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/20.jpg)
20
0
2,500
1,250
0
0
3,750
![Page 21: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/21.jpg)
21
![Page 22: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/22.jpg)
22
![Page 23: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/23.jpg)
23
![Page 24: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/24.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/25.jpg)
25
![Page 26: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/26.jpg)
26
![Page 27: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/27.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/28.jpg)
28
Reduce Cyclomatic Complexity…
![Page 29: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/29.jpg)
29
Generalizing Patterns for Arguments
![Page 30: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/30.jpg)
30
Common in Calculation, Differ in Parameter
3%
5%
8%
13%
15%
![Page 31: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/31.jpg)
31
The Parameter
![Page 32: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/32.jpg)
32
The Process
![Page 33: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/33.jpg)
33
The Consumer
![Page 34: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/34.jpg)
34
When Types AppearProperty
Residential Property
Non-Residential Property
First Home Second Home Leasehold Freehold
![Page 35: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/35.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/36.jpg)
36
Base Class
![Page 37: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/37.jpg)
37
Subclasses
![Page 38: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/38.jpg)
38
![Page 39: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/39.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/40.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/41.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/42.jpg)
42
![Page 43: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/43.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/44.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/45.jpg)
45
1980s -1980 - Smalltalk – 1st OOP Language 1983 - C++
1991 - Java
2001 - C#
![Page 46: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/46.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/47.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/48.jpg)
48
Interface and Implementations (I)
![Page 49: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/49.jpg)
49
Interface and Implementations (II)
![Page 50: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/50.jpg)
50
Interface and Implementations (III)
![Page 51: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/51.jpg)
51
Class - Property
Property
![Page 52: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/52.jpg)
52
Class - Quote
Quote
![Page 53: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/53.jpg)
53
Class - Calculator
Calculator
![Page 54: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/54.jpg)
54
Service Factory
![Page 55: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/55.jpg)
55
Test
![Page 56: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/56.jpg)
56
VS Code Metrics Result
![Page 57: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/57.jpg)
57
Reduce Cyclomatic Complexity?
![Page 58: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/58.jpg)
58
Generalizing Patterns for Process
![Page 59: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/59.jpg)
59
Higher-order function
![Page 60: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/60.jpg)
60
![Page 61: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/61.jpg)
61
Supporting Function1. Send Email2. Get Properties3. Calculate Quote
![Page 62: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/62.jpg)
62
Function Factory
![Page 63: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/63.jpg)
63
Code Metrics Result
![Page 64: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/64.jpg)
64
![Page 65: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/65.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/66.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/67.jpg)
67
![Page 68: Take Control, Control Code Complexity](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/68.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/69.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022081513/58a497311a28ab741b8b5acd/html5/thumbnails/70.jpg)
70
Thank you!
https://uk.linkedin.com/in/fanyang2010
https://yangfan2010.wordpress.com/