Unit 2 of 5 · about 54 minutes
2.1 Principles of good software design
Software design is about organising code so that it meets its requirements today and can be understood and changed safely tomorrow. Two programs can produce exactly the same output while differing enormously in how easy they are to maintain. Good design reduces the effort, risk and cost of every future change, which, as the previous unit noted, is where most of the lifetime cost of software lies.
Managing complexity
The central challenge in software design is complexity. As systems grow, the number of parts and the connections between them increase rapidly. If every part depends on many others, a change in one place can cause unexpected failures elsewhere, and nobody can hold the whole system in their head. Most design principles are, at heart, ways of keeping complexity under control.
Modularity, cohesion and coupling
Modularity means dividing a system into separate parts, or modules, each responsible for a clear piece of functionality. Two properties determine whether this division is helpful.
- Cohesion describes how closely the contents of a module relate to one another. High cohesion is desirable: a module that handles everything about invoices, and nothing else, is easier to understand than one that mixes invoices, user settings and email formatting.
- Coupling describes how much modules depend on each other. Low coupling is desirable: if the payments module relies on the internal details of the customer module, changing one will often break the other.
The general goal is high cohesion and low coupling. Each module does one job well and interacts with others through clear, limited connections.
Abstraction and encapsulation
Abstraction means presenting the essential features of something while hiding unnecessary detail. When you use a "send email" function, you do not need to know how the email server protocol works. Encapsulation is the related practice of keeping a module's internal data and workings hidden, exposing only a well-defined interface. Because other parts of the system rely only on the interface, the internals can be changed or improved without affecting them.
Some widely used principles
- Separation of concerns: keep different responsibilities, such as data storage, business rules and user interface, in different parts of the code.
- Don't repeat yourself (DRY): avoid duplicating the same knowledge or logic in several places, since changes must then be made consistently everywhere. However, merging code that only looks similar but represents different ideas can create unhelpful coupling.
- Keep it simple (often abbreviated KISS): prefer the simplest design that meets the need.
- You aren't gonna need it (YAGNI): avoid building features or flexibility for imagined future needs, which add complexity and may never be used.
In object-oriented programming, the five SOLID principles offer more specific guidance. The first, the single responsibility principle, states that a module or class should have only one reason to change. The others deal with how to extend code without modifying it, how to design substitutable components, how to keep interfaces focused and how to depend on abstractions rather than concrete details.
Tip: code is read far more often than it is written. Choose clear names for variables, functions and modules, so that a colleague can understand what the code does without needing to ask you.
Readable code
Design also operates at the level of individual lines and functions. Readable code typically has meaningful names, short functions that each do one thing, consistent formatting and comments that explain why something is done rather than repeating what the code obviously does. Many teams agree a shared style guide and use automatic formatting tools so that all code looks consistent.
Design patterns
Design patterns are named, reusable solutions to common design problems, such as creating objects flexibly or notifying several parts of a system when something changes. They give teams a shared vocabulary. They should be used when they solve a real problem, not applied for their own sake, since unnecessary patterns add complexity.
Common mistakes
- Creating very large modules or functions that do many unrelated things.
- Allowing modules to reach into each other's internal data.
- Building elaborate, general-purpose frameworks before they are needed.
- Copying and pasting code rather than creating a shared function where the logic is genuinely the same.
- Using unclear names, such as "data2" or "doStuff".
In practice
If you write code, review a piece you wrote some months ago. Could a colleague understand it quickly? Are there functions doing several unrelated things, or duplicated logic? Make one small improvement, such as renaming something unclear or extracting a well-named function. If you do not write code, ask a developer colleague how they keep their code maintainable; the conversation will show you how these principles apply in your organisation's systems.
2.2 Software architecture styles and trade-offs
Software architecture concerns the high-level structure of a system: its major components, how they interact and the principles that govern its design and evolution. Architectural decisions are typically the most expensive to change later, so they deserve careful thought. There is rarely a single correct answer; architecture is about making trade-offs in light of a system's most important quality attributes.
Architecture drivers
Architectural choices are shaped by a small number of drivers:
- key functional requirements;
- the most important quality attributes, such as scalability, availability, security or time to market;
- constraints, such as budget, existing systems, regulations and the team's skills.
A system processing card payments for millions of users has very different drivers from an internal tool used by twenty people, and its architecture should reflect that.
Common architectural styles
Layered architecture. The system is organised into layers, typically presentation (user interface), business logic and data access. Each layer uses the one below it. This style is familiar, easy to understand and suits many business applications.
Client-server. Clients, such as web browsers or mobile apps, request services from servers that hold data and business logic. Most web applications follow this model, often communicating through an application programming interface (API), a defined set of requests and responses that one system offers to others.
Monolith. The whole application is built and deployed as a single unit. Monoliths are simpler to develop, test and deploy at first, and are often the right choice for new products and small teams. A modular monolith keeps a single deployment but enforces clear boundaries between internal modules.
Microservices. The system is divided into many small, independently deployable services, each responsible for a specific business capability and often owning its own data. Microservices allow teams to develop, deploy and scale parts of the system independently. However, they add significant complexity: network communication, distributed data, monitoring across many services and more demanding operations.
Event-driven architecture. Components communicate by publishing and reacting to events, such as "order placed" or "payment received", rather than calling each other directly. This reduces coupling and suits systems where many parts need to react to the same change, though it can make the overall flow harder to follow and debug.
Serverless. Developers write functions that run on a cloud platform in response to events, without managing servers. This can reduce operational effort and cost for variable workloads, but can create dependence on a particular provider.
Comparing monoliths and microservices
| Consideration | Monolith | Microservices |
|---|---|---|
| Initial development | Simpler and faster | More setup and infrastructure needed |
| Deployment | Whole application deployed together | Services deployed independently |
| Scaling | Scale the whole application | Scale individual services as needed |
| Team independence | Teams share one codebase | Teams can own separate services |
| Operational complexity | Lower | Higher: networking, monitoring, data consistency |
Remember: no architectural style is best in every situation. Choose the simplest architecture that meets your key quality attributes, and be wary of adopting a fashionable style because other organisations use it.
Cross-cutting concerns
Some concerns affect every part of the architecture and should be considered from the start rather than added later. These include security (authentication, authorisation, protection of data), observability (logging, metrics and tracing so the system can be understood in operation), resilience (how the system behaves when parts fail) and data management (where data lives, how it is backed up and how privacy obligations are met).
Evolutionary architecture
Requirements and technology change, so architecture should be able to evolve. Many experienced engineers recommend starting with a well-structured, modular design and splitting out separate services only when there is a clear reason, such as a component with very different scaling needs or a team that needs to deploy independently. Clear module boundaries make such changes far easier later.
Common mistakes
- Adopting microservices for a small team or new product without the operational capacity to support them.
- Ignoring non-functional requirements until late in development.
- Letting a monolith grow without internal boundaries until every change is risky.
- Making major decisions without recording the reasons for them.
In practice
For a system you know, try to identify its main components and how they communicate. Is it a monolith, a set of services or something in between? Which quality attributes seem to have driven its design, and which are causing problems? If your organisation is considering a significant architectural change, ask what problem it solves, what new complexity it introduces and whether a simpler option would work.
2.3 Documenting and communicating design
A design that exists only in one person's head is fragile. When that person is unavailable or leaves, the knowledge goes with them, and others may unknowingly undermine the design. Effective teams document and communicate design decisions in ways that are useful, lightweight and kept up to date. The aim is not to produce large documents, but to create shared understanding.
Why documentation matters
Good design documentation helps new team members understand a system quickly, supports discussions about changes, records the reasons behind decisions and helps other teams integrate with the system. It is also often needed for security reviews, audits and regulatory purposes. Too little documentation leads to confusion and repeated mistakes; too much, especially if it becomes outdated, wastes time and misleads readers.
Diagrams at different levels
Diagrams are one of the most effective ways to communicate design, but a single diagram trying to show everything usually communicates nothing. A widely used approach, known as the C4 model, describes a system at four levels of detail:
- Context: the system as a single box, showing the people who use it and the other systems it interacts with. This is understandable by non-technical stakeholders.
- Containers: the main deployable parts, such as a web application, a mobile app, an API and a database, and how they communicate.
- Components: the major building blocks inside each container.
- Code: detailed structure, usually generated from the code itself when needed.
Most teams find the first two levels the most valuable. Each diagram should have a title, a key explaining shapes and lines, and labels describing what each connection does, such as "sends order details using a secure API".
Other useful diagrams
- Sequence diagrams show the order of interactions between components for a particular scenario, such as a customer placing an order.
- Data models show the main entities, such as customers, orders and products, and how they relate.
- Deployment diagrams show where software runs, for example which cloud services and environments are used.
The Unified Modeling Language (UML) provides standard notations for many diagram types. Some teams use it formally; many use simplified versions or informal boxes and arrows. Consistency and clarity matter more than strict notation.
Architecture decision records
Diagrams show what the design is; they rarely explain why. An architecture decision record (ADR) is a short document capturing one significant decision. A typical ADR includes:
- a title and date;
- the context: the problem and the forces at play;
- the decision made;
- the options considered and why they were rejected;
- the consequences, including trade-offs and risks accepted.
ADRs are usually stored alongside the code so they are easy to find. When a later decision replaces an earlier one, a new ADR is written and the old one is marked as superseded, preserving the history.
Tip: when you find yourself explaining the same design decision to a third person, write it down as a short decision record. It will save time and prevent the decision from being reopened without good reason.
Documenting interfaces
Where systems interact through APIs, clear interface documentation is essential. It should describe the available operations, the data expected and returned, error responses, authentication and any limits on usage. Many teams describe APIs using machine-readable specifications, from which human-readable documentation and test tools can be generated, helping to keep documentation accurate.
Design reviews
Before significant work begins, many teams hold a design review, in which colleagues examine a proposed design. Reviews catch problems early, spread knowledge and improve decisions. They work best when the proposal is shared in advance, the discussion focuses on risks and trade-offs rather than personal preferences, and outcomes are recorded.
Keeping documentation alive
Documentation decays if nobody owns it. Practical habits include keeping documents close to the code, updating them as part of the definition of done, reviewing key diagrams periodically and deleting documents that are no longer accurate.
In practice
Sketch a context diagram of a system you use: put the system in the centre, then add the people who use it and the other systems it connects to, labelling each connection. Then think of a significant decision your team has made recently, technical or otherwise, and write it up as a short decision record with context, decision, options and consequences. Both exercises take less than an hour and demonstrate how lightweight documentation can create lasting clarity.
Case study: Choosing an architecture at an online learning start-up
Tutorly, a fictional start-up offering online tutoring, had four developers and a deadline to launch within six months. One developer proposed building the platform as a set of microservices, citing large technology companies that used them.
The lead engineer organised a short design review. The team listed their main drivers: speed of delivery, reliability during peak exam periods and the ability to protect children's personal data. They had no dedicated operations staff and limited experience of running distributed systems.
They chose a modular monolith with clear internal boundaries for scheduling, payments, video sessions and user accounts. Video sessions would use an external specialist service through its API. The team drew context and container diagrams and wrote an architecture decision record explaining that microservices had been rejected for now because of operational complexity, but that the clear module boundaries would allow services to be separated later if needed.
Two years later, when scheduling demand grew sharply, the team extracted it as a separate service. The earlier decision record helped new engineers understand why the system was structured as it was.
Activity
Choose a system you know, such as an online shop, a booking service or an internal tool. Draw or describe a context diagram and a container diagram for it, labelling the main connections. Then write a short architecture decision record for one significant design choice, including at least two options considered.
Compare with a model answer
A good answer describes a clear context: for an online shop, customers, staff, a payment provider, a delivery company and an email service around the central system. The container level identifies parts such as a web front end, a mobile app, a back-end API, a database and a search service, with labelled connections such as "API sends payment requests to payment provider".
The decision record is concise and complete. For example, it might record the choice of an external payment provider rather than processing card details directly. The context explains security and compliance obligations, the options include building in-house payment handling and using a provider, and the consequences note reduced compliance burden in return for transaction fees and dependence on the provider.
Key takeaways
- Good design manages complexity through modules with high cohesion and low coupling, communicating through clear interfaces.
- Principles such as separation of concerns, DRY, KISS and YAGNI help keep code simple and maintainable, and readable code is a design concern.
- Architectural styles such as monoliths, microservices and event-driven systems each involve trade-offs, and the simplest option that meets key quality attributes is usually best.
- Security, observability, resilience and data management should be considered from the start.
- Layered diagrams, architecture decision records and design reviews create shared understanding without heavy documentation.
Unit check
Create a free account to answer the check, save your progress and receive your free certificate.
Create a free account Log in