Diagrams as code 2.0
Tags: architecture, c4model, productivity
Published: 2021-05-13 13:34:33

---
title: Diagrams as code 2.0
published: true
description: 
tags: #architecture #c4model #productivity
//cover_image: https://direct_url_to_image.jpg
---

[Diagrams as code](https://www.thoughtworks.com/radar/techniques/diagrams-as-code) is becoming a popular way to diagram software architecture, particularly for long-lived high-level documentation. You write the diagram source in a text-based domain specific language (e.g. [PlantUML](https://plantuml.com), [C4-PlantUML](https://github.com/plantuml-stdlib/C4-PlantUML), [Mermaid](https://mermaid-js.github.io/mermaid/#), [WebSequenceDiagrams](https://www.websequencediagrams.com), [Graphviz/DOT](https://graphviz.org)) or a programming language (e.g. [Diagrams](https://github.com/mingrammer/diagrams)), and render diagrams using web-based or command line tooling. The benefits are well understood - writing the diagram source as text allows for easy integration into software development practices and toolchains, plus the automatic layout facilities allow authors to focus on content.

But there's a problem with this approach, which I'm going to call "diagrams as code 1.0" - if you need to create multiple diagrams, you need to create multiple diagram source files, and ensure that you keep these files in sync.

![Diagrams as code 1.0](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1sdt0gwi7btwkyw3r33b.png)

Some tools do provide a way to "include" snippets, but most don't, forcing you to repeat information across those diagram definitions. Once you violate the "DRY principle", you need to ensure that any changes you make to one diagram are reflected elsewhere. Easy in theory ("we'll just use global search and replace", etc), but that doesn't necessarily happen in the real world.
 
![Keeping diagrams in sync is important](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x6twl8qdt00u1ef39uhm.png)

One way to resolve this problem is to generate multiple diagrams from a single source file. To do that, we need to shift our thinking away from "diagrams", and towards "views of a model". That's the essence of "diagrams as code 2.0" - it's a way to define a model of our software architecture and the views that we'd like to see, ultimately resulting in a consistent set of diagrams that are generated for us.

![Diagrams as code 2.0](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7qrx3g2az3wsm1zkfq7m.png)

This is the concept implemented by the Structurizr tooling, the majority of which is open source. Structurizr builds upon "diagrams as code", providing a way to create __multiple diagrams__ from a __single model__, using a number of tools and programming languages. There are three key concepts:

1. Workspace - this is the definition of the model and views, described using an open JSON format.
2. [Authoring tools](https://structurizr.org/#authoring) - these provide a way to create a workspace. The most popular is the text-based [Structurizr DSL](https://structurizr.com/dsl).
3. [Rendering tools](https://structurizr.org/#rendering) - these provide a way to render the views as diagrams. Supported diagram formats include PlantUML, C4-PlantUML, Mermaid, DOT, WebSequenceDiagrams, Ilograph, and the Structurizr cloud service/on-premises installation.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ev571sjhh4nphn3ox85t.jpg)

The separation of authoring and rendering reduces lock-in to any particular diagramming tool. And having a model of your software architecture provides a way to slice and dice it to create many different views, which can then be rendered using the best tool for the task ... a static PNG, an interactive and zoomable SVG, or perhaps an interactive force-directed graph.

## Summary

To summarise the key differences:

- Diagrams as code 1.0: you write diagram definitions by hand
- Diagrams as code 2.0: diagram definitions are generated automatically, by tooling, based upon views of a model

Although many teams define their models by hand, there's nothing preventing you from generating the model automatically, in part or in whole, by parsing infrastructure as code definitions, parsing distributed log files, performing static analysis of a code base, etc. Such techniques are especially useful for teams seeking ways to diagram their larger and more complicated distributed architectures.