Reverse Engineering of Software

When you join a project, the code is often the only accurate description of the system. The documentation has drifted, the original designers have moved on, and you still have to fix a bug or add a feature without breaking anything. Reverse engineering is the disciplined way to read a system back into a design you can reason about. You will use it whenever you maintain a legacy system, evaluate a component before reusing it, or check that an implementation still matches its intended design.

What reverse engineering produces

Reverse engineering is the process of analysing an existing system to identify its components and their relationships, and to create representations of it at a higher level of abstraction. It does not change the system: the output is knowledge, such as a class diagram, a call graph or a description of a protocol, not new code.


A diagram with requirements, design and implementation boxes, a blue forward engineering arrow pointing toward implementation above them and an orange reverse engineering arrow pointing back toward requirements below them.


Forward engineering moves from requirements to design to code, adding detail at each step. Reverse engineering moves the other way, discarding detail to expose structure. Several related activities are often confused with it:


Activity

Abstraction level

Changes the code?

Typical output

Redocumentation

Same level

No

Call graphs, cross-reference listings

Design recovery

Higher level

No

Class and sequence diagrams with rationale

Restructuring

Same level

Yes, behaviour preserved

Refactored code

Re-engineering

Up, then back down

Yes

A rebuilt system on a new design or platform


Design recovery is the most demanding form of reverse engineering: it combines facts extracted from code with domain knowledge, surviving documents and conversations with developers to explain what the design is and why it took that shape. Re-engineering usually begins with reverse engineering and then forward-engineers a new version.



Recovering a class diagram from code

The most common reverse-engineering task in a design course is turning source code into a UML class diagram. Tools can extract the raw facts, but you must interpret them. Each code construct suggests a model element:


Code construct

Recovered model element

A class or interface declaration

A class or interface

extends or implements

Generalization or interface realization

A field whose type is another project class

Association, navigable from the field's owner

A single reference field

Upper bound of one at the far end

An array or collection field

Upper bound of many at the far end

Two classes holding references to each other

One bidirectional association, not two

A type used only as a parameter or local variable

Dependency, not association

Primitive or library types such as String

Attributes, not separate classes


Lower bounds are the hard part. A reference field allows zero or one object unless something guarantees it is set, so look for a constructor that rejects null and the absence of any setter before you write a multiplicity of exactly 1. An array or list allows zero or more elements; a lower bound above zero needs a check somewhere in the code.



Measuring what you recovered

Once you have a model, simple counts show where to look first. The fan-in of a class is the number of other project classes that refer to it; its fan-out is the number of project classes it refers to. Every link from one class to another adds one to the source's fan-out and one to the target's fan-in, which gives a built-in check on your counting:


∑all classesfan-in=∑all classesfan-out=number of class-to-class links\sum_{\text{all classes}} \text{fan-in} = \sum_{\text{all classes}} \text{fan-out} = \text{number of class-to-class links}


A class with high fan-in is used by many others, so changing it is risky and documenting it first pays off. A class with high fan-out depends on many others and is harder to understand or test in isolation.

What to remember

  • Reverse engineering raises the level of abstraction and leaves the system unchanged; restructuring and re-engineering change code.
  • Design recovery adds domain knowledge and rationale to the facts extracted from code.
  • Fields of project types become associations; parameter-only uses become dependencies; paired references form one bidirectional association.
  • A single reference gives an upper bound of one; a lower bound of one needs evidence such as a constructor check.
  • Total fan-in always equals total fan-out, and high fan-in marks the classes to understand first.

Example: Recovering the design of a banking module

You have inherited a small banking module with no surviving documentation. Before changing how interest is posted, you need a class diagram and a sense of which class is riskiest to touch. The complete source is below; String, LocalDate and double are library or primitive types.


Java
public abstract class Account {
    private String number;
    private Customer owner;
    private Transaction[] history;

    public void post(Transaction t) { /* appends t to history */ }
}

public class SavingsAccount extends Account {
    private double rate;

    public void addInterest(InterestPolicy policy) {
        post(policy.interestFor(this));
    }
}

public class ChequingAccount extends Account {
    private OverdraftPlan plan;
}

public class Customer {
    private String name;
    private Account[] accounts;
}

public class Transaction {
    private double amount;
    private LocalDate date;
}

public class OverdraftPlan {
    private double limit;
}

public interface InterestPolicy {
    Transaction interestFor(Account account);
}


For part c, use these counting rules. The fan-out of a type is the number of distinct other project types named in its own source, including its superclass. Its fan-in is the number of other project types whose source names it. Library and primitive types do not count.


a) List the project types and the generalizations, and say which fields become plain attributes.


Scan the listing for type declarations and set library types aside. The module declares six classes, Account, SavingsAccount, ChequingAccount, Customer, Transaction and OverdraftPlan, plus one interface, InterestPolicy, for seven project types. Account is declared abstract, so on the diagram its name is shown in italics.


Two classes use the extends keyword: SavingsAccount and ChequingAccount both extend Account. That gives two generalizations sharing one superclass. SavingsAccount inherits the post operation, which is why it can call post without naming any new type.


Fields of library or primitive types become attributes rather than separate classes: number in Account, rate in SavingsAccount, name in Customer, amount and date in Transaction, and limit in OverdraftPlan.


Result: seven project types and two generalizations, both into the abstract class Account.


b) Identify every association and its multiplicities, and say which remaining relationships are only dependencies.


Each field whose type is a project class suggests an association. There are four such fields: Account.owner, Account.history, ChequingAccount.plan and Customer.accounts.

  • Account.owner and Customer.accounts describe the same relationship from both sides, so together they form ________ bidirectional association between Customer and Account. The array Customer.accounts gives the Account end 0..∗0..*. The single reference Account.owner gives the Customer end 0..10..1; nothing in the listing guarantees that an owner is set, so record 0..10..1 and confirm with the developers whether every account must have an owner before writing 11.
  • Account.history is an array, so the Transaction end is 0..∗0..*. Transaction holds no reference back, so the association is navigable only from Account, and the code does not settle how many accounts can share one transaction.
  • ChequingAccount.plan is a single reference, giving the OverdraftPlan end 0..10..1.

InterestPolicy appears in SavingsAccount only as a method parameter, so that link is a ________ rather than an association. InterestPolicy itself names Transaction and Account only in a method signature, which adds two more dependencies.


Result: the four fields yield three associations, Customer to Account, Account to Transaction and ChequingAccount to OverdraftPlan, plus three dependencies involving InterestPolicy.


c) Compute the fan-out and fan-in of every project type, check that the totals agree, and decide which class to document first.


Fan-out comes from reading each type's own source:

  • Account names Customer and Transaction, so its fan-out is 2.
  • SavingsAccount names Account and InterestPolicy, so its fan-out is ________.
  • ChequingAccount names Account and OverdraftPlan, so its fan-out is 2.
  • Customer names only Account, so its fan-out is 1.
  • Transaction and OverdraftPlan name no project type, so each has fan-out 0.
  • InterestPolicy names Transaction and Account, so its fan-out is 2.

The fan-out values add to ________ links:


∑fan-out=2+2+2+1+0+0+2=9\sum \text{fan-out} = 2 + 2 + 2 + 1 + 0 + 0 + 2 = 9


Fan-in reads the same links from the other end:

  • Account is named by SavingsAccount, ChequingAccount, Customer and InterestPolicy, so its fan-in is 4.
  • Transaction is named by Account and InterestPolicy, so its fan-in is ________.
  • Customer, OverdraftPlan and InterestPolicy are each named by ________ type.
  • SavingsAccount and ChequingAccount are named by no other type, so each has fan-in 0.


∑fan-in=4+2+1+1+1+0+0=9\sum \text{fan-in} = 4 + 2 + 1 + 1 + 1 + 0 + 0 = 9


The totals agree, as they must, because each link adds one to a fan-out and one to a fan-in. Account has the highest fan-in, so a change to it can ripple into four other types.


Result: document Account first; it has fan-in 4, the largest in the module, and the interest change passes through it via post.

A maintenance team uses several related activities on an old inventory system. Match each activity with the description that fits it.

Match each term
A.

Analysing a system and then rebuilding it in a new form, such as a new language or platform

B.

Transforming code at the same abstraction level to improve its internal organisation while preserving external behaviour

C.

Analysing an existing system to create representations of it at a higher level of abstraction without changing it

D.

Producing new or corrected views at the same abstraction level, such as call graphs and cross-reference listings

E.

Moving from requirements and design toward a concrete implementation

F.

Combining code analysis with domain knowledge and documents to rebuild abstractions and the reasons behind them

Redocumentation

Design recovery

Restructuring

Re-engineering

Reverse engineering

Forward engineering

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Two engineers investigate an undocumented scheduling application. Complete each description using either static analysis or dynamic analysis. Use each phrase once.


The first engineer parses the source files and extracts possible function calls without running the application. This technique is .


The second engineer runs selected scheduling scenarios and records which functions actually execute. This technique is .

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A developer must recover the design of an undocumented payroll module before changing its tax rules. Put the stages of a typical design-recovery workflow in order, from first to last.

  1. 1.

    Abstract the facts into a model, merging paired references and assigning multiplicities.

  2. 2.

    Validate the model with developers or by tracing real scenarios through the code.

  3. 3.

    Extract raw facts with tools: types, fields, inheritance links and method calls.

  4. 4.

    Collect the source code and any surviving documentation, build files and test suites needed to answer those questions.

  5. 5.

    Decide which questions the recovered model must answer, such as which classes the tax change will touch.

  6. 6.

    Record the validated design so later maintainers can reuse it.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice