An Analytical Method for Refactoring Object Oriented Code
We present a new method based on software analysis for refactoring object-oriented programs. The segment to be refactored is decomposed by a parser into structural elements and described by ﬁve sets of relations organized in sparse matrix format. Rows correspond to tuples, columns to variables in the segment, column partitions to classes, and row partitions to methods. Row and column permutations and rearrangement of partitions are refactorings. A heuristic search algorithm is proposed, which follows prescribed rules or menu selections. The output is a design for the given segment.
Our work draws heavily from mathematical notation by establishing a parallel between the “refactoring” of equations and computer code. The method divides the refactoring problem into parts suitable for separate analysis, makes critical refactoring information readily available, and helps to appreciate the nature of refactoring with a visual presentation. Examples illustrate the algorithm. The ﬁrst two examples demonstrate how rules are applied, class and method deﬁnition, variable classiﬁcation into attributes and arguments, temporaries or return values for methods, how diﬀerent search paths yield diﬀerent refactorings, and hierarchical model nesting. The Fowler refactoring Move Method is illustrated. The third example starts from a poor UML design and produces two diﬀerent good designs. It illustrates how designs can be refactored, the use of relational operations to refactor, and the conversion between conditional logic and polymorphism. The technology can automate refactoring, or be combined with development and enhance coherence and integration in program evolution. Refactoring is a technology that improves the quality of existing code by applying a series of elementary behavior-preserving transformations called refactorings. Research in refactoring oﬃcially started in 1992 with the publication of a PhD thesis  by William Opdyke. More recently, Martin Fowler published a catalog of refactorings in book form , listing more than 70 refactorings. An extensive survey of software refactoring was published by Tom Mens et al. . Much of the work loosely clusters along two main directions, methods based on graph representations of the code and intended for automation, and methods that support manual operations such as ﬁnding segments of code that need improvement, or deciding which refactorings to apply. In the ﬁrst group, we should mention the detection of “bad smells” [2, 4, 5, 6], applying design patterns to automate refactoring in Java, although the authors admit to possible errors , feature decomposition of programs, where a feature is an increment in program functionality , detection of duplicated code [9, 10, 11], detection of program invariants , and using dynamic data obtained during runtime . The use of code metrics is commanding attention, including using distance cohesion metrics to associate methods and variables of a class , using a combination of code metrics and visualization , and using object-oriented metrics for bad smell detection . Eﬀorts in the second group include [16, 17, 18, 19]. Graph transformation and rewriting was formalized and used to analyze dependencies in refactoring [20, 21]. M. Verbaere et al.  note that existing tools are language-speciﬁc and contain signiﬁcant bugs, even in sophisticated development environments, and propose a language for refactoring that manipulates a graph representation of the program. There is also a third, incipient direction. T. Mens et al.  suggest that refactorings can be classiﬁed according to the code quality attributes they aﬀect, such as robustness, extensibility, reusability, or performance, and then used to enhance the attributes of interest. They propose to estimate the eﬀect by considering the internal quality attributes aﬀected, such as code size, complexity, coupling and cohesion, which can be measured if access to program structure information is available. Code metrics can be used to determine the eﬀect of refactorings on maintainability , or quality . Performance can be improved by replacing conditional logic with polymorphism . There has been work on specialized refactoring systems such as architectural refactoring of inherited software, particularly corporate software  and clustering techniques for architecture recovery . And in aspect-oriented programming, using program slicing to improve method and aspect extraction , role-based refactoring , role mapping , and aspect refactoring . Refactoring of non-object-oriented code has also been considered. . Code refactoring has been linked to algebraic factoring , and, in the particular case of user interface design, to matrix algebra . The need to teach refactoring in Computer Science curricula has now been recognized . In spite of all the eﬀorts, refactoring remains a complex and risky procedure. We propose a method for refactoring object-oriented software where the segment of code of interest is separated into elements. The method is behavior-preserving and static. It is based on code analysis , intended for automation, language-independent, applicable to objet-oriented and non-object-oriented code, and inspired on mathematical notation. Mathematical notation is mature and precise. For centuries, mathematicians have been writing equations, condensing properties and behavior into objects, “refactoring” their equations to make them more meaningful and easier to manipulate, using patterns, and perfecting a notation that would allow them to express all that. For example, a symmetric positive deﬁnite matrix A is a