At Øredev in November 2008, I got the opportunity to listen to Robert C. Martin, also known as Uncle Bob. I have been a fan of his work for many years ever since I read his book Agile Software Development Principles, Patterns, and Practices. A few years later when the C# version came out I took the opportunity to reread many of the chapters. To me, that book became a eye opener to a new way of working with code. And I think I am not alone. For instance, it seems to me like much of the ALT.NET movement gets a lot of inspiration from topics found in this book. Like for instance the excellent description of the 6 design principles, now commonly referred to as S.O.L.I.D.
Anyways, in 2008, Robert Martin came out with a completly new book called Clean Code: A Handbook of Agile Software Craftsmanship. Naturally, Clean code was his chosen topic at Øredev for both his keynote and the session on Functions (chapter 3). Robert Martin proved to be a passionate speaker so I was not disappointed. Neither have I been disappointed in this new book so far. Since Øredev I have read about half the book and I truly feel that it is a gold mine to read for any professional developer.
But I kind of stopped reading a couple of weeks ago since I had this great idea. But before I tell you what, I’ll tell you why. That’s usually a good start.
The big WHY
The concept of “Clean” is often defined per developer
This is a bit unfortunate but my experience is that we developers all have different opinion as to what clean code really is. We all get our little habits and our own way to keep our code clean and readable to ourselves. It happens that we shun the sight of other people’s code since it simply is not clean. As in our definition of clean that is.
If only, we could keep our noses glued solemnly into our own written code. Unfortunatly, this is not so.
The common code base
Professional programmers usually don’t work isolated from other professional programmers (although it happens). Most often you share a code base with others and you will have to dig into and work with other peoples code. This is usually where a developers' nose start sneering since, "no way, that code is clean!"
Imagine this sneering nose developers name being Robert. So what does Robert do when confronted with such ugly code? Well, he would probably sneak in a bit of refactoring of the code with the sole purpose of making it clean. That’s ok and quite normal for any programmer to do. So it should be ok.
The only problem is that when Joe, who originally wrote the code, wants to work again with the submitted, now clean, code, he finds the code to be … unclean.
What does Joe do? Well, you guessed right: refactor a bit back to “clean” state. With a big grin of proudness, Joe will resubmit the now clean code into the code repository.
Later when Robert comes back to this code, what does she do? Well perhaps refact............
Verbal Communication
Naturally, we developers aren’t idiots (although it may perhaps seem so occasionally to non-devs.) and we do have a mouth for other purposes than to shovel food and drinks into it. So in the end, Robert, or Joe, will walk over and talk to eachother. This is where the "big discussion" may start. What is clean and what is not clean code.
It may sound like it would be a heated and most dreadful debate. But I think most developers actually like discussing these things. As long as you are able to convince the other guy of how beautiful and logical your clean code is. However, most of the time, this agreement does not occur and the constant battle of Clean Code carries on.
In the end, developers may stay away from other peoples code since it is tainted by uncleanness.
The Contract
Naturally, the previous description is not a successful situation to have at a company. The code base must be shared for lots of reasons; work scalability and fresh influences to name a few. So many companies bring out code policies, written in a document for all developers to see and sign on. In blood. Code inspection tools, like FxCop, will run at night and report to everyone in the morning if someone has violated the signed contract of Clean Code.
Naturally, the contract has got to come from somewhere. There are a couple of different ways
A) The company chief architect(s) brings forth the document for all developers to follow.
B) All developers join in big battle lasting for days and come up with a final agreement.
C) The company borrows someone else’s contract.
Naturally, C) is the most common option. But there will always be room for B) and A) since there are domain specific stuff that still can be agreed upon.
The idea – the WHAT
At Admeta, where I work, we are using the Scrum project methodology and we are fairly agile in the way we work. We are using model C) as in the Microsoft conventions and guidelines and we also have a wiki page with further conventions agreed upon, i.e. B). We do not believe in the A) alternative.
So naturally when I began reading the book, it became clear to me that I would be doing an awful lot of referencing to the book when discussing clean code with my collegues. Very soon I thought, my colleagues would get very tired of both me and Uncle Bobs opinions (although they all heard him speak at Öredev) and tell me to put a sock into my mouth... What a dreadful scenario!!! So naturally, I was forced to get an idea. And thus it came to me one beautiful day:
The idea: So why not try to make everyone read the book and we will have a discussion for each chapter? I have previously been participating in a group doing exactly this at Dotway with the GOF Design Pattern book. To me Clean Code seems like a perfect topic and we could probably gain a lot as a company and individuals if we spend some time doing this together.
The idea – the HOW
Said and done. I talked to my colleagues and every developer on the company has now joined. Everyone has got an own copy of the book (it’s actually very inexpensive) and we get an hour once a week on company time to do discussions. Reading is done on spare time, but hey: it is fun reading! Besides, each chapter is quite small and would take little time to read.
So this Thursday, we’ll have our first Clean Code discussion meeting. As a preparation, the meeting attendees will have to read Chapter 1 of the Clean Code book. I have also given them the following questions to reflect upon:
- Before reading, take a moment to reflect what the concept of Clean Code means to you.
- Is there anyone of the different "guru's" description of Clean Code (p7-12 + forword) that do you think lies closest to your own definition of Clean Code?
- After having read the chapter, has the reading changed your conception?
- What is your experience / opinion of code generation tools or any other higher abstraction level of programming (4GL)?
- Do you have any good or bad experience of building your own code generator (in some way)?
- Where do you think we are heading when it comes to abstraction levels in languages and tools?
- Do you consider this to be a bright future for us developers?
- extra: What do you think of Joel Spolskys described "Law of Leaky Abstractions"?
- Have you experienced something like "the Grand Redesign in the Sky" and how did that end?
- What is / has been your explanation to having written bad code (as we all have done!) in the past?
- Do you think it is suitable for us as a team to align to a "School Of Thoughts" (p12) or do you see any better path to having a more uniform clean code conception?
- What do you think of "the Boy Scout Rule" (p.14) applied to code?
- Are you familiar with any or all following Principles of Design (S.O.L.I.D.) (p15):
- SRP - The Single Responsibility Principle
- OCP - The Open / Closed Principle
- LSP - The Liskov Substitution Principle
- ISP - The Interface Segration Principle
- DIP - The Dependency-Inversion Principle
- What do you think is the status of Clean Code at our company?
I think these questions will be more than enough to discuss for our meeting so we will probably end up choosing the most interesting ones to discuss. I am truly looking forward to it.
Btw, I'll be continuously posting my questions for each chapter on this blog. So stay tuned if you are interested.