How Implementation Language Affects Design Patterns
Total Page:16
File Type:pdf, Size:1020Kb
How Implementation Language Affects Design Patterns A Comparison of Gang of Four Design Pattern Implementations in Different Languages Kristian Pedersen Thesis submitted for the degree of Master in Programming and Networks 30 credits Department of Informatics Faculty of mathematics and natural sciences UNIVERSITY OF OSLO Spring 2019 How Implementation Language Affects Design Patterns A Comparison of Gang of Four Design Pattern Implementations in Different Languages Kristian Pedersen © 2019 Kristian Pedersen How Implementation Language Affects Design Patterns http://www.duo.uio.no/ Printed: Reprosentralen, University of Oslo Abstract In the present work the impact of implementation language on code quality for implementations based on the Gang of Four design patterns is examined. These are used by many developers and system architects to improve their development process. The patterns Composite, Prototype, Adapter and Decorator are exam- ined in the context of the programming languages Python, JavaScript, C#, Go and Smalltalk. A case was designed and implemented in these lan- guages for each pattern and then analyzed based on a gauntlet of metrics. It was found that there were differences between the languages. Having mechanics solving problems similar to the problem the design pattern is solving was positive. In addition it was positive for a language to have flexible typing schemes, little overhead and attribute visibility control. No language was found to be clearly best suited, but individual strengths and weaknesses are discussed. i ii Contents I Introduction 1 1 Introduction 3 1.1 Motivation . .3 1.2 Scope and Problem Statement . .4 1.3 Goal . .4 1.4 Approach . .4 1.5 Evaluation . .5 1.6 Work Done . .5 1.7 Results . .6 1.8 Conclusion . .6 1.9 Thesis Outline . .7 II Theory 9 2 Background 11 2.1 Introduction . 11 2.2 Design Patterns Implementation . 11 2.2.1 Introduction . 11 2.2.2 Implementation Description . 11 2.2.3 Implementation Studies . 12 2.2.4 Implementation and Language . 12 2.3 Individual Papers . 13 2.3.1 Introduction . 13 2.3.2 GoHotDraw . 13 2.3.3 Empirical Study of Github . 13 2.3.4 On the Issue of Language Support . 14 III Method 15 3 Methodology 17 3.1 Introduction . 17 3.2 Setting . 17 3.3 Outline . 18 3.4 Metrics . 19 3.4.1 Introduction . 19 3.4.2 Metric Selection . 19 iii 3.4.3 Example . 20 3.4.4 Lines of code . 21 3.4.5 Cyclomatic complexity . 23 3.4.6 Object Oriented Measures . 24 3.4.7 Shallow References . 26 3.4.8 NCLOC aggregation . 27 3.4.9 Code as Data . 28 3.5 Validity . 29 3.5.1 Introduction . 29 3.5.2 Definition . 29 3.5.3 Need For Validity . 29 3.5.4 Design and Implementation Philosophy . 29 3.5.5 Well defined requirements . 30 3.5.6 Model Design . 30 3.5.7 Visibility Relaxation . 31 3.5.8 Order of implementation . 31 3.5.9 Agile selection of patterns . 31 3.5.10 Pseudocode . 32 3.5.11 Adapting Metrics to Go . 32 3.5.12 Further threats to validity . 33 4 Cases 35 4.1 Introduction . 35 4.2 Composite . 35 4.2.1 Pattern . 35 4.2.2 The case . 35 4.2.3 Usage example . 37 4.2.4 Choices . 37 4.2.5 Results . 38 4.2.6 Analysis . 38 4.2.7 Summary . 40 4.3 Prototype . 40 4.3.1 Pattern . 40 4.3.2 Case . 41 4.3.3 Choices . 41 4.3.4 Usage example . 42 4.3.5 Results . 42 4.3.6 Analysis . 43 4.4 Adapter . 45 4.4.1 Pattern . 45 4.4.2 Case . 45 4.4.3 Choices . 46 4.4.4 Usage example . 47 4.4.5 Results . 47 4.4.6 Analysis . 47 4.5 Decorator . 51 4.5.1 Pattern . 51 4.5.2 Case . 51 iv 4.5.3 Usage Example . 52 4.5.4 Modeling choices . 52 4.5.5 Results . 53 4.5.6 Analysis . 53 IV Discussion and Conclusion 57 5 Analysis 59 5.1 Introduction . 59 5.2 Aggregates . 59 5.2.1 Introduction . 59 5.2.2 Proportion of Case’s NCLOC . 60 5.2.3 Proportion of Language’s NCLOC . 60 5.2.4 Relative Size . 60 5.2.5 Relative Verbosity of Language . 61 5.2.6 Summary . 61 5.3 Cross case comparison . 61 5.3.1 Introduction . 61 5.3.2 NCLOC . 61 5.3.3 Cyclomatic complexity . 62 5.3.4 Tree Related Metrics . 62 5.3.5 Shallow References . 63 5.3.6 Summary . 63 5.4 Summary . 63 6 Discussion 65 6.1 Introduction . 65 6.2 Core Issues . 65 6.2.1 Introduction . 65 6.2.2 The Issues . 65 6.2.3 Relevance . 66 6.2.4 Validity . 67 6.2.5 Implications . 67 6.3 Languages . 68 6.3.1 Introduction . 68 6.3.2 Python . 68 6.3.3 JavaScript . 69 6.3.4 C# . 70 6.3.5 Go . 72 6.3.6 Smalltalk . 73 6.3.7 Comparison . 75 6.3.8 Generalization . 76 6.4 Metric Validity . 78 6.4.1 Introduction . 78 6.4.2 Lines of Code . 79 6.4.3 Cyclomatic Complexity . 79 6.4.4 Tree Metrics . 80 v 6.4.5 Loose Class Cohesion . 80 6.4.6 Shallow References . 81 6.4.7 Metric Coverage . 81 6.5 Comparing to Literature . 82 6.5.1 Introduction . 82 6.5.2 GoHotDraw . 82 6.5.3 Empirical Study of Github . 82 6.5.4 On the Issue of Language Support . 83 6.5.5 Conclusion . ..