1.3.14 Tracing Nested Method Calls

A Method Call Can Lead to Another Method Call

Consider:

team1.ShowTeam();

The ShowTeam method contains:

public void ShowTeam()
{
    this.DisplayName();
}

Then DisplayName contains:

public void DisplayName()
{
    System.Console.WriteLine(this.name);
}

Execution does not stay on one source line.

It moves through a sequence of method calls.

Trace from the Original Call

Start with:

team1.ShowTeam();

The first receiver is:

Activity diagram showing caller, then team1.ShowTeam(), then team1.DisplayName(), then Console.WriteLine(...).
Caller.
  • New
  • Changed
  • Removed

Each new method call temporarily moves execution into another method.

When the called method finishes, execution returns to the statement after the call in the previous method.

Method Calls Form a Stack of Active Work

Conceptually, while DisplayName() is running:

Activity diagram showing player1.ShowTeamName(), then team1.DisplayName().
Player1ShowTeamName.
  • New
  • Changed
  • Removed

Inside ShowTeamName, this refers to player1.

Inside DisplayName, this refers to team1.

Tracing requires both:

  • method name;
  • receiver object.
Use a Trace Table

A small trace can be organized as:

Step Current object Current method Next call
1 player1 ShowTeamName() team1.DisplayName()
2 team1 DisplayName() display statement
3 team1 DisplayName() finishes return to Player method
4 player1 ShowTeamName() finishes return to caller

This helps prevent the common mistake of assuming this always refers to the same object through the entire call chain.

Sequence Diagrams Visualize the Same Trace

A conceptual sequence diagram may show:

Sequence diagram showing caller player1 : Player team1 : Team, then ShowTeamName(), then DisplayName().
CallerPlayer1PlayerTeam1Team.
  • New
  • Changed
  • Removed

The vertical order shows call order.

The horizontal direction shows which object receives each message.

Return Values Are Not Part of This Trace Yet

Every method in the current examples is void.

Methods still finish and return execution to their caller.

They simply do not return a data value.

Methods that return values are taught later.

Do Not Skip Calls When Explaining Execution

A weak trace might say:

That may be the visible result, but it hides how execution reached that result.

A stronger trace explains:

That explanation connects source structure to runtime flow.

The Main Tracing Questions

At every method call, ask:

  1. Which object receives the call?
  2. Which method begins?
  3. What does this refer to inside that method?
  4. Does that method call another method?
  5. Where will execution return when the method finishes?

Those questions prepare you to use debugger stepping and the Call Stack in CO-037.