Diagrams guide

Class diagrams: sketching the shape of your code

A class diagram shows the kinds of thing in a program, what each knows and does, and how they relate. Sketch one to agree a design before writing it.

Diagrams · · 4 minute read

The short answer

  • Give each kind of thing a box with what it knows and what it does.
  • Read every arrow aloud as a sentence, such as "an invoice is a kind of document".
  • Draw only the classes this decision needs, not the whole program.

A finished example

This is what the Class diagram template can look like once it is filled in. This one is for a small publishing tool.

An example made up for this guide. Your own board starts with the headings and prompts, and you add the notes.
Read the example as words
On the board
Author: name, text; email, text
Document: title, text; status, text; publish()
Report: period, text; total()
Invoice: amount, money; send()
Questions
Every document has a title and a status
Only an invoice has an amount
How the boxes are joined
Report, is a kind of, leads to Document
Invoice, is a kind of, leads to Document
Author, writes, leads to Document

What a class diagram shows

Most programs are built from classes. A class is a kind of thing the program deals with, such as a document, an invoice or an author. The class says what every thing of that kind knows, and what it can do.

A class diagram draws each class as a box. The name goes at the top. Under it are the facts the class holds, such as a title and a status. Under those are the actions it can carry out, written with () after the name, such as publish().

Lines between the boxes show how the classes relate. An invoice is a kind of document. An author writes documents. A report uses a calculator.

The diagram does not show what happens in what order. It shows the shape of the code: what exists and how the parts lean on each other.

That is useful at two moments. Before building, a team can agree a design while changing it costs nothing. Later, a new starter can see how an area of the code fits together without reading every file.

Classes, what they know and what they do

Start from the problem, not from the code. Listen to how people describe the work and pick out the nouns. Those are your first classes. Name each in the singular, in the team's own words.

For each class, list what it knows. These are its facts: a name, an amount, a date. Write the kind of information beside each where it helps, such as text, number or money.

Then list what it does. These are its actions, and each is a verb: send, publish, total. Put () after the name so actions stand apart from facts.

Keep each list short. Three to five lines is enough for a sketch. The aim is to show what each class is responsible for, not to list everything it will ever contain.

A good test is to finish the sentence "This class is responsible for...". If the answer needs the word "and" twice, the class is probably doing two jobs. Consider splitting it. If two classes give the same answer, consider joining them.

How classes relate

Draw an arrow wherever one class needs another, and label it. Three kinds of link cover most sketches.

  • "Is a kind of". An invoice is a kind of document, so it has everything a document has, plus its own extras. The arrow points from the special class to the general one.
  • "Has" or "is part of". An order has order lines. The arrow points from the part to the whole it belongs to.
  • "Uses". A report uses a calculator to do its sums, but does not own it. Draw this as a dashed arrow.

Read each arrow aloud as a sentence. "A report is a kind of document." If the sentence sounds odd, the link is wrong or points the wrong way.

Be careful with "is a kind of". It is the strongest link, and the hardest to undo later. Ask whether the special class really shares everything the general one has. If it shares only a few facts, "has" is usually the safer choice.

Leave out links that do not matter to the decision in front of you. A diagram where everything joins everything tells nobody anything.

Using the template on doodleboard

The Class diagram template on doodleboard shows a small publishing tool. Document holds a title, a status and a publish() action. Report and Invoice are each a kind of document. An Author writes documents. A Questions frame holds a note that asks "What do they all share?"

Each class is drawn as a data table. Double-click one to rename it or change its rows. Put the name of a fact or an action on the left, and the kind of information on the right. An action needs nothing on the right.

To relate two classes, draw a connector from one to the other and double-click it to add a label. To make it dashed, select it and set Line style to Dashed.

If the code or a design note already exists, you can paste it. Choose More, then Diagram from text (Mermaid), and start with the line classDiagram. The board draws a table for each class and an arrow for each link, labelled in the same plain words.

Review the sketch with the team before anyone builds from it. Take one real task, such as sending an invoice, and trace which classes take part. If the path wanders through too many boxes, the design may need another look.

Questions people ask

Do I need to know UML to draw a class diagram?

No. UML is a formal set of drawing rules, and it helps for detailed work. For a sketch, boxes with a name, a few facts and actions, and labelled arrows in plain words are enough.

How is a class diagram different from a data model?

A data model shows what is stored and how records link. A class diagram shows the code: what each kind of thing knows and also what it does. The two often look alike, and one can start from the other.

How much of the program should one diagram cover?

One area, or one decision. Six to ten classes is plenty. A drawing of the whole program is too crowded to read and out of date within a week. Draw small diagrams when you need them.

Templates for diagrams and mapping

Put it on a shared whiteboard

Start for free with one board and up to three people, or try every feature for 7 days.

Start for freeSee pricing