DEGREE PROJECT IN COMPUTER SCIENCE AND ENGINEERING, SECOND CYCLE, 30 CREDITS STOCKHOLM, SWEDEN 2017 Immutability: An Empirical Study in Scala LUDVIG AXELSSON KTH ROYAL INSTITUTE OF TECHNOLOGY SCHOOL OF COMPUTER SCIENCE AND COMMUNICATION Immutability: An Empirical Study in Scala LUDVIG AXELSSON Master in Computer Science Date: 2017 Supervisor: Philipp Haller Examiner: Mads Dam Swedish title: Oföränderlighet: en empirisk studie i Scala School of Computer Science and Communication i Abstract Utilizing immutability is considered to have many desired benefits when it comes to software develop- ment and reasoning about programs. It is also one of the core principles of functional programming, and many programming languages have support for specifying immutability. Developers can by spec- ifying immutability write code that, for example, prevent program state from being unintentionally mutated. The Scala programming language is a functional and object-oriented language where devel- opers can specify immutability with reassignable and non-reassignable variables. The type system in Scala has no built-in support for developers to express the fact that a type is immutable, immutability is instead by convention and considered best practice. However, knowledge about the immutability usage and how prevalent it is in real-world Scala code are until this point non-existent. This project presents an immutability analysis and evaluation of six small-to-large open source projects written in Scala providing empirical data on immutability usage. The analysis investigates the immutability property of templates, where a template refers to one of Scala’s different class types, on three distinct properties: shallow, conditionally deep and deep immutability, where deep is the strongest immutability property. The analysis works as a plug-in for the Scala compiler that statically analyzes the source code of projects. We report immutability statistics for each evaluated project, including three widely used projects, Scala’s standard library, Akka’s actor framework and ScalaTest. Expla- nations to why stronger immutability properties do not hold are also provided. The analysis show that the majority of templates for each project satisfied an immutability property and were not classified as mutable. Because each analyzed project had templates that were assumed to be mutable, as they were unreachable by our analysis, there could potentially be more templates that satisfy an immutability property. Inheritance is shown to be an important factor when it comes to a template’s immutability and mutability was found to be lower for the template types case class and sin- gleton object. This can be seen as intended by the designers of Scala, indicating that these type of class abstractions help programmers utilize immutability. Our results show that immutability is frequently used in Scala and the high degree of immutability usage could be due to the functional nature of the language. Keywords immutability, empirical study, static analysis ii Sammanfattning Att använda immuterbar (oföränderlig) data anses ha många önskvärda fördelar när det kommer till ut- veckling av program och att kunna resonera om dess funktionalitet. Immuterbar data är också en viktig princip inom funktionell programmering och många språk har idag stöd för att ange immuterbarhet. Utvecklare kan i kod ange ifall data ska vara immuterbar för att till exempel förhindra ett programtill- stånd från att oavsiktligt förändras. Programmeringsspråket Scala är ett funktionellt och objektoriente- rat språk där utvecklare kan ange immuterbarhet med två typer av variabler, en som är tilldelningsbar och en som är icke-tilldelningsbar. Typsystemet i Scala har inget inbyggt stöd för utvecklare att uttryc- ka det faktum att en typ är immuterbar, att använda immuterbarhet är i stället konvention och anses vara den bästa metoden. Men uppgifter om hur immuterbarhet egentligen används i riktiga Scala pro- jekt har fram tills nu inte varit tillgängligt. Detta projekt presenterar en immuterbarhetsanalys och en utvärdering av sex små till stora projekt med öppen källkod skrivna i programmeringsspråket Scala. Analysen undersöker immuterbarhetse- genskaper hos Scalas olika typer av klasser med avseende på tre olika egenskaper: ytlig, villkorligt djup och djup immuterbar, där djup är den starkaste immuterbarhetsegenskapen. Analysen fungerar som ett tillägg för Scalas kompilator och utfärdar en statisk analys av källkoden för ett projekt. Statistik om immuterbarhet för varje projekt redovisas och utvärderas, bland annat tre välkända och populära kod- baser, Scalas standard bibliotek, Akka’s actor ramverk och ScalaTest. Förklaringar till varför klasser inte uppfyller en immuterbarhetsegenskap visas också. Analysen visar att majoriteten av alla klasser i projekten har en immuterbarhetsegenskap och var inte klassificerade som muterbara. Eftersom varje projekt hade klasser som antogs vara muterbara för att dessa inte var nåbara för våran analys så kan det potentiellt finnas fler klasser som har en immuter- barhetsegenskap. Vad en klass ärver visar sig vara en viktig faktor när det kommer till vilken typ av immuterbarhetsegenskap den har. Muterbarhet visade sig vara lägre för klasser som är av typen case class and singleton object vilket kan anses vara avsett av Scalas skapare, då dessa klass abstraktioner hjälper programmerare att använda immuterbarhet. Resultaten visar att immuterbarhet används flitigt i Scala och den höga användningsgraden kan vara på grund av att det är ett funktionellt språk. Keywords immuterbarhet, empirisk studie, statisk analys Contents 1 Introduction 1 1.1 Motivation ........................................ 1 1.2 Problem Statement ................................... 3 1.3 Contribution ....................................... 3 1.4 Choice of Methodology ................................. 3 1.5 Delimitations ...................................... 4 1.6 Societal, Sustainability and Ethical Aspects ....................... 4 2 Background 5 2.1 The Scala Programming Language ........................... 5 2.1.1 Classes and Objects ............................... 6 2.1.2 Templates (class, object and trait) ........................ 6 2.1.3 Types ...................................... 9 2.2 Functional Programming ................................ 10 2.3 Immutability ....................................... 11 2.3.1 Why Immutability Matters ........................... 11 2.3.2 Varieties of Immutability ............................ 12 2.3.3 Immutability in Scala .............................. 13 2.4 Immutability Analysis .................................. 14 2.4.1 Static Program Analysis ............................. 14 2.4.2 Scala Compiler Plug-in ............................. 15 3 Related Work 17 3.1 Immutability Support in Programming Languages ................... 17 3.2 Immutability with Type Systems ............................ 18 3.3 Immutability Analysis .................................. 20 3.3.1 Static Code Analysis .............................. 20 3.4 An Empirical Study on C++ const and Immutability .................. 21 3.5 Tools for Immutability .................................. 22 3.6 Summary ........................................ 22 4 Methodology 23 4.1 The Immutability Analysis ............................... 23 4.1.1 Immutability Properties ............................. 24 4.1.2 Detecting Immutability ............................. 25 4.1.3 Limitations ................................... 27 4.1.4 Immutability Assumptions ........................... 28 iii iv CONTENTS 4.2 Evaluation ........................................ 28 5 Results 29 5.1 Projects Analyzed .................................... 29 5.2 Template Statistics .................................... 30 5.3 Immutability Statistics .................................. 32 5.4 Immutability Attributes ................................. 36 5.5 Conditional Deep Immutability ............................. 39 6 Discussion 41 6.1 The Empirical Study ................................... 41 6.2 Future Work ....................................... 42 6.3 Conclusion ........................................ 43 Glossary 44 Bibliography 45 A Source Code and Data 48 A.1 The Analysis Program .................................. 48 A.2 Immutability Assumptions ................................ 48 A.3 Immutability Attributes ................................. 50 Chapter 1 Introduction This chapter serves as an introduction to the degree project. We begin by describing the motivation and background of the project, followed by the problem statement and overall goal of it. The choice of methodology, delimitations, and contributions of the project are then also explained. 1.1 Motivation Unintended mutations of a program’s state are one reason for inconsistent behavior and bugs of the program. These mutations might have been introduced by side-effects of functions that developers were unaware of during the program’s implementation. Some programming languages do, for exam- ple, allow arguments of a function to be mutated. The fact that a third-party function can mutate the state of its argument can go undetected. The problem of rogue and complicated state mutations can become difficult to handle when states are shared among objects. One way to avoid undesired muta- tions is to use immutable data instead of mutable data. Immutable data cannot be mutated once created and instead of mutating shared data in memory, data would have to be re-created to include the modi- fications needed.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages60 Page
-
File Size-