Most class diagrams are drawn once, during design, and then quietly stop matching the code. Six months later the box labelled OrderService still sits on a wiki page while the real class has grown eleven methods, and a new developer reads the diagram as if it were true. A diagram that lies is worse than no diagram, because it buys confidence it has not earned.
Key takeaways
UML 2.5.1 is the current formal version of the language, published by the OMG in December 2017; a class diagram is its static structure view: classes, their attributes, their operations and the relationships between them.
A class box has three compartments, and every member carries a visibility mark: + public, - private, # protected, ~ package. A leading / marks a derived value and an underline marks a static member.
Six relationships cover nearly every line you will draw: association, aggregation, composition, generalization, realization and dependency.
Multiplicity on the ends (1, 0..1, , 1..) is the part that has to match the code and the schema, and it is the part most diagrams get wrong.
UML 2 gives aggregation no semantics beyond a plain association, so a hollow diamond is a note to humans, not a rule the database enforces.
Working definition
A class diagram is a static structure diagram: it shows the system's classes, their attributes, their operations, and the relationships among objects. That wording comes from Wikipedia's Class diagram article, and it matches the static structure view in the OMG's UML 2.5.1 specification.
Why diagrams stop being true
The diagram itself is rarely the problem. The habit around it is. Three failures account for most stale class diagrams I have seen in real projects.
The first is the one-off drawing. Someone opens a diagram tool during design, produces a beautiful picture, exports a PNG and pastes it into a wiki. From that moment the picture is frozen while the code keeps moving. There is no diff, no review, no reminder, and no date on it, so nobody can tell whether it describes last month or last year.
The second is the wrong zoom. A diagram that lists every attribute and every getter of eleven classes is not a design document, it is a screenshot of a directory. Readers stop at the second box, and the one relationship that mattered is buried under forty lines of boilerplate.
The third is drawing for an audience that never arrives. If the only consumer of the diagram is an assessor or a template, it will be written to satisfy the template. Diagrams that stay alive are the ones a colleague opens during a code review to answer a specific question: what owns this object, and what happens when it is deleted?
The fix is unglamorous. Draw small diagrams of the parts that change, keep the source next to the code so it moves in the same commit, and put a checked-on date in the caption. A diagram of nine classes that is true beats a diagram of ninety that nobody trusts.
What the box actually carries
A class is drawn as a rectangle with up to three compartments: the name on top, the attributes in the middle, and the operations at the bottom. Wikipedia's description is precise about the details people skip: the name is bold, centred and capitalised; attributes and operations are left-aligned and start with a lowercase letter.
Before each member sits a visibility mark, and it is the fastest way to see whether a diagram is describing an interface or leaking internals. + is public, - is private, # is protected and ~ is package visibility. A derived value, one that is computed rather than stored, is written with a leading slash, so / taxRate: decimal says the tax rate is calculated. An underlined member is static, shared by every instance rather than held per object. An italic class name marks an abstract class, and a stereotype such as «interface» above the name marks an interface.
Tip
Write the type after the member name (- total: Money) and keep one member per line. If a compartment needs more than six lines to make your point, the diagram is trying to be a code listing; cut it and describe the rest in a sentence.
Six lines, six meanings
The lines are where a class diagram earns its keep, and where most of the misreading happens. Six relationships cover almost everything you will draw.
An association is a plain structural link: two classes that know about each other. An aggregation is a whole and its parts, drawn with a hollow diamond at the whole end, and the parts can outlive the whole: a pond has ducks, and draining the pond does not delete the ducks. A composition is the stricter version, a filled diamond, where the whole controls the parts' lifetime: delete the order and its lines go with it.
Generalization is the “is a” line, a solid line with a hollow triangle pointing at the parent, so any instance of the child is also an instance of the parent. Realization is the same hollow triangle on a dashed line: a class implementing an interface. Dependency is a dashed line with an open arrow, meaning “uses”: a change in the supplier can force a change in the client, without either holding a long-lived reference to the other.
Caution
The hollow diamond is weaker than it looks. UML 2 does not define any semantics for aggregation beyond a plain association, which Wikipedia states plainly. Two teams can read the same diamond differently, and no code generator or database will enforce it. If lifetime really matters, say it in words on the diagram, or model it as composition and enforce the cascade in the code.
Multiplicity is a promise the code must keep
Multiplicity sits at the end of a line and says how many objects may appear there. 1 means exactly one. 0..1 means zero or one, the shape of an optional reference. * means zero or more and 1..* means at least one. Read the diagram out loud and the sentence should sound like a business rule: one customer has zero or more orders; an order has one or more lines; each line points at exactly one product.
This is where diagrams quietly disagree with the database. A line marked 1..* promises at least one row, but a foreign key column with no NOT NULL and no application check happily stores zero. A 0..1 end promises at most one, which is a unique index, not just a column. Drawing the arrow without adding the constraint is how a diagram becomes a wish.
A multiplicity that no constraint enforces is a comment, and comments drift.
“A class diagram should show every attribute and method, or it is incomplete.”
A diagram is an argument about structure, not a copy of the source. The members worth drawing are the ones that explain a relationship or a rule: the foreign key, the status field that drives behaviour, the computed total. Everything else belongs in the code, where it stays current for free.
A worked example: splitting a class that does everything
Here is a shape that appears in almost every growing codebase: one service class that started as a convenience and became the place where everything happens.
class OrderService {
createOrder(cart, customer) { /* builds lines, prices, saves */ }
priceOrder(cart) { /* discounts, tax, shipping */ }
renderInvoice(order) { /* PDF layout */ }
sendInvoiceEmail(order) { /* template + transport */ }
logAudit(order) { /* who did what */ }
}
Nothing here is wrong on its own; the problem is that five different reasons to change meet in one file. Drawing it exposes the issue faster than reading it does, because a single box with five unrelated duties looks exactly as uncomfortable as it is. Split by reason to change and the diagram becomes an argument for the split.
The result is smaller than it looks. Order keeps identity and state, Pricing owns the calculation and can be tested with a table of carts and expected totals, and Invoice owns rendering. The original name survives as a facade, so callers do not have to change on the same day:
class OrderService {
constructor(orders, pricing, invoices) { ... }
createOrder(cart, customer) {
const order = this.orders.create(cart, customer);
order.total = this.pricing.total(cart);
return this.invoices.issue(order);
}
}
This is the point where the diagram pays for itself. The next time someone asks where a discount rule belongs, the answer is visible in one picture instead of buried in a 600-line file, and the picture is short enough to stay true.
Keep the diagram as text
A diagram that lives only in a drawing tool will drift. The practical fix is to write it as text in the repository, next to the code it describes, so it appears in the same pull request as the change. Two tools make that easy and both are free.
PlantUML reads almost like the code it describes: visibility markers +, - and #, a colon before the type, parentheses for methods. Mermaid is supported directly by many Markdown viewers, including GitHub, and writes relationships with arrows such as Animal <|-- Duck for generalization and Animal : +int age for a member.
classDiagram
Customer "1" --> "*" Order
Order "1" --> "1..*" OrderLine
OrderLine "*" --> "1" Product
Order : -string number
Order : -date placedAt
Once the source is text, two habits follow naturally: a diagram change shows up as a diff in review, and comparing two versions is a text comparison rather than a game of spot the difference. If the Markdown around the diagram needs to become clean HTML for a wiki page, the Markdown converter does that in the browser, and when two branches disagree about the diagram source, the text diff shows exactly which lines changed.
A checklist before you share it
☐ The diagram answers one question, and I can say that question in a sentence.
☐ Every line has a kind: association, aggregation, composition, generalization, realization or dependency, and the style matches the meaning.
☐ Every association end has a multiplicity, and each one matches a constraint in the code or the schema.
☐ Visibility marks are present, and nothing private is shown that the reader does not need.
☐ The caption says what the diagram shows, who drew it and when it was last checked.
☐ The source lives in the repository, so the next change arrives as a diff and not as a new picture.
“UML is dead; nobody draws diagrams any more.”
What faded is the habit of buying a modelling tool and drawing everything before writing code. The notation is still maintained — the OMG lists UML 2.5.1 as the current formal version, published in December 2017 — and the text-based tools built on it are more widely used than the drawing tools ever were, because a diagram in a pull request costs nothing to keep.
Further reading
The specification itself: UML 2.5.1 at the OMG (formal, December 2017), which is the primary source for every notation rule above. For the notation with examples: Wikipedia's Class diagram. For the two text tools: PlantUML class diagram syntax and Mermaid class diagrams. Pages change; treat them as a starting point and read the specification for the definitions.
Part 5 of 14 — Engineering Foundations series
Previous: Behavioral Design Patterns: Strategy, Observer and Command. Next in the series: UML sequence and activity diagrams, which take the same classes and show what happens when they talk to each other.
Browse the rest of the series on the AIGS blog, or see how we apply it on client work on the services page.
Originally published at ai-gs.net.
Top comments (0)