Skip to content

C4 Level 4: Code Diagram

The C4 model describes a system in four levels of detail: system context, containers, components and code (see the article C4 Model). The code diagram is the last one. It zooms into a single component from the level above (see C4 Component Diagram).

What it shows. The elements are code elements inside the component in scope. Examples are classes, interfaces, objects, functions and database tables.

Notation. Teams typically draw it as a UML class diagram (see UML Class Diagram). An entity relationship diagram, which shows database tables and how they link, or a similar notation also works.

Optional and generated. This level is optional. An IDE (the editor developers write code in) or a UML modeling tool can often produce it on demand. Ideally, a tool generates the diagram automatically instead of a person drawing it.

When to use it. Long-lived documentation should usually not include this level, because most IDEs can generate it whenever someone needs it. Keep it for the most important or most complex components.

Link to arc42. arc42 is a documentation template (see arc42 Architecture Documentation Template). Its building block view is hierarchical. Level 3 and deeper zoom into selected blocks of the level above. This parallels the lower C4 levels. arc42 also advises that every piece of source code can be located in the building block view.

A team has a “Payment Handling” component. It is complex and central to the product. A developer opens the IDE and generates a class diagram of that component. It shows the classes, interfaces and the tables they use. The developer uses it during a design discussion. The team does not store it in the long-lived documentation, because the IDE can produce a fresh one at any time.

The benefit is detail. Developers and architects can see exactly which code elements make up a component.

The cost is upkeep. Because tools can generate this view on demand, a hand-drawn copy in long-lived documentation adds little. So draw or keep it only for the most important or complex components.

  • The documentation includes a code-level diagram only for the most important or complex components.
  • Code-level diagrams are generated by an IDE or modeling tool instead of drawn by hand.
  • Each code-level diagram covers a single component and shows code elements such as classes, interfaces or tables.
  • Every piece of source code can be located in the building block view.