Adam Cogan did a great Web TV interview with Marcel de Vries and myself on the topics of architecture and code quality. It was real fun participating in this session. Although we know each other from the MVP ALM community, Marcel, Adam and I haven’t worked together before. It was very interesting to see how we agreed on so many terms, and how alike we where thinking. The basics of ensuring you have a good architecture and how you could document it is one thing. Also, the same agreement on the importance of having a high quality code base, and how we used the Visual Studio 2012 tools, and some others (NDepend for example) to measure and ensure that the code quality was where it should be. As the tools, methods and thinking popped up during the interview it was a lot of “Hey ! I do that too!”.

The tools are not only for “after the fact” work, but we use them during the coding. That way the tools becomes an integrated part of our coding work, and helps us to find issues we may have overlooked.

The video has a bunch of call outs, pinpointing important things to remember. These are also listed on the corresponding web page. I haven’t seen that touch before, but really liked this way of doing it – it makes it much easier to spot the highlights.

Titus Maclaren and Raj Dhatt from SSW have done a terrific job producing this video. And thanks to Lei Xu for doing the camera and recording job. Thanks guys !

The highlights points out some important practices. I’ll elaborate on a few of them here:

Add instructions on how to compile the solution. You do this by adding a text file with instructions to the solution, and keep it under source control. These instructions should contain what is needed on top of a standard install of Visual Studio. I do a lot of code reviews, and more often that not, I am not even able to compile the program, because they have used some tool or library that needs to be installed. The same applies to any new developer who enters into the team, so do this to increase your productivity when the team changes, or a team member switches computer.

Don’t forget to document what you have to configure on the computer, the IIS being a common one.

The more automatic you can do this, the better. Use NuGet to get down libraries.

When the text document gets more than say, half a page, with a bunch of different things to do, convert it into a powershell script instead.

The metrics warning levels. These are very conservatively set by Microsoft. You rarely see anything but green, and besides, you should have color scales for each of the metrics. I have a blog post describing a more appropriate set of levels, based on both research work and industry “best practices”. The essential limits are:

Cyclomatic complexity and coupling: Higher numbers are worse

On method levels:

Green : From 0 to 10

Yellow: From 10 to 20 (some say 15). Acceptable, but have a look to see if there is something unneeded here.

Red: From 20 to 40: Action required, get these down.

Bleeding Red: Above 40 This is the real red alert. Immediate action! (My invention, as people have asked what do I do when I have cyclomatic complexity of 150. The only answer I could think of was: RUN! )

Maintainability index: Lower numbers are worse, scale from 0 to 100.

On method levels:

Green: 60 to 100

Yellow: 40 – 60. You will always have methods here too, accept the higher ones, take a look at those who are down to the lower limit. Check up against the other metrics.)

Red: 20 – 40: Action required, fix these.

Bleeding red: Below 20. Immediate action required.

When doing metrics analysis, you should leave the generated code out. You do this by adding attributes, unfortunately Microsoft has “forgotten” to add these to all their stuff, so you might have to add them to some of the code. It most cases it can be done so that it is not overwritten by a new round of code generation. Take a look a my blog post here for details on how to do that.

Class level metrics might also be useful, at least for coupling and maintenance. But it is much more difficult to set any fixed limits on those. Any metric aggregations on higher level tend to be pretty useless, as the number of methods vary pretty much, and there are little science on what number of methods can be regarded as good or bad. NDepend have a recommendation, but they say it may vary too. And in these days of data binding, the number might be pretty high, as properties counts as methods. However, if you take the worst case situations, classes with more than 20 methods are suspicious, and coupling and cyclomatic complexity go red above 20, so any classes with more than 20x20 = 400 for these measures should be checked over.

In the video we mention the SOLID principles, coined by “Uncle Bob” (Richard Martin). One of them, the Dependency Inversion principle we discuss in the video. It is important to note that this principle is NOT on whether you should use a Dependency Inversion Container or not, it is about how you design the interfaces and interactions between your classes. The Dependency Inversion Container is just one technique which is based on this principle, but which main purpose is to isolate things you would like to change at runtime, for example if you implement a plug in architecture. Overuse of a Dependency Inversion Container is however, NOT a good thing. It should be used for a purpose and not as a general DI solution. The general DI solution and thinking however is useful far beyond the DIC. You should always “program to an abstraction”, and not to the concreteness.

We also talk a bit about the GRASP patterns, a term coined by Craig Larman in his book Applying UML and design patterns. GRASP patterns stand for General Responsibility Assignment Software Patterns and describe fundamental principles of object design and responsibility assignment. What I find great with these patterns is that they is another way to focus on the responsibility of a class. One of the things I most often found that is broken in software designs, is that the class lack responsibility, and as a result there are a lot of classes mucking around in the internals of the other classes.

We also discuss the term “Code Smells”. This term was invented by Kent Beck and Martin Fowler when they worked with Fowler’s “Refactoring” book. A code smell is a set of “bad” coding practices, which are the drivers behind a corresponding set of refactorings. Here is a good list of the smells, and their corresponding refactor patterns. See also this.