Zurück zur Übersicht

Seuss: Cleaning up Class Responsibilities with Language-based Dependency Injection

Publikationsart Peer-reviewed
Publikationsform Tagungsbeitrag (peer-reviewed)
Publikationsjahr 2011
Autor/in Schwarz Niko, Lungu Mircea, Nierstrasz Oscar
Projekt Synchronizing Models and Code
Alle Daten anzeigen

Tagungsbeitrag (peer-reviewed)

Titel der Proceedings TOOLS Europe 2011
Status Publiziert
DOI 10.1007/978-3-642-21952-8_20

Open Access


Unit testing is often made more difficult by the heavy use of classes as namespaces and the proliferation of class methods to encapsulate configuration code. We have analyzed the use of 120 class methods from 96 projects by categorizing them according to their responsibilities. We find that most class methods support a hodgepodge of mixed responsibilities, held together only by their common need to be globally visible. Tight coupling between instances and their classes breaks encapsulation, and, together with the global visibility of class methods, complicates testing. By making dependency injection a feature of the programming language, we can get rid of class methods altogether. We employ the following semantic changes: (1) Replace every occurrence of a global with an access to an instance variable; (2) Let that instance variable be automatically injected into the object when it is instantiated. We present Seuss, a prototype that implements this change of semantics in Smalltalk. We show how Seuss eliminates the need to use class methods for non-reflective purposes, reduces the need for many design patterns, such as Abstract Factory, and simplifies configuration code, particularly for unit tests.