Immutability: An Empirical Study in Scala

University essay from KTH/Skolan för datavetenskap och kommunikation (CSC)

Abstract: Utilizing immutability is considered to have many desired benefits when it comes to software development 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 specifying 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 developers 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. Explanations 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 singleton 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.

  AT THIS PAGE YOU CAN DOWNLOAD THE WHOLE ESSAY. (follow the link to the next page)