Restructuring programs by tucking statements into functions Arun Lakhotia and Jean-Christophe Deprez The Center for Advanced Computer Studies University of Southwestern Louisiana Lafayette, LA 70504 (318) 482-6766, -5791 (Fax) arun@cacs.usl.edu Abstract Changing the internal structure of a program without changing its behavior is called restructuring. This paper presents a transformation called tuck for restructuring programs by decomposing large functions into small functions. Tuck consists of three steps: Wedge, Split, and Fold. A wedge—a subset of statements in a slice—contains computations that are related and that may create a meaningful function. The statements in a wedge are split from the rest of the code and folded into a new function. A call to the new function is placed in the now restructured function. That tuck does not alter the behavior of the original function follows from the semantic preserving properties of a slice. Keywords: Program Restructuring; Program Slicing 1 Introduction Software restructuring is the transformation of software from one representation to another at the same relative abstraction level, without changing the external behavior of the subject system [CC90]. A software system may be restructured to make it easier to understand and change, and therefore less costly to maintain [Arn89]. Restructuring may also be the enabling step for reengineering a system [SJ87, Wat88], and for reverse engineering a system to extract its abstractions [BL91, HPLH90, War93]. Restructuring in the early days of structured programming implied removing the goto statements [AM71, Bak77, Kas74]. This notion of restructuring is quite mature and has led to several automated tools [Arn89]. Even though automatic removal of goto statements does not always produce programs that are desirable [Cal88], such restructuring is a necessary step for creating higher, logic-based abstractions from code [BL91, HPLH90, War93, Wat88]. This paper investigates the problem of restructuring programs by breaking its large code fragments and tucking them into new functions. The technical challenge in creating new functions lies in capturing computations that are meaningfully related. If that was not necessary, one could simply create functions by breaking off contiguous pieces of code of some pre-set size, such as done by Sneed and Jandrasics [SJ87]. Such a straightforward approach may not yield good functions because of the interleaving of unrelated computations in real-world code [RSW96]. We present a restructuring transformation tuck to decompose large, non-cohesive code fragments into small, cohesive functions [Dep97]. To tuck, according to the American Heritage Dictionary, is to “gather and fold.” This is precisely what our transformation does. Tuck is a composition of three primitive 1