Patent Publication Number: US-2004049767-A1

Title: Method and apparatus for comparing computer code listings

Description:
BACKGROUND OF THE INVENTION  
       [0001] 1. Technical Field  
       [0002] The present invention relates generally to an improved data processing system, and in particular, to a method and apparatus for processing data. Still more particularly, the present invention provides a method and apparatus for comparing listings of computer code.  
       [0003] 2. Description of Related Art  
       [0004] A program is a collection of instructions that tell the computer what to do. A program is called “software” and programs that users work with, such as word processors and spreadsheets, are called “applications” or “application programs”. A program is written in a programming language, such as Visual Basic, C or C++, and the statements and commands written by the programmer are converted into the computer&#39;s machine language by software called “assemblers”, “compilers”, and “interpreters”.  
       [0005] In developing programs or software, the programmer typically generates several versions of a program in the process of developing a final product. Often times in writing a new version of a program, a programmer may desire to locate differences between the versions. The programmer may compare the source files of the two different versions looking at the new code listing and the old code listing to identify differences in-lines of code between the two code listings. Typically, the programmer will place the old code listing on one side and the new code listing next to it. The programmer will compare lines of code by moving his or her hands to mark lines for comparison. The programmer will move down the lines in the two code listings until a match does not occur. When a mismatch between the lines in the code listings is identified, the programmer will move down the new listing while holding the place in the old code listing, looking for a match to the line in the old code listing. If a match is found, then the programmer assumes that additional lines have been added to the new code listing.  
       [0006] If no match occurs while moving down the new listing and while holding the old, then the programmer holds at the current line in the new code listing and moves down the old code listing looking for a matching line. If a match occurs, then it is assumed that lines have been deleted from the old listing to make the new listing. If no match occurs, then it is assumed that the line in the old code listing is replaced by the line in the new code listing. The programmer will move down to the next line of each code listing and repeat the comparison process.  
       [0007] Currently, this process results in an inaccuracy in correctly matching code listings. The fact that statements containing loops and/or if-then-else constructs in multiple nestings have the same end statements. As a result, the first occurrence of an assumed match may not be the right line. Therefore, it would be advantageous to have an improved method, apparatus, and computer instructions for accurately comparing code listings between different versions of a computer program.  
       SUMMARY OF THE INVENTION  
       [0008] The present invention provides a method, apparatus, and computer instructions for comparing programs. The present invention provides for allowing the specification or selection that several consecutive lines are required to match for the match to be a true match. A current line in a first code listing is compared to a current line in a second code listing. A next line in the second code listing is selected as the current line in the second code listing and the comparing step is repeated in response to an absence of a match between the current line in the first code listing to the current line in the second code listing. A determination is made as to whether a set of additional consecutive lines match between the first code listing and the second code listing in response to a match between the current line in the second code listing to the current line in the first code listing after a next line in the second code listing has been selected as the current line. The set of lines are identified as an addition to the second code listing if a match in the set of additional lines between the first code listing and the second code listing is present if the first code listing is an older code listing than the second code listing. The set of lines are identified as a deletion to the second code listing if a match in the set of additional lines between the first code listing and the second code listing is present if the second code listing is an older code listing than the first code listing.  
     
    
    
     BRIEF DESCRIPTION OF THE DRAWINGS  
     [0009] The novel features believed characteristic of the invention are set forth in the appended claims. The invention itself, however, as well as a preferred mode of use, further objectives and advantages thereof, will best be understood by reference to the following detailed description of an illustrative embodiment when read in conjunction with the accompanying drawings, wherein:  
     [0010]FIG. 1 is a pictorial representation of a data processing system in which the present invention may be implemented in accordance with a preferred embodiment of the present invention;  
     [0011]FIG. 2 is a block diagram of a data processing system is shown in which the present invention may be implemented; and  
     [0012]FIGS. 3A and 3B depict the flow of the Matching Process (MP); and  
     [0013]FIG. 4 is a diagram illustrating sample code on which the process of the present invention may be used in accordance with a preferred embodiment of the present invention.  
    
    
     DETAILED DESCRIPTION OF THE PREFERRED EMBODIMENT  
     [0014] With reference now to the figures and in particular with reference to FIG. 1, a pictorial representation of a data processing system in which the present invention may be implemented is depicted in accordance with a preferred embodiment of the present invention. A computer  100  is depicted which includes system unit  102 , video display terminal  104 , keyboard  106 , storage devices  108 , which may include floppy drives and other types of permanent and removable storage media, and mouse  110 . Additional input devices may be included with personal computer  100 , such as, for example, a joystick, touchpad, touch screen, trackball, microphone, and the like. Computer  100  can be implemented using any suitable computer, such as an IBM eServer computer or IntelliStation computer, which are products of International Business Machines Corporation, located in Armonk, N.Y. Although the depicted representation shows a computer, other embodiments of the present invention may be implemented in other types of data processing systems, such as a network computer. Computer  100  also preferably includes a graphical user interface (GUI) that may be implemented by means of systems software residing in computer readable media in operation within computer  100 .  
     [0015] With reference now to FIG. 2, a block diagram of a data processing system is shown in which the present invention may be implemented. Data processing system  200  is an example of a computer, such as computer  100  in FIG. 1, in which code or instructions implementing the processes of the present invention may be located. Data processing system  200  employs a peripheral component interconnect (PCI) local bus architecture. Although the depicted example employs a PCI bus, other bus architectures such as Accelerated Graphics Port (AGP) and Industry Standard Architecture (ISA) may be used. Processor  202  and main memory  204  are connected to PCI local bus  206  through PCI bridge  208 . PCI bridge  208  also may include an integrated memory controller and cache memory for processor  202 . Additional connections to PCI local bus  206  may be made through direct component interconnection or through add-in boards. In the depicted example, local area network (LAN) adapter  210 , small computer system interface SCSI host bus adapter  212 , and expansion bus interface  214  are connected to PCI local bus  206  by direct component connection. In contrast, audio adapter  216 , graphics adapter  218 , and audio/video adapter  219  are connected to PCI local bus  206  by add-in boards inserted into expansion slots. Expansion bus interface  214  provides a connection for a keyboard and mouse adapter  220 , modem  222 , and additional memory  224 . SCSI host bus adapter  212  provides a connection for hard disk drive  226 , tape drive  228 , and CD-ROM drive  230 . Typical PCI local bus implementations will support three or four PCI expansion slots or add-in connectors.  
     [0016] An operating system runs on processor  202  and is used to coordinate and provide control of various components within data processing system  200  in FIG. 2. The operating system may be a commercially available operating system such as Windows XP, which is available from Microsoft Corporation. An object oriented programming system such as Java may run in conjunction with the operating system and provides calls to the operating system from Java programs or applications executing on data processing system  200 . “Java” is a trademark of Sun Microsystems, Inc. Instructions for the operating system, the object-oriented programming system, and applications or programs are located on storage devices, such as hard disk drive  226 , and may be loaded into main memory  204  for execution by processor  202 .  
     [0017] Those of ordinary skill in the art will appreciate that the hardware in FIG. 2 may vary depending on the implementation. Other internal hardware or peripheral devices, such as flash read-only memory (ROM), equivalent nonvolatile memory, or optical disk drives and the like, may be used in addition to or in place of the hardware depicted in FIG. 2. Also, the processes of the present invention may be applied to a multiprocessor data processing system.  
     [0018] For example, data processing system  200 , if optionally configured as a network computer, may not include SCSI host bus adapter  212 , hard disk drive  226 , tape drive  228 , and CD-ROM  230 . In that case, the computer, to be properly called a client computer, includes some type of network communication interface, such as LAN adapter  210 , modem  222 , or the like. As another example, data processing system  200  may be a stand-alone system configured to be bootable without relying on some type of network communication interface, whether or not data processing system  200  comprises some type of network communication interface. As a further example, data processing system  200  may be a personal digital assistant (PDA), which is configured with ROM and/or flash ROM to provide non-volatile memory for storing operating system files and/or user-generated data.  
     [0019] The depicted example in FIG. 2 and above-described examples are not meant to imply architectural limitations. For example, data processing system  200  also may be a notebook computer or hand held computer in addition to taking the form of a PDA. Data processing system  200  also may be a kiosk or a Web appliance.  
     [0020] The processes of the present invention are performed by processor  202  using computer implemented instructions, which may be located in a memory such as, for example, main memory  204 , memory  224 , or in one or more peripheral devices  226 - 230 .  
     [0021] The present invention provides an improved method, apparatus, and computer instructions for comparing instructions or lines of code in two versions of a computer program. The mechanism of the present invention provides a more accurate matching or comparison process by allowing the designation that not just one but several consecutive lines must match for the match to be true. The number of consecutive lines selected may vary with the coding standards and the programming language. The use of requiring a match in some selected number of consecutive statements helps guarantee an accurate match and allows the mechanism of the present invention to recognize the multiple end statements and make adjustments.  
     [0022]FIGS. 3A and 3B depict the flow of the Matching Process (MP). The Matching Process starts the process. A number of lines to substantiate a match after a mismatch is stored for use in determining whether a match meets the criteria (step  300 ). This number may be preselected or input by a user. The number of lines selected depends on the particular implementation. The number of lines may vary depending on the coding standards and the particular programming language. For example, the number of lines may be one, two, three, or four. For code where after the end of a loop or some other block of code there is always some repetitive set of one or two statements at the end, a need exists for a matching requirement of three or four lines of statements to be back at a true match. In today&#39;s languages for HTML, Java, and VB, a need exists even more for multiple matches because of the beginning and closing statements for a block. The process of comparison then begins with selecting the first line of the old code listing and new code listing (step  302 ). The lines in each listing are compared ( 304 ). A determination is then made as to whether a match is present between the first line in the old code listing and the first line in the new code listing (step  306 ). If there is a match, a check is made to see if more unprocessed or unchecked lines are present in each code listing (step  308 ). If more unchecked or unprocessed lines are present, the process selects or moves to the next line in both code listings (step  310 ) with the process returning to step  306  as described above. These steps of matching and moving to the next line are repeated as long as the lines match.  
     [0023] With reference again to step  306 , if the lines do not match, the process stays at or holds on the line in the old code listing and moves to the next line in the new code listing (step  312 ). These two lines are the lines currently selected for comparison. A test is performed to determine whether a match is present between the currently selected lines (step  314 ). If a match is present, a determination is made as to whether the selected number of lines stored in step  300  match between the two code listings after the occurrence of the mismatch (step  316 ).  
     [0024] If the selected number of lines match between the two code listings, then the lines found in the new code listing are identified as additions to the new code listing (step  318 ). Thereafter, the process returns to step  308  to continue the search for matches between the code listings as described in steps  308  and  310 .  
     [0025] With reference again to step  316 , if the selected number of lines for a match has not been reached, then the process moves to or selects the next line in each code listing for processing (step  320 ) with the process then returning to step  312  to test for a match again. Steps  314 ,  316 , and  320  are repeated until the selected set has matched or a mismatch occurs. In step  322 , if more lines are present in the new code listing, the process returns to step  312  with these steps being repeated until a match is confirmed or the number of unprocessed lines in the new code listing becomes exhausted.  
     [0026] When the number of unprocessed lines in the new codes listing is identified as being exhausted in step  322 , the process then returns to the original place in the code listings where the mismatch occurred (step  324 ). After returning to the place in the code listings in which the mismatch occurred, the process moves to or selects the next line in the old code listing, while holding the line in the new code listing (step  326 ).  
     [0027] Next, a determination is made as to whether a match between the currently selected lines in the code listings is present (step  328 ). If a match occurs, then a determination is made as to whether the number of lines stored in step  300  match between the two code listings after the mismatch (step  330 ). If the number of lines match, then these lines are assumed to be deletions from the old code listing (step  332 ) with the process then returning to steps  308  and  310  as described above to continue the search.  
     [0028] If the selected number of lines for a match has not been reached, the process then selects or moves to the next line in each listing (step  334 ) with the process then returning to step  328  to test for a match between the currently selected lines in each of the code listings. Steps  328 ,  330 , and  334  are repeated until the number of lines selected for matching has matched or a mismatch occurs. When a mismatch occurs in step  328 , a determination is then made as to whether additional unprocessed lines are present in the old code listing (step  336 ). If additional unprocessed lines are present in the old code listing, the process returns to step  326  with these steps being repeated until a match is confirmed or the number of unprocessed lines in the old code listing is exhausted.  
     [0029] When the number of unprocessed lines in the old code listing is exhausted, the process returns to the original place in the code listings where the mismatch occurred and the line in the old code line is identified as having been changed with respect to the line in the new code listing (step  338 ). The process then proceeds to check for additional lines in each code listing and to move to the next line in each code listing (step  310 ) with the process then returning to step  306  as described above.  
     [0030] If additional unprocessed lines are not present in both of the code listings in step  308 , a determination is made as to whether the new code listing has more unprocessed lines (step  340 ). If additional unprocessed lines are present in the code listing, these lines are identified as additions (step  342 ) with the process terminating thereafter. If in step  340 , the new code listing does not have additional unprocessed lines, a determination is made as to whether the old code listing contains unprocessed lines (step  344 ). If additional unprocessed lines are present in the old code listing, these lines are identified as deletions made to the new code listing (step  346 ) with the process terminating thereafter. Turning back to step  344 , if additional unprocessed lines are not present in the old code listing, the process terminates.  
     [0031] Turning next to FIG. 4, a diagram illustrating sample code on which the process of the present invention may be used is depicted in accordance with a preferred embodiment of the present invention. Specifically, old code  400  and new code  402  are examples of code that may be processed using the steps illustrated in FIGS. 3A and 3B above. If the comparison between old code  400  and new code  402  were to be performed using presently available matching systems, only one line, then a mismatch would occur at lines  404  and  406  between old code  400  and new code  402 . This mismatch would show that the match when holding old code  400  at line  404  and line  408  in new code  402 . In this case, lines  406  and  410  in new code  402  are additions.  
     [0032] On the other hand, if the matching process used two lines, then line  404  and line  412  in old code  400  would match lines  414  and  416  in new code  402 . In this case, the new additions found between the mismatch and the matches would be correct. If a single line process were used, the right lines would show all as added but as two groups of lines not as one when two lines are used in performing the matching process.  
     [0033] Thus, the present invention provides an improved method, apparatus, and computer instructions for comparing code listings. Specifically, the mechanism of the present invention allows for the comparing of multiple lines of code in a code listing when determining whether the code from a first code listing matches the code in a second code listing to determine lines that have been deleted or inserted. The first code listing is referred to as the old code listing and the second code listing is referred to as the new code listing. The process performs a series of comparisons instead of a single comparison when looking for a match. The result is a more accurate set of comparison results than with a single comparison.  
     [0034] It is important to note that while the present invention has been described in the context of a fully functioning data processing system, those of ordinary skill in the art will appreciate that the processes of the present invention are capable of being distributed in the form of a computer readable medium of instructions and a variety of forms and that the present invention applies equally regardless of the particular type of signal bearing media actually used to carry out the distribution. Examples of computer readable media include recordable-type media, such as a floppy disk, a hard disk drive, a RAM, CD-ROMs, DVD-ROMs, and transmission-type media, such as digital and analog communications links, wired or wireless communications links using transmission forms, such as, for example, radio frequency and light wave transmissions. The computer readable media may take the form of coded formats that are decoded for actual use in a particular data processing system.  
     [0035] The description of the present invention has been presented for purposes of illustration and description, and is not intended to be exhaustive or limited to the invention in the form disclosed. Many modifications and variations will be apparent to those of ordinary skill in the art. The embodiment was chosen and described in order to best explain the principles of the invention, the practical application, and to enable others of ordinary skill in the art to understand the invention for various embodiments with various modifications as are suited to the particular use contemplated.