Mapping Software Architecture to Code
Join the DZone community and get the full member experience.Join For Free
bringing software architecture back into the domain of the development team
one of the things i'm currently doing with a number of software teams is teaching them how to draw pictures. as an industry we've got really good at visualising the way that we work using things like kanban boards and story walls, but we've forgotten how to visualise the software that we're building. in a nutshell, many teams are trying to move fast but they struggle to create a shared vision that the whole team can work from, which ultimately slows them down. and few people use uml nowadays, which just exaggerates the problem. i've written an article about this and it's due for publication soon (i'll come back and add a link) plus it's covered in my software architecture for developers ebook and in a number of talks that i'm doing around europe ( itarc , iasa uk , mix-it ) and the us ( saturn ) during april. here are the slides from agile software architecture sketches - nouml! that i presented a few weeks ago in dublin.
the tl;dr version
the tl;dr version of this post is simply this ... if you're building monolithic software systems but think of them as being made up of a number of smaller components, ensure that your codebase reflects this. consider organising your code by component (rather than by layer or feature) to make the mapping between software architecture and code explicit. if it's hard to explain the structure of your software system, change it.
decomposition into components
for the purpose of this post, let's assume visualising a software system isn't a problem and that you're sketching some ideas related to the software architecture for a new system you've been tasked to build. an important aspect of "just enough" software architecture is to understand how the significant elements of a software system fit together. for me, this means going down to the level of components, services or modules. it's worth stressing this isn't about understanding low-level implementation details, it's about performing an initial level of decomposition. the wikipedia page for component based development has a good summary, but essentially a component might be something like a risk calculator, audit logger, report generator, data importer, etc. the simplest way to think about a component is that it's a set of related behaviours behind an interface, which may be implemented using one or more collaborating classes. good components share a number of characteristics with good classes. they should have high cohesion, low coupling, a well-defined public interface, good encapsulation, etc.
there are a number of benefits to thinking about a software system in terms of components, but essentially it allows us to think and talk about the software as a small number of high-level abstractions rather than the hundreds and thousands of individual classes that make up most enterprise systems. the photo below shows a typical component diagram produced during the training classes we run. groups are asked to design a simple financial risk system that needs to pull in some data, perform some calculations and generate an excel report as the output.
this sketch includes the major components you would expect to see for a system that is importing data, performing risk calculations and generating a report. these components provide us with a framework for partitioning the behaviour within the boundary of our system and it should be relatively easy to trace the major use cases/user stories across them. this is a really useful starting point for the software development process and can help to create a shared vision that the team can work towards. but it's also very dangerous at the same time. without technology choices (or options), this diagram looks like the sort of thing an ivory tower architect might produce and it can seem very "conceptual" for many people with a technical background.
talk about components, write classes
people generally understand the benefit of thinking about software as higher level building blocks and you'll often hear people talking in terms of components when they're having architecture discussions. this often isn't reflected in the codebase though. take a look at your own codebase. can you clearly see components or does your codebase reflect some other structure? when you open up a codebase, it will often reflect some other structure due to the organisation of the code. mark needham has a great post called coding: packaging by vertical slice that talks about one approach to code organisation and a google search for "package by feature vs package by layer" will throw up lots of other discussions on the same topic. the mapping between the architectural view of a software system and the code are often very different. this is sometimes why you'll see people ignore architecture diagrams (or documentation) and say "the code is the only single point of truth".
auto-generating architecture diagrams
to change tack slightly, i was in dublin a few weeks ago and i met chris chedgey, who is part of the inspiration behind this post. chris is the co-founder of a company called headway software and they have a product called structure101 . you should take a look if you've not seen it before, they have some cool stuff in the pipeline. i won't do their product any justice by trying to summarise what it does, but one of its many features is to visualise and understand an existing codebase .
when i teach people how to visualise their software systems, we create a number of simple nouml sketches at different levels of abstraction. these are the context , containers and components diagrams. this context, containers and components approach is basically just a tree structure. a system is made up of containers (e.g. a web server, application server, database, etc), each of which is further made up of components. you can see some example diagrams on flickr and in my book.
given this is really just a tree structure, it should be fairly straightforward to auto-generate these diagrams from an existing codebase. and perhaps there is a tool out there that can do this, but i've never seen one that has worked really well. microsoft visual studio can generate some layer diagrams but i've never met anybody that really raves about the architecture diagram support. most tools generate diagrams showing dependencies between packages or classes but they don't tend to show components. and what's a component anyway? is any class that implements an interface a component? if you're using inversion of control, perhaps everything that you inject is a component?
there are a number of reasons why auto-generating such diagrams is tricky but, once we start coding, much of the semantics associated with "containers" (runtime environments, process boundaries, etc) and "components" becomes lost of the sea of classes that make up the typical codebase. many developers break their systems up into a number of projects within their ides to represent reusable libraries and deployable units but external tools often don't have access to this information if they are solely working from a bunch of jar files or dlls (for example). in essence, the information related to the abstract structural elements isn't adequately represented within a codebase. if you take a look at most codebases, i'm fairly sure that you could come up with a set of rules as to what defines a component but perhaps it would be easier to simply make these concepts explicit. some techniques already exist to do this (e.g. the architecture description language ) but i've never seen them used in the corporate world.
packaging by component
to bring this discussion back to code, the organisation of the codebase can really help or hinder here. organising a codebase by layer makes it easy to see the overall structure of the software but there are trade-offs. for example, you need to delve inside multiple layers (e.g. packages, namespaces, etc) in order to make a change to a feature or user story. also, many codebases end up looking eerily similar given the fairly standard approach to layering within enterprise systems. uncle bob martin says that if you're looking at a codebase, it should scream something about the business domain. organising your code by feature rather than by layer gives you this, but again there are trade-offs. a variation i've been experimenting with is organising code explicitly by component. the following screenshot shows an example of this in the codebase for my techtribes.je website (a content aggregator and portal for jersey's digital sector). this screenshot only shows the core components; there's a separate spring mvc project and the controllers use the components illustrated here.
this is similar to packaging by feature, but it's more akin to the "micro services" that mark needham talks about in his blog post . each sub-package of je.techtribes.component houses a separate component, complete with it's own internal layering and spring configuration. as far as possible, all of the internals are package scoped. you could potentially pull each component out and put it in it's own project or source code repository to be versioned separately. this approach will likely seem familiar to you if you're building something that has a very explicit loosely coupled architecture such as a distributed messaging system made up of loosely coupled components. i'm fairly confident that most people are still building something more monolithic in nature though, despite thinking about their system in terms of components. i've certainly packaged *parts* of monolithic codebases using a similar approach in the past but it's tended to be fairly ad hoc. let's be honest, organising code into packages isn't something that gets a lot of brain-time, particularly given the refactoring tools that we have at our disposal. organising code by component lets you explicitly reflect the concept of "a component" from the architecture into the codebase. if your software architecture diagram screams something about your business domain (and it should), this will be reflected in your codebase too.
the structural elements of software
we could create a convention here to say that all sub-packages of je.techtribes.component are components, but it would be much easier to explicitly mark components using metadata. in java, we could use annotations to do this, attributes in .net, etc. if we used the same approach for other structural elements of software (e.g. services, layers, containers, etc), tool vendors could use this metadata to generate meaningful and *simple* architecture diagrams automatically. plus, they could also use this structural information to generate dependency diagrams that focus on components rather than classes. i've started experimenting with annotations as a way to do this and i've created a github repo to store whatever i come up with.
the major caveat to all of this is that designing a software system based around components isn't "the only way". it's a nice approach to think about software systems that are more monolithic in nature and it's a great stepping stone to designing loosely coupled architectures. but it isn't a silver bullet. regardless of how you design software, i do hope this post has got you thinking about the mapping between software architecture and how it's reflected in the code.
software architecture and coding are often seen as mutually exclusive disciplines and there's often very little mapping from the architecture into the code and back again. effectively and efficiently visualising a software architecture can help to create a good shared vision within the team, which can help it go faster. having a simple and explicit mapping from the architecture to the code can help even further, particularly when you start looking at collaborative design and collective code ownership. furthermore, it helps bring software architecture firmly back into the domain of the development team, which is ultimately where it belongs.
Published at DZone with permission of Simon Brown, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Microservices With Apache Camel and Quarkus (Part 2)
Health Check Response Format for HTTP APIs
Comparing Cloud Hosting vs. Self Hosting