An Empirical Study of Method Chaining in Java

An Empirical Study of Method Chaining in Java

An Empirical Study of Method Chaining in Java Tomoki Nakamaru Tomomasa Matsunaga Tetsuro Yamazaki [email protected] [email protected] [email protected] The University of Tokyo The University of Tokyo The University of Tokyo Soramichi Akiyama Shigeru Chiba [email protected] [email protected] The University of Tokyo The University of Tokyo ABSTRACT Method chaining is promoted as a good practice that improves While some promote method chaining as a good practice for im- the readability of source code. By method chaining, redundant proving code readability, others refer to it as a bad practice that temporary variables and code repetitions are eliminated [8]; related worsens code quality. In this paper, we first investigate whether method invocations are grouped into a single expression [34, 42]; method chaining is a programming style accepted by real-world an expression becomes easy to read from left to right as natural- programmers. To answer this question, we collected 2,814 Java language texts [13, 14, 18, 42]. repositories on GitHub and analyzed historical trends in the fre- However, at the same time, method chaining is often referred to quency of method chaining. The results of our analysis revealed as a bad practice. In the thread on StackOverflow [8], many posts the increasing use of method chaining; 23.1% of method invoca- claim that method chaining worsens the readability. For instance, a tions were part of method chains in 2018, whereas only 16.0% were post says: such invocations in 2010. We then explore language features that If you do everything in a single statement then that is are helpful to the method-chaining style but have not been sup- compact, but it is less readable (harder to follow) most ported yet in Java. For this aim, we conducted manual inspections of the times than doing it in multiple statements. of method chains that are randomly sampled from the collected Another post mentions that method chaining makes code confusing repositories. We also estimated how effective they are to encourage since it hides the type of an object that a programmer operates upon. the method-chaining style if they are adopted in Java. Negative opinions are not only about code readability. One post in the thread [8] states that method chaining is not reconcilable with CCS CONCEPTS most debuggers that offer line-level breakpoints: • ! Software and its engineering Software libraries and repos- You can’t put the breakpoint in a concise point so you ; Software design engineering. itories can pause the program exactly where you want it. If one of these methods throws an exception, and you KEYWORDS get a line number, you have no idea which method in Method chaining, Repository mining, Quantitative analysis the “chain” caused the problem. ACM Reference Format: Further, several posts in the thread [8] claim that method chaining Tomoki Nakamaru, Tomomasa Matsunaga, Tetsuro Yamazaki, Soramichi violates the law of Demeter [27], a guideline for developing loosely Akiyama, and Shigeru Chiba. 2020. An Empirical Study of Method Chaining coupled software. in Java. In 17th International Conference on Mining Software Repositories The controversy above leads us to our first question: Is method (MSR ’20), October 5–6, 2020, Seoul, Republic of Korea. ACM, New York, NY, USA, 10 pages. https://doi.org/10.1145/3379597.3387441 chaining accepted widely by real-world programmers? To answer this question, we collected Java repositories on GitHub and an- 1 INTRODUCTION alyzed historical trends in the frequency of method chaining. If method chaining is commonly considered as a bad practice, pro- Method chaining is a programming style in which multiple method grammers would avoid chaining method invocations. As a result, invocations are chained in a single expression as follows: the frequency would be the same or decrease over time. Conversely, new AlertDialog() if the frequency increases, that result will be supportive evidence . setTitle ("Warning") for the wide acceptance of method chaining in the real world. .setMessage("Are you sure?").show(); The answer to our first question is yes; the frequency of method chaining has increased considerably. Our analysis revealed that 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 23.1% of method invocations were part of method chains in 2018, for profit or commercial advantage and that copies bear this notice and the full citation whereas only 16.0% were such invocations in 2010. To help us better on the first page. Copyrights for components of this work owned by others than ACM understand the trends, this paper also presents our investigation must be honored. Abstracting with credit is permitted. To copy otherwise, or republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a on the bias in the frequency and the categories of method chains. fee. Request permissions from [email protected]. Our second question then arises in response to the first answer: MSR ’20, October 5–6, 2020, Seoul, Republic of Korea How can we extend the Java language to support this increasing © 2020 Association for Computing Machinery. ACM ISBN 978-1-4503-7517-7/20/05...$15.00 trend (or what API design should library developers adopt)? To https://doi.org/10.1145/3379597.3387441 answer this question, we manually analyzed method chains that MSR ’20, October 5–6, 2020, Seoul, Republic of Korea T. Nakamaru, T. Matsunaga, T. Yamazaki, S. Akiyama, and S. Chiba Dataset construction 1e3 1e5 Algorithm 1 2.0 Input: Set of repositories '4?>B8C>A84B 6 Output: Dataset 퐷0C0B4C 1.5 1: for each A4?>B8C>A ~ 2 '4?>B8C>A84B do 2: #0<4 Name of A4?>B8C>A ~ 4 3: '4E8B8>=B Year-end revisions of A4?>B8C>A ~ 1.0 4: for each A4E8B8>= 2 '4E8B8>=B do 0.5 2 5: ~40A Year of A4E8B8>= Number of files 6: for each .java file 5 8;4 in A4E8B8>= do Number of repos 7: 2>34 Content of 5 8;4 0.0 0 8: Add ¹2>34,=0<4, ~40Aº to 퐷0C0B4C 1998 2000 2002 2004 2006 2008 2010 2012 2014 2016 2018 1998 2000 2002 2004 2006 2008 2010 2012 2014 2016 2018 Year Year are randomly sampled from the collected repositories. We present (a) Number of repositories (b) Number of files language features (or API design) that we discovered are helpful for encouraging the method-chaining style in Java. Those features are 1e8 supported in other languages but have not been supported yet in 1.25 Java. We also estimated how effective they are if they are adopted 1.00 in Java. 0.75 The contribution of this paper is summarized as follows: 0.50 • We present, to the best of our knowledge, the first quantitative Number of lines 0.25 study on the use of method chaining that is based on a large set 0.00 of source code in the real world. • We empirically show the increasing use of method chaining 1998 2000 2002 2004 2006 2008 2010 2012 2014 2016 2018 Year in Java, which has been claimed without empirical evidence in preceding studies [20, 21, 26, 33, 34, 42, 43]. (c) Number of lines • We present language features (or API design) that support method chaining but are not supported yet in Java. We statistically esti- Figure 1: Number of repositories, files, and lines mated how effective each feature/design would be. The remainder of this paper is organized as follows: Section 2 de- 1 List<String> list = new ArrayList(); scribes the dataset and method that we use to answer our questions. 2 list .add( 3 createRandomString()// Length=1 Section 3 and 4 show the results of our analyses. Section 5 discusses 4 );// Length=1 the threats to validity of our results. Section 6 relates our work to 5 list.stream().map(s -> { preceding studies, and Section 7 concludes our paper. 6 return s.replace("foo","bar") 7 . replace ("baz","qux");// Length=2 8 }).forEach(s -> 2 MATERIALS AND METHODS 9 String output = String.format( 2.1 Dataset 10 "%d lines", 11 s. split ("\n").length// Length=1 To build our dataset, we collected 2,814 Java repositories on GitHub. 12 );// Length=1 Those repositories are the ones that were listed at least once in 13 System.out.println(output);// Length=1 the most-starred 1000 Java repositories on GitHub between Nov. 14 );// Length=3 10th, 2019 and Dec. 21st, 2019. We collected them by monitoring the response of the GitHub API1 every day during that period. Figure 2: Method chains and their lengths We built our dataset by extracting syntactically valid .java files from the year-end revisions of each repository in themost- starred repository set. The year-end revision of a year is the lat- which the file belongs, and ~40A is the year of the revision to which est revision made in that year. We marked a .java file as syn- the file belongs. Algorithm 1 shows pseudocode for constructing tactically valid when JavaParser2, a parser often used both in in- our dataset from a given set of repositories. A set of the most-starred dustry and academia, successfully parses the content of that file. repositories were used as the input to that algorithm. To find year-end revisions, we use the command git rev-list Our dataset contains over three million Java files (approximately <branch>, which lists all the revisions reachable from <branch>. seven hundred million lines) in total.

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