The C4 Model

The only diagrams you'll ever need (maybe)

Have you ever started a new code project and wondered what is the first diagram I should draw? What subsequent diagrams should I put together? The options are many and I feel UML provided us with too many diagrams, so where should we start?

Let me introduce you to the C4 Model by Simon Brown.

Context

Let's look at an example online eBook publishing system. Users can sign up to view a number of eBooks with a monthly subscription. Authentication and subscription payments are being provided by third party systems. The best place to start is at the highest level, your system as one box.

You can then draw the interactors (i.e. users and external systems) connecting to your system - this is your context.

context

Container

This represents the deployable units or entities in your system. You may have a web app, mobile app, database, any of these type of entities should be shown at this level.
context

Component

These are the building blocks of your deployable units. Controllers, high level models, gateways. Here show the relationship between components.

context

Code/Class

Now diagrams of code level artifacts carry one big risk - that they may go out of date very quickly during refactoring. Should you even diagram your classes with this in mind? I think the key here is to look at the core models of your system and pick those. You may even have just one core domain model and it's definitely worth documenting that.

context

Is that it? Well no, even the original author of C4 suggests some additional diagrams such as System Landscape, Dynamic and Deployment diagrams. One of my most frequently used UML diagrams is the Sequence Diagram especially where there is a complex set of interactions between internal components and/or external systems. Flow charts are also useful for communicating process between technical and non-technical people.

The C4 Model:

Diagramming Tools: