1.5.2 Representing Method Returns in Class Diagrams

A Class Diagram Can Show a Method's Return Type

A UML class diagram can describe both:

  • the method name;
  • the type of value the method returns.

Suppose a soccer Team class has a method that returns its score.

A class-diagram method entry can be written as:

GetScore() : int

Read it as:

The Return Type Appears After the Colon

In:

GetScore() : int

the pieces are:

GetScore

method name

()

parameter list

int

return type

The colon separates the method portion from the return type.

Compare a Returning Method with a void Method

A class might include:

DisplayScore() : void
GetScore() : int

The first method performs behavior without returning a data value.

The second method returns an integer.

The UML notation lets you see that difference before reading the C# implementation.

Connect UML to C#

UML:

GetScore() : int

C#:

public int GetScore()
{
    return this.score;
}

The class diagram describes the method structure.

The C# contains the executable behavior.

The two representations should agree about:

  • method name;
  • parameters;
  • return type.
A Class Diagram Shows the Return Type, Not One Current Returned Value

Suppose:

team1.score = 3

A call to GetScore() may produce:

3

But the class diagram still shows:

GetScore() : int

It does not normally change to:

GetScore() : 3

Why?

Because the class diagram describes the method's type structure for every Team object.

The value 3 belongs to one runtime situation.

Parameters and Return Types Can Appear Together

You already know parameter notation such as:

SetScore(score : int) : void

A returning method can also have a parameter, although CO-043 keeps the examples simple.

The reading pattern is:

MethodName(parameterName : Type) : ReturnType

The parentheses describe information going into the method.

The final type describes information coming out.

Return Types Help Explain How a Method Can Be Used

Compare:

DisplayScore() : void

with:

GetScore() : int

A caller cannot expect a numeric result from DisplayScore().

A caller can expect an integer from GetScore().

The class diagram therefore communicates an important part of the method contract.

Use the Type Required by the Design

If the method returns:

int

the UML should show:

: int

Do not change the UML to double because another numeric type seems more flexible.

The model and code should represent the same design.

Keep Return Types Separate from Fields

A class can contain a field:

score : int

and a method:

GetScore() : int

They use the same data type for different reasons.

The field:

score : int

describes stored object state.

The method:

GetScore() : int

describes behavior that produces an integer for the caller.

A Simple Soccer Class View

Conceptually, a Team class might include:

Team
-------------------------
name : string
score : int
-------------------------
DisplayScore() : void
GetScore() : int

Read it as:

The Main UML Pattern

For a parameterless returning method:

MethodName() : ReturnType

Example:

GetScore() : int

The class diagram answers: