If you've ever stared at a blank editor trying to remember whether it's --> or ..> for a dependency relationship, you already know why a UML diagram scripting cheat sheet matters. Writing UML diagrams as code using tools like PlantUML or Mermaid saves hours compared to dragging boxes around in a GUI. But the syntax rules are easy to forget, especially when you're switching between diagram types. This cheat sheet gives you the fast, no-fluff reference you need to write UML scripts quickly and correctly.
What does "UML diagram scripting" actually mean?
UML diagram scripting is the practice of writing Unified Modeling Language diagrams using plain text code instead of a visual editor. You describe classes, actors, relationships, and sequences in a markup language, and a rendering engine converts that text into a diagram. Tools like PlantUML and Mermaid are the most common options for this approach. The result is the same kind of UML diagram you'd produce in a GUI tool class diagrams, sequence diagrams, activity diagrams, use case diagrams but the source lives in a text file you can version-control, diff, and collaborate on like any other code artifact.
Why do developers write UML diagrams as code?
There are a few practical reasons this approach keeps gaining traction:
- Version control. Text-based diagrams live in Git alongside your code. You can track changes, review pull requests, and see the full history of your architecture decisions.
- Speed. Once you know the syntax, typing a class diagram takes a fraction of the time it takes to draw one. No clicking, no aligning boxes, no fighting with connector lines.
- Automation. You can generate diagrams from code, embed them in documentation pipelines, or include them in CI/CD builds so your docs always stay current.
- Portability. A
.pumlor.mdfile with diagram markup works anywhere in your IDE, on GitHub, in Confluence, or in a static site generator.
If you're already familiar with Mermaid diagram syntax, UML scripting follows a similar text-to-diagram workflow with UML-specific notation rules.
Class diagram syntax at a glance
Class diagrams are the most common UML diagram people script. Here's the core syntax you'll use over and over:
Declaring classes and attributes
In PlantUML, you define a class with its name, then list attributes and methods inside braces:
@startuml
class User {
-id: int
-name: String
+getEmail(): String
+setPassword(pwd: String): void
}
@enduml
The + prefix means public, - means private, # means protected, and ~ means package-private. These match the standard UML visibility notation.
Relationships between classes
This is where most people need the cheat sheet most. Here's a quick reference:
- Association:
A --> B(solid line, open arrow) - Inheritance:
B --|> A(solid line, hollow triangle B extends A) - Implementation:
B ..|> A(dashed line, hollow triangle B implements A) - Composition:
A -- B(solid line, filled diamond A owns B) - Aggregation:
A o-- B(solid line, hollow diamond A contains B) - Dependency:
A ..> B(dashed line, open arrow A uses B)
A common mistake is mixing up composition and aggregation. Remember: composition means the child can't exist without the parent (a Room inside a House). Aggregation means the child can exist independently (a Student in a School).
Sequence diagram syntax
Sequence diagrams show how objects interact over time. The syntax is straightforward once you see the pattern:
@startuml
actor User
participant "Auth Service" as Auth
participant Database
User -> Auth: login(username, password)
Auth -> Database: queryUser(username)
Database --> Auth: user record
Auth --> User: JWT token
@enduml
Key symbols to remember:
->and-->for solid and dashed arrows between participants->>and-->>for asynchronous calls (open arrowheads)alt/elsefor conditional blocksloopfor repeated interactionsnote right ofornote left offor annotations
Activation and nesting
To show that an object is actively processing, use activate and deactivate:
@startuml
Client -> Server: request()
activate Server
Server -> Database: query()
activate Database
Database --> Server: results
deactivate Database
Server --> Client: response
deactivate Server
@enduml
For flowchart-style logic within a sequence, you can nest alt/else and loop blocks using the end keyword to close each block.
Use case diagram syntax
Use case diagrams are simpler to script. You define actors, use cases, and their relationships:
@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
UC2 ..> UC1 : <>
@enduml
The < stereotype means one use case always requires another. < means a use case optionally adds behavior. These are easy to confuse include is mandatory, extend is conditional.
Activity diagram syntax
Activity diagrams model workflows and business processes. The basic structure looks like this:
@startuml
start
:Receive order;
if (In stock?) then (yes)
:Process payment;
:Ship item;
else (no)
:Notify customer;
:Reorder from supplier;
endif
stop
@enduml
You can add swimlanes by putting double colons around the actor name, and fork/join bars using fork, fork again, and end fork for parallel activities.
State machine diagram syntax
State diagrams describe how an object changes states in response to events:
@startuml
[] --> Idle
Idle --> Processing : submit()
Processing --> Completed : success()
Processing --> Failed : error()
Failed --> Idle : retry()
Completed --> []
@enduml
The [] symbol represents the initial and final states. Guard conditions go in square brackets: Idle --> Processing : submit() [valid].
What are the most common UML scripting mistakes?
After working with text-based UML for a while, certain errors come up repeatedly:
- Forgetting the wrapper tags. PlantUML needs
@startumland@enduml. Without them, the renderer won't parse your diagram. - Wrong arrow direction.
A --> BandB --> Aproduce different diagrams. Make sure the arrow points from the source to the target. - Confusing inheritance and implementation.
--|>is inheritance (solid line)...|>is implementation (dashed line). Mixing them up changes the meaning completely. - Overloading one diagram. Keep each diagram focused on one concept. A single class diagram with 40 classes is harder to read than four diagrams with 10 each.
- Ignoring stereotypes and packages. Grouping classes into packages with
package "Name" { }keeps large diagrams organized and readable. - Inconsistent naming conventions. Use the same naming style throughout either
PascalCaseorsnake_caseso your diagrams match your codebase.
Quick reference table for PlantUML relationship symbols
Bookmark this section. It's the part of a cheat sheet you'll actually use daily:
--solid line (horizontal)|..dashed line>open arrowhead|>hollow triangle (generalization)filled diamond (composition)ohollow diamond (aggregation)#circle (required interface, in component diagrams)
Combine these building blocks to express any UML relationship. For example, A -- B gives you composition with a solid line, while A o-- B gives you aggregation.
How does Mermaid compare to PlantUML for UML scripting?
Mermaid has grown popular because it renders natively on GitHub, GitLab, and many documentation platforms without extra setup. Its UML support covers class diagrams, sequence diagrams, state diagrams, and use case diagrams though PlantUML still offers broader diagram type coverage and more fine-grained styling control.
If your team already uses Mermaid for other diagram types, the UML class diagram syntax in Mermaid looks like this:
classDiagram
class User {
-int id
-String name
+getEmail() String
}
User --> Order : places
The main syntax differences from PlantUML are the keyword classDiagram at the top, using --> without the leading class name on the same line, and slightly different visibility markers. Both tools produce clean, readable UML diagrams pick the one that fits your workflow.
Tips for keeping your UML scripts maintainable
A few habits make a big difference when you're managing UML as code across a real project:
- One diagram per file. Name files clearly:
user-class-diagram.puml,order-sequence.puml. Avoid cramming multiple diagrams into one file. - Use skinparam for consistent styling. PlantUML's
skinparamsettings let you control fonts, colors, and line styles globally so all your diagrams look uniform. - Add title and header metadata.
title Order Processing Flowat the top of each diagram makes rendered output self-documenting. - Keep comments in your scripts. Use
'for single-line comments in PlantUML. Future you (and your teammates) will appreciate the context. - Render frequently. Don't write 200 lines before checking the output. Render after every few lines to catch syntax errors early.
Where do you go from here?
Start by picking the one diagram type your team needs most usually class diagrams or sequence diagrams and script a real diagram from your current project. Keep this cheat sheet open in a tab while you write. Within a few sessions, the common syntax patterns will stick and you'll only need the cheat sheet for less frequent notations like state guards or activity forks.
Quick-start checklist:
- Choose your tool PlantUML for maximum UML coverage or Mermaid for platform-native rendering.
- Script your first class diagram with at least three classes and two relationship types.
- Render it and verify the arrows, diamonds, and visibility markers look correct.
- Add it to version control with a clear filename and a title inside the diagram.
- Share the rendered image or embedded diagram with your team for feedback.
- Build up your cheat sheet with the specific syntax patterns you use most.
Flowchart Markup Language Quick Start
Diagram-As-Code Syntax Comparison Chart: Mermaid, Plantuml, D2, and More
Plantuml Sequence Diagram Code Examples and Syntax Guide
Mermaid Diagram Syntax Reference Guide
Visual Mapping Techniques for Effective Business Strategy Analysis
Mind Map Diagram Symbols and Their Meanings Explained