SOLID Principles: Summary

An extrmely simple summary of the SOLID Principles:

Introduction

As systems scale, the SOLID Principles become more important.

The Single Responsibility Principle

This is to do with the design of individual models. It says that a modules (classes) should have one and only one role to play (ie. it should have only one reason to change).

The Open-Closed Principle

You should be able to extend a module, but changes to a module shouldn’t effect existing clients.

The Liskov Substitution Principle

Best explained by example:

If, in our program, we define a lion as a kind of cat, then everything that could reasonably be said about a cat can also be said about a lion. In other words, we can treat a lion as if it was a cat… because it is one.

This is rarely violated in in code that I have seen, but has been the source of subtle and unpleasant issues when it has.

The Interface Segragation Principle

This principle is about the way that modules interact with other modules. It says that a module shouldn’t tell everyone everything about itself, but only the things that they need to know.

The Dependency Inversion Principle

The Open-Closed Principle (OCP)

A basic explaination of the Open-Closed Principle of software design (one of the five SOLID principles).

Confusion About the Open-Closed Principle

There appears to be some confusion about what it actually is, and how it should be applied:

There appear to be several different interpretation of the principle – some only subtly distinct, others seemingly almost unrelated.Jon Skeet, C# Guru

Why is this principle especially hard to understand?

The principle was devised by B. Meyer and popularized by Robert C. Martin (AKA Uncle Bob). Sadly, Meyer’s text isn’t widely available, and Martin’s explanations are less crisp as his other writing.
The principle is often summarized like this:

Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.Bertrand Meyer, quoted on Wikipedia

But this doesn’t help unless we know exactly what is meant by “open”, “closed”, “extension” and “modification”.

The Worst Definition

Based on poor explanation of their ideas, some people seem to think it means:

A class should no change (except to fix bugs), and only inheritance can be used to extend it.

But this definition is misleading in that it appears to misrepresent what Meyer and Martin had in mind. Moreover, in practice it appears to be devastatingly restrictive and, consequentially, downright unhelpful.

A Working Definition

As far as I can tell, the basic idea of the Open Closed Principle is that:

You should be able to extend a module, but changes to a module shouldn’t effect existing clients.

In other words, you create a stable interface – a contract – between modules.
Based on this definition, we can revisit Meyer’s definition. So, by “open for extension” we mean that new functions can added. By “closed to modification”, we mean that those new features won’t modify existing functionality.

Practical Outcomes

Some of the implications of this are that:

  • No existing functions can be removed, renamed, have their signatures changed, because then all their clients will break.
  • You shouldn’t add a new abstract methods in a class, because then all its descendants will break.
  • You can extend a class using, for example, the strategy pattern, inheritance, or by adding new methods.

Reality Check

No software design can ever be completely consistent with this principle and coninue to accommodate changes. The principle is aspirational rather than prescriptive. By their nature, design decisions invariably leave some avenues of extension “closed” and others “open”. This implies that we are required to make best-guesses about future changes based on what we know today. In the words of Kent Beck, “there are no permanently closed abstractions”.

Cohesion, Coupling and Conceptual Integrity

Good design involves High Cohesion, Low Coupling and Conceptual Integrity.

Michelangelo divided his mural into panels: separate, free-standing areas, each of which tells a story.Andrew Hunt and David Thomas, The Art in Computer Programming, 2001

High Cohesion

Each part (class, function, service, tool etc.) of a system should do one thing, and do it well.

Low Coupling

Unrelated things should remain self-contained.

Conceptual Integrity

Together, all parts of the system tell the same story.

Bad Things about Good Design

Good design comes at a price, and it is important to understand these costs if we seek to build and maintain well designed code.

What are the Costs?

Some examples:

Up-Front Design Costs

Elegant code usually takes longer to create than spaghetti code.

Refactoring Costs

As time goes by, good designs tend to rot. It can take considerable effort to maintain good design.

The counter to this is that poorly designed  code becomes increasingly difficult to maintain, reducing its value and the value that can be added to it.

Increased Cost of Grokking a Code Base

The smaller your modules, the more there are and the higher your levels of abstraction. But more layers in your code mean that new developers will have a harder time understanding your code base.

In addition, a good design often maps onto a business domain. As a result, a developer may need deep domain knowledge in order to understand the reasoning behind many of the design decisions t

hat have been taken. It takes time and effort (ie. investment) for developers to gain this knowledge.Of course, developers are generally happier working on clean, well designed code, and happy are less likely to change jobs. Nevertheless, this can be a considerable cost to some organisations.

Higher Levels of Abstraction Lead to Slower Code

In general, the more modules you have, the higher the cost of those modules communicating with each other, the slower your code will run. Big balls of mud are often fast.

Of course, not all code needs to be especially fast. Moreover, well designed code is easier to optimize. Furthermore, it can be cheaper to add better hardware than reduce developer productivity: developer time is usually more expensive than CPU cycles.

Finally, good design often does involve more abstraction… but not always.

Better Design Results in More Lines of Code

Many languages require significant amounts of “boiler plate” (standard code) in the header and footer of each module, typically indicating such things as the module’s scope, namespace and name.

Better design often calls for more descriptive names, which are often longer than poorly thought through names.

It is arguable that “lines of code” is a poor measure of developer productivity, but even so, each additional character is a codebase has a cost associated with it.

Is Good Design Worth the Cost?

In my view, not always. I believe that the purpose of design is to maximise value delivered by a solution over the lifetime of a problem. So, if a solution is only intended to be used once, never changed, used by a single user and very short and simple then there is little to be gained by putting too much time into making its code base perfect. On the other hand, if a solution is intended to be with an organisation for any length of time, then overall costs can be reduced if the principles of good design are applied.

A common principle of good design is to avoid repetition, and this often involves increasing abstraction. I have often witnessed a pattern amongst developers, according to their experience:

  • Novice developers apply little abstraction
  • More experienced tend towards over-abstraction (using, for example, all the design patterns they can think of on every project)
  • The true expert will apply appropriate, pragmatic levels of abstraction.

STUPID Principles

Software design improves whe you avoid doing the wrong thing.

What are the STUPID Principles?

Poor Object Oriented design is achieved when people follow the S.T.U.P.I.D. principles of software design:

(S)tuff that is Global

For example, global variables, static methods, and singletons.
The problems here include:

  • Uncontrolled access to global entities
  • Tight coupling throughout the code base
  • Broken encapsulation
  • Extension is often hard
  • Changes can effect everything

(T)ight Coupling

In other words, high levels of interdependency between modules. Tight coupling is often caused by dependency on specifics of an implementation.

(U)nclear Naming

Often a symptom of not knowing what a module is actually for.

(P)remature Optimization

Sacrificing code clarity (not to say developer time) to making things faster… even if nobody cares if they’re slow.

(I)nvisible Dependencies

When, for example, using a class means that you have to take a whole lot of other classes along for the ride… even if you don’t actually need them.

(D)uplication and Redundancy

For example:

  • Over-generalization (I think eveyone does this)
  • Commented-out code
  • Repetition and duplication and other types of repetition
  • Functionality that the user doesn’t value

 

When Good Code Goes Bad

Most systems start off with a nice clean design, but over time software starts to rot.

Does Design Matter?

  • Software design is about managing the complexity of a system. Unmanaged complexity leads to difficulty in making changes to the system, which is important because all software development is change.
  • Poor design leads to lower quality systems, less functionality to clients, higher costs (for everyone), loss of business and misery for developers.

Why Do People Tolerate Poor Design?

  • Business pressures:
    • Developers are often encourages to working software fast, so “quick and dirty” solutions go into production
  • Lack of knowledge:
    • Introductory courses / texts tend to focus on syntax rather than design
    • People learn from what they see, and there are a lot of poorly designed systems to learn from
  • Scale effects:
    • Larger systems benefit from good design more than smaller systems
    • It takes more effort to design larger systems well
    • Fewer developers have experience developing larger systems

What are Design Principles?

Design principles help us to understand the “rules” about the best way to manage complexity, and therefore maintiain and increase the value that is delivered by our code. They are best viewed as guidelines. They shouldn’t be followed blindly, as there are costs involved in using them.

What is Design Rot?

Most systems start of with a nice clean design, but over time, “software starts to rot”. As “Uncle Bob” Martin says:

At first it isn’t so bad. An ugly wart here, a clumsy hack there, but the beauty of the design still shows through. Yet, over time as the rotting continues, the ugly festering sores and boils accumulate until they dominate the design of the application. The program becomes a festering mass of code that the developers find increasingly hard to maintain.Robert C Martin, Clean Code

What are the Symptoms of Design Rot?

Martin identifies 4 symptoms of rot:

  1. Rigidity: Software becomes difficult to change, and changes cause other changes in dependent modules. Simple changes become expensive, developers become fearful, unknowns increase.
  2. Fragility: The tendency of the software to break in many places every time it is changed, sometimes in areas that have no conceptual relationship with the area that was changed. Fixes introduce new bugs. Developer credibility is lost.
  3. Immobility: The inability to reuse software from other projects or from parts of the same project. Drawing on existing modules is impossible because of they bring in too many dependencies.
  4. Viscosity: Design Viscosity occurs when the design-preserving approaches are harder to use than hacks, i.e. it is easier to do the wrong thing than the right thing. Environmental Viscosity occurs when the development environment is slow and inefficient, leading to the temptation to take short cuts.

These issues tend to be more of a problem as the size of a software base increases.

What are the Causes of Design Rot?

Each of these symptoms is mainly caused by improper dependencies between the modules of the software. Therefore, managing dependencies between modules is at the core of good design. This applies at several levels: framework, library, package, class and method.

Where Does it Happen

At all levels of abstraction:

  • Function / method level
  • Class / module level
  • Package level
  • API level
  • In the interfaces between systems

How Do We Avoid Design Rot?

Some of the general principles are:

  • Value design and pay attention to it
  • Iteratively improve design
  • Improve design at each layer of abstraction
  • Desgin modules that are small and focused (high cohesion)
  • Reduce dependencies between modules (low coupling)
  • Remove redundancy and repetition
  • Keep learning about design and architecture

Troubleshooting

These general steps can be used to troubleshoot anything.

Step 1: Ask Questions to Understand the Presenting Problem

  • Who is effected?
  • What happens?
  • What should happen?
  • Has it ever worked?
  • Where does it happen?
  • Does it happen for everyone?
  • When does it happen?
  • What were you doing?
  • Has anything changed?

Step 2: Gather Evidence

Seek out information that will help you investigate and to support your understanding.
From, for example:

  • Other reports
  • Logs
  • Attempts to reproduce the issue

Step 3: Formulate a Theory

Step 4: Test to Disprove

It is important to attempt to disprove to overcome “Confirmation Bias”.

Step 5: Provide Recommendations for Further Investigation

Based on understanding of impact vs cause.

Sources of Data on IT Operations Performance

Data that tells you what is going on in your IT infrastructure comes from the following sources:

  • Wire Data (network packets)
  • Machine Data (e.g. CPU usage for workstations and servers)
  • Agent Data (that tell you current number of users, which applications are used when etc.)
  • Synthetic Data (data that you check as required, e.g. “is it available now”)
  • Human Generated Data (e.g. knowledge bases)

It is essential to gather data, so decisions are made based on information rather than guesses.

Solved: Missing Target Framwork in Visual Studio

Here is how I fixed my missing .Net target framework 4.0 issue in VS2010.

The Symptoms

Visual Studio 2010 stopped detecting the .Net framework 4.0. Specifically, this framework version was missing from the “Target framework” drop-down on a project’s property page. Versions 2.0, 3.0 and 3.5 were present in the list, but not 4.0. Reinstalling the framework and rebooting had no effect.

The Solution

The problem was that the file,”FrameworkList.xml” was missing from folder “C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\RedistList“. Copying this file from another machine solved the problem.

I am now able to target .Net 4.0 again:

Further Reading

  • I found this solution at the end of this discussion.
  • There is also a stackoverflow question dedicated to this issue.

Solved: BizTalk Map Renaming Fails in Deployed Application

BizTalk map renaming isn’t quite as simple as renaming the map.

The Problem

I renamed a BizTalk map in VisualStudio’s Solution Explorer, compiled and deployed the application. However, in the BizTalk Management Console, the renamed map still shows up with its old name.

The Solution

This is one of those BizTalk things that we’ve all been caught out by at least once.
The solution is to right-click the map name in Solution Explorer and click properties. In the properties window, change the name of the Type to the name of the map. Then redeploy.
BizTalk Map Renaming