1.3.16 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:

DisplayRoster
StartMatch
ResetScore

Each name suggests one responsibility.

Avoid the "Do Everything" Method

A method named:

DoEverything

might:

  • create players;
  • update scores;
  • display results;
  • change team state.

That makes the source harder to understand and change.

A focused method makes it easier to answer:

Design the Purpose Before the Syntax

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

For example:

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:

Player
Team
Match

Methods usually name behavior:

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:

  • parameters;
  • return values;
  • method overloading;
  • advanced helper patterns.

Those ideas appear later.

Focus on a method that performs one clear task.