Towards a Catalog of Java Dependency Injection Anti-Patterns

Towards a Catalog of Java Dependency Injection Anti-Patterns

Towards a Catalog of Java Dependency Injection Anti-Patterns Rodrigo Laigner Marcos Kalinowski Luiz Carvalho PUC-Rio, Brazil PUC-Rio, Brazil PUC-Rio, Brazil [email protected] [email protected] [email protected] Diogo Mendonça Alessandro Garcia CEFET/RJ, Brazil PUC-Rio, Brazil [email protected] [email protected] ABSTRACT ACM Reference Format: [Context] Dependency Injection (DI) is a commonly applied mech- Rodrigo Laigner, Marcos Kalinowski, Luiz Carvalho, Diogo Mendonça, and Alessandro Garcia. 2019. Towards a Catalog of Java Dependency Injec- anism to decouple classes from their dependencies in order to pro- tion Anti-Patterns. In SBES ’19: Brazilian Symposium on Software Engineering, vide better modularization of software. In the context of Java, the September 23–27, 2019, Salvador, BA. ACM, New York, NY, USA, 10 pages. availability of a DI specification and popular frameworks, such as https://doi.org/10.1145/1122445.1122456 Spring, facilitate DI usage in software projects. However, bad DI implementation practices can have negative consequences. Even though the literature suggests the existence of DI anti-patterns, 1 INTRODUCTION there is no detailed catalog of such bad practices. Moreover, there Dependency injection (DI) is a mechanism for improving software is no evidence on their occurrence and perceived usefulness from modularity. DI enables less coupling among modules by refraining the developer’s point of view. [Goal] Our goal is to review the them from being aware of implementation details of each other reported DI anti-patterns in order to analyze their completeness [15]. DI has become a common practice in the software industry, and to propose and evaluate a novel catalog of DI anti-patterns in as characterized by the existence of DI frameworks and industry- the context of Java. [Method] We propose an initial catalog con- oriented publications [12][16]. For instance, Spring1, one of the taining twelve Java DI anti-patterns. We selected four open source most popular Java frameworks, and Google AdWords2, a large- software projects that adopt a DI framework and developed a tool scale web application, have its components interconnected through to statically analyze the occurrence of the DI anti-patterns within DI. Furthermore, Java defines a specification targeted at DI and their source code. Also, we conducted a survey through face to Microsoft .NET Core provides native DI capabilities. face interviews with three experienced developers that regularly Despite the existence of well designed frameworks, such as apply DI. [Results] At least nine different DI anti-patterns appeared Spring and Guice3, the implementation of DI is not trivial and in each analyzed project. In addition, the feedback received from demands in-depth knowledge on object-oriented design. Hence, the developers confirmed their relevance and the importance of while DI might support providing a better modularization, once investing further effort towards a catalog. [Conclusion] The results system modules do not need to concern about dependence resolu- indicate that the initial catalog contains Java DI anti-patterns that tion, improper DI usage may hinder the effective achievement of occur in practice and are useful. Sharing it with practitioners may this goal. help them to avoid such anti-patterns. Sharing it with the research Although the technical literature [16][20] suggests the existence community will enable further improving the catalog. of DI anti-patterns, there is no evidence about their occurrence, acceptance and perceived usefulness from developers’ point of view. CCS CONCEPTS Furthermore, such anti-patterns can lead to negative consequences, such as high coupling and potential overuse of memory. Therefore, • Software and its engineering → Object oriented develop- it is necessary to properly document DI anti-patterns. Considering ment; Software design engineering. this scenario, our study aims at reviewing the completeness of the DI anti-patterns reported in the literature. We also propose a KEYWORDS catalog of DI anti-patterns in the context of Java, and investigate dependency injection, anti-pattern, java, pattern, framework their occurrence by mining open source repositories. Additionally, this study seeks to gather the perception of developers through an expert survey in order to validate the proposed catalog. The contributions of this paper are: (i) an initial effort towards Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed documenting a catalog of Java DI anti-patterns; (ii) an investigation for profit or commercial advantage and that copies bear this notice and the full citation on the occurrence of the proposed anti-patterns, indicating that they on the first page. Copyrights for components of this work owned by others than the author(s) must be honored. Abstracting with credit is permitted. To copy otherwise, or are general and occur within different projects; and (iii) gathering republish, to post on servers or to redistribute to lists, requires prior specific permission the perception of developers on the proposed catalog by an expert and/or a fee. Request permissions from [email protected]. SBES ’19, September 23–27, 2019, Salvador, BA © 2019 Copyright held by the owner/author(s). Publication rights licensed to ACM. 1https://spring.io ACM ISBN 978-1-4503-9999-9/18/06...$15.00 2https://github.com/google/guice/wiki/AppsThatUseGuice https://doi.org/10.1145/1122445.1122456 3https://github.com/google/guice SBES ’19, September 23–27, 2019, Salvador, BA R. Laigner, M. Kalinowski, L. Matheus, D. Mendonça, A. Garcia survey, indicating that the catalog is perceived as relevant and Table 1: Annotations defined in JSR-330 useful. The remainder of this paper is organized as follows. In Section 2 Annotation Description we provide the background on dependency injection and DI anti- A type annotated with @Singleton will be patterns and discuss related work. In Section 3, we describe our instantiated only once across the run-time goal and research questions. Next, the proposed catalog of DI anti- Singleton of the application patterns is introduced in Section 4. An evaluation of the occurrence of the cataloged DI anti-patterns is presented on Section 5. Section Scopes are rules applied to the life-cycle 6 presents the expert survey on the acceptance and perceived use- of a given object. Depending on the fulness of the catalog. Section 7 discusses the threats of validity. framework, the default implementation Scope Finally, Section 8 presents the concluding remarks. varies from retaining the instance for reuse or providing a new instance every 2 BACKGROUND AND RELATED WORK time a dependence is requested This section provides a brief background on the principles behind The value associated with a Qualifier DI and its main concepts. Also, previously reported DI anti-patterns Qualifier annotation is used to distinguish which are reviewed and related work is presented. concrete implementation should be provided Used to identify an object instance. It aims 2.1 Dependency Injection Named at assigning a specific name to a dependence The term Inversion of Control (IoC) was first introduced by Johnson and Foote [8] in the context of software frameworks. In order to Defines an injection point for which aDI container must provide an object instance. make use of functionalities provided by a framework, it is usually Inject needed to extend a set of classes. Meanwhile, this extension often It can be placed in constructors, methods, refrains the system from controlling its own execution, incurring in and fields an inversion of control. Later, the dependency inversion principle (DIP) was introduced by Martin [11]. The DIP states that modules should not depend on details of another module. Instead, abstrac- Table 2: DI anti-patterns extracted from [16] tions should be used in place of direct dependencies in order to enable reuse [11]. Based on Martin’s [11] description, Yang et al. Name Description [22] assert that in Java, DIP means that "a class should depend on Dependencies are controlled interface or abstract types, not on concrete types". These concepts Control Freak directly, as opposed to IoC form a basis for comprehending DI. Foreign defaults are used as According to Crasso et al. [4], DI is a programming mechanism Bastard Injection that "builds on the decoupling given by isolating components be- default values for dependencies hind interfaces, and focuses on delegating the responsibility for Constructors are assumed Constrained Construction component [or module] creation and binding to a DI container". to have a particular signature As noted by Yang et al. [22], DI is a specific structural form of DIP. Indeed, DI implements the DIP principle, once components are An implicit service can serve decoupled through an interface oriented design. Service Locator dependencies to consumers However, although we achieve better modularity through ab- but isn’t guaranteed to do so stractions, we still have to deal with instantiation of concrete classes, as noted by the Gamma et al. [7]. In the context of DI, Crasso et al. [4] assert that the responsibility for component creation is given to 2.2 DI Anti-Patterns a DI container. In particular, the DI container is usually employed According to Brown et al. [2], an anti-pattern is a "description in order to enable the IoC principle in DI. In the context of DI, IoC of a commonly occurring solution to a problem that generates is about delegating responsibilities that are outside of the scope of a decidedly negative consequences". In line with this definition, we given class to other classes of the system, the ones that can actually can understand a DI anti-pattern as a recurring DI usage pattern in be accountable for these given responsibilities, such as instance source code that degrades aspects that DI is supposed to improve, provision.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    10 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us