1.3.2 Designing Single-Purpose Methods

A Method Should Have a Clear Job

A method groups behavior under a name.

A single-purpose method performs one focused task that can be described clearly.

For a soccer program, examples might include:

Plain text
DisplayRoster
StartMatch
ResetScore

Each name suggests one responsibility.

Avoid the "Do Everything" Method

A method named:

Plain text
DoEverything

might:

That makes the source harder to understand and change.

A focused method makes it easier to answer:

What is this behavior responsible for?

Design the Purpose Before the Syntax

Before writing a method, state its job in one sentence.

For example:

Display the current team's name.

If the sentence contains several unrelated actions joined by "and," consider whether the behavior is too broad.

Method Names Should Communicate an Action

Classes often name things:

Plain text
Player
Team
Match

Methods usually name behavior:

Plain text
DisplayRoster
StartMatch
ClearMessage

The name should help a reader predict what the method does.

Single Purpose Supports Debugging

If one focused method produces the wrong result, the investigation area is smaller.

A method that changes unrelated parts of the application makes state changes harder to trace.

Start Small

At this stage, you are learning basic methods.

Do not add:

Those ideas appear later.

Focus on a method that performs one clear task.