Automated refactoring to the Null Object design pattern

We eliminate null-checking conditionals via refactoring to the Null Object pattern.We introduce an algorithm for automated discovery of refactoring opportunities.We specify refactoring preconditions and the source code transformation procedure.Quality assessment and execution efficiency results support our method. ContextNull-checking conditionals are a straightforward solution against null dereferences. However, their frequent repetition is considered a sign of poor program design, since they introduce source code duplication and complexity that impacts code comprehension and maintenance. The Null Object design pattern enables the replacement of null-checking conditionals with polymorphic method invocations that are bound, at runtime, to either a real object or a Null Object. ObjectiveThis work proposes a novel method for automated refactoring to Null Object that eliminates null-checking conditionals associated with optional class fields, i.e., fields that are not initialized in all class instantiations and, thus, their usage needs to be guarded in order to avoid null dereferences. MethodWe introduce an algorithm for automated discovery of refactoring opportunities to Null Object. Moreover, we specify the source code transformation procedure and an extensive set of refactoring preconditions for safely refactoring an optional field and its associated null-checking conditionals to the Null Object design pattern. The method is implemented as an Eclipse plug-in and is evaluated on a set of open source Java projects. ResultsSeveral refactoring candidates are discovered in the projects used in the evaluation and their refactoring lead to improvement of the cyclomatic complexity of the affected classes. The successful execution of the projects' test suites, on their refactored versions, provides empirical evidence on the soundness of the proposed source code transformation. Runtime performance results highlight the potential for applying our method to a wide range of project sizes. ConclusionOur method automates the elimination of null-checking conditionals through refactoring to the Null Object design pattern. It contributes to improvement of the cyclomatic complexity of classes with optional fields. The runtime processing overhead of applying our method is limited and allows its integration to the programmer's routine code analysis activities.

[1]  William Pugh,et al.  Null dereference analysis in practice , 2010, PASTE '10.

[2]  Jeannette M. Wing,et al.  Behavioral Subtyping Using Invariants and Constraints , 2001, FME 2001.

[3]  Mel Ó Cinnéide,et al.  A methodology for the automated introduction of design patterns , 1999, Proceedings IEEE International Conference on Software Maintenance - 1999 (ICSM'99). 'Software Maintenance for Business Change' (Cat. No.99CB36360).

[4]  Alexander Chatzigeorgiou,et al.  Identification of extract method refactoring opportunities for the decomposition of methods , 2011, J. Syst. Softw..

[5]  Mauricio A. Saca Refactoring improving the design of existing code , 2017, 2017 IEEE 37th Central America and Panama Convention (CONCAPAN XXXVII).

[6]  Anas N. Al-Rabadi,et al.  A comparison of modified reconstructability analysis and Ashenhurst‐Curtis decomposition of Boolean functions , 2004 .

[7]  Don Batory,et al.  Automated Software Evolution via Design Pattern Transformations , 1995 .

[8]  David Hovemeyer,et al.  Finding more null pointer bugs, but not too many , 2007, PASTE '07.

[9]  Alexander Chatzigeorgiou,et al.  Identification of Move Method Refactoring Opportunities , 2009, IEEE Transactions on Software Engineering.

[10]  Frank Buschmann,et al.  Explicit Interface and Object Manager , 2003, EuroPLoP.

[11]  Vladimir Cretu,et al.  A logic based approach to locate composite refactoring opportunities in object-oriented code , 2010, 2010 IEEE International Conference on Automation, Quality and Testing, Robotics (AQTR).

[12]  Shinji Kusumoto,et al.  Identifying, Tailoring, and Suggesting Form Template Method Refactoring Opportunities with Program Dependence Graph , 2012, 2012 16th European Conference on Software Maintenance and Reengineering.

[13]  Joshua Kerievsky,et al.  Refactoring to Patterns , 2004, XP/Agile Universe.

[14]  Doo-Hwan Bae,et al.  An automated refactoring approach to design pattern-based program transformations in Java programs , 2002, Ninth Asia-Pacific Software Engineering Conference, 2002..

[15]  Joe D. Warren,et al.  The program dependence graph and its use in optimization , 1987, TOPL.

[16]  Jean-Claude Royer,et al.  Transformations between Composite and Visitor Implementations in Java , 2013, 2013 39th Euromicro Conference on Software Engineering and Advanced Applications.

[17]  Paris Avgeriou,et al.  Proceedings of the 16th European Conference on Pattern Languages of Programs , 2012 .

[18]  K. Rustan M. Leino,et al.  Declaring and checking non-null types in an object-oriented language , 2003, OOPSLA.

[19]  Emmanouel A. Giakoumakis,et al.  Automated refactoring to the Strategy design pattern , 2012, Inf. Softw. Technol..

[20]  Béat Hirsbrunner,et al.  Toward an Implementation of the "Form Template Method" Refactoring , 2007, Seventh IEEE International Working Conference on Source Code Analysis and Manipulation (SCAM 2007).

[21]  Alexander Chatzigeorgiou,et al.  Identification of refactoring opportunities introducing polymorphism , 2010, J. Syst. Softw..

[22]  James O. Coplien,et al.  Pattern languages of program design , 1995 .

[23]  Tom Mens,et al.  A survey of software refactoring , 2004, IEEE Transactions on Software Engineering.

[24]  Bobby Woolf,et al.  Null object , 1997 .

[25]  Matthew Naylor,et al.  Finding Inputs that Reach a Target Expression , 2007 .

[26]  Ravichandhran Madhavan,et al.  Null dereference verification via over-approximated weakest pre-conditions analysis , 2011, OOPSLA '11.

[27]  D. Janaki Ram,et al.  JIAD: a tool to infer design patterns in refactoring , 2004, PPDP '04.

[28]  Béat Hirsbrunner,et al.  Toward an Implementation of the "Form Template Method" Refactoring , 2007 .