Every software project starts with a plan. Whether you're sketching out a new feature, documenting an existing system, or communicating architecture to your team, UML diagrams give structure to ideas that words alone can't capture. But here's the thing most developers don't draw UML diagrams by hand anymore. They write them as code. And that's exactly why a reliable UML diagram code reference guide is something every developer, architect, and technical writer needs within reach.
UML-as-code lets you version-control your diagrams, generate them from plain text, and keep them in sync with your actual codebase. Tools like Mermaid.js and PlantUML have made this standard practice. But the syntax can be tricky to remember especially when you switch between diagram types. This guide covers the most-used UML diagram code notations, practical examples, and the mistakes that trip people up.
What Is a UML Diagram Code Reference Guide?
A UML diagram code reference guide is a written resource that documents the text-based syntax used to create UML diagrams programmatically. Instead of dragging shapes on a canvas, you write structured text usually in a domain-specific language that a rendering tool converts into a visual diagram.
The two most common syntaxes are:
- PlantUML A Java-based tool that uses its own text format to generate class diagrams, sequence diagrams, use case diagrams, and more.
- Mermaid.js A JavaScript library that renders diagrams from Markdown-like syntax. It integrates directly with GitHub, GitLab, and many documentation platforms.
A good reference guide catalogs the specific keywords, symbols, and patterns each syntax supports, along with examples showing what the rendered output looks like.
When Do Developers Actually Need This?
You reach for a UML diagram code reference in several common situations:
- Documenting system architecture Class diagrams and component diagrams stored alongside source code in version control.
- Planning API interactions Sequence diagrams showing how services communicate before writing implementation code.
- Onboarding new team members Visual references that explain how modules connect without requiring someone to read every source file.
- Writing technical specifications RFPs, design docs, and RFCs that need diagrams embedded directly in Markdown or HTML.
- Database design reviews Entity-relationship diagrams generated from text to visualize table relationships. If that's your use case, you may find our database schema diagram code editor helpful for quickly drafting ER diagrams.
The pattern is always the same: you know what you want to draw, but you can't remember the exact syntax to write it.
What Are the Most Common UML Diagram Types You Can Write as Code?
Class Diagrams
Class diagrams describe the structure of a system classes, their attributes, methods, and relationships. In PlantUML syntax, a basic class looks like this:
@startuml
class User {
-name: String
-email: String
+login(): void
+logout(): void
}
class Order {
-orderId: int
-total: float
+calculateTotal(): float
}
User "1" -- "" Order : places >
@enduml
The + symbol means public, - means private, and the relationship line with cardinality shows that one user can have many orders.
Sequence Diagrams
Sequence diagrams show how objects interact over time messages passed between participants in a specific order. This is one of the most popular UML diagram types written as code because timing and ordering are hard to get right by hand.
Mermaid syntax for a sequence diagram looks like this:
sequenceDiagram
participant Client
participant Server
participant Database
Client->>Server: POST /login
Server->>Database: SELECT user
Database-->>Server: user data
Server-->>Client: 200 OK + token
For a deeper look at sequence diagram syntax and ready-to-use templates, check our sequence diagram code generator.
Use Case Diagrams
Use case diagrams show who interacts with a system and what they do. PlantUML supports these natively:
@startuml
left to right direction
actor Customer
actor Admin
rectangle "Online Store" {
usecase "Browse Products" as UC1
usecase "Place Order" as UC2
usecase "Manage Inventory" as UC3
}
Customer --> UC1
Customer --> UC2
Admin --> UC3
@enduml
Activity Diagrams
Activity diagrams model workflows decision points, parallel processes, and sequential steps. Think of them as flowcharts with UML semantics.
@startuml
start
:Receive order;
if (In stock?) then (yes)
:Ship item;
else (no)
:Backorder item;
endif
:Send confirmation email;
stop
@enduml
If you're working with flowchart-style logic and want syntax templates, our flowchart diagram code templates cover common patterns you can adapt.
State Diagrams
State diagrams track how an object changes status in response to events:
@startuml
[] --> Idle
Idle --> Processing : submit request
Processing --> Approved : approval granted
Processing --> Rejected : denial
Approved --> []
Rejected --> Idle : resubmit
@enduml
What Do These Syntax Elements Mean at a Glance?
Here's a quick reference table of the most common syntax patterns across PlantUML and Mermaid:
| Element | PlantUML | Mermaid |
|---|---|---|
| Start a diagram | @startuml / @enduml |
Diagram type keyword on first line |
| Declare a class | class Name { } |
class Name |
| Arrow (sync call) | A -> B : message |
A->>B: message |
| Arrow (return) | A --> B : response |
A-->>B: response |
| Relationship | A -- B or A --> B |
A--B or A-->B |
| Notes | note left: text |
Not all diagram types support notes |
| Stereotypes | <<interface>> |
Limited support |
| Colors/styles | #color inline |
style directives |
What Mistakes Do People Make With UML Diagram Code?
Forgetting arrow direction or style
This is the most common error. A solid arrow (->) means a direct association or synchronous call. A dashed arrow (-->) usually means a return message or dependency. Mixing them up changes the meaning of the diagram entirely.
Not closing diagram blocks
PlantUML requires @enduml to close every diagram. Mermaid doesn't use closing tags, but does require consistent indentation. Missing these details causes rendering failures that are hard to debug because the error messages are often vague.
Overloading a single diagram
Trying to show an entire system in one class diagram with 40 classes makes the diagram unreadable. Break complex systems into multiple focused diagrams. Each one should answer one specific question about your architecture.
Using inconsistent naming conventions
If your class diagram uses CamelCase but your sequence diagram uses snake_case, the documentation looks sloppy and becomes harder to maintain. Pick a convention and stick with it across all diagrams in a project.
Ignoring cardinality on relationships
Writing User -- Order without specifying "one-to-many" loses important information. Always include cardinality markers: "1" -- "", "0..1" -- "1..", etc.
Practical Tips for Working With UML Diagram Code
- Store diagrams as
.pumlor.mmdfiles in your repo treat them like source code with pull requests and reviews. - Use comments liberally PlantUML supports
' commentsyntax, and Mermaid supports%%. Add context that won't appear in the rendered output. - Preview constantly Use browser-based editors or VS Code extensions to see rendered output as you type. Don't write 200 lines blind and then check.
- Start with a rough sketch, then refine Get the relationships and structure right first, then add styling, notes, and stereotypes.
- Learn one diagram type deeply before branching out Master class diagrams or sequence diagrams first. The patterns you learn transfer to other types.
- Reuse templates Keep a library of your most-used diagram patterns. Most teams only use 3–4 diagram types regularly, so a small template library covers most needs.
How Do You Choose Between PlantUML and Mermaid?
Both are solid tools, but they fit different workflows:
- Choose PlantUML if you need advanced UML features like deployment diagrams, timing diagrams, or complex stereotypes. It supports more diagram types and has more fine-grained styling options.
- Choose Mermaid if your priority is easy integration with Markdown-based documentation. It works natively in GitHub READMEs, GitLab wikis, Notion, and many static site generators.
Many teams use both PlantUML for detailed design docs and Mermaid for lightweight inline diagrams in READMEs and wikis.
Checklist: Your Next Steps With UML Diagram Code
- Pick your tool Install PlantUML (requires Java or a Docker image) or use Mermaid (no install needed if you use the Mermaid Live Editor).
- Choose your first diagram type Start with sequence diagrams if you're documenting APIs, or class diagrams if you're documenting data models.
- Write your first diagram Use 5–10 lines of code. Keep it simple. Render it and check the output.
- Save the file in version control Commit it alongside the code it documents.
- Build a small template library After you've written 3–4 diagrams, extract reusable patterns into template files your team can reference.
Start small, keep your diagrams focused, and let the code do the drawing for you.
Sequence Diagram Code Generator Tool Template
Flowchart Diagram Code Template Syntax
Mermaid Diagram Code Snippet Library for Quick Template Generation
Database Schema Diagram Code Editor Online
Mermaid Diagram Scripting for Beginners: Easy Step-by-Step Tutorial
Diagram Types Reference Guide for Software Engineers