Software Design Guidelines for .NET


Prerequisite:

Anyone who is related to the software development cycle can benefit from this article. However prior knowledge of Object Oriented Programming will help you understand many parts of the article as well as the programming samples.

Introduction

What is Good Software Design? Are there written Guidelines which clearly mention what is 'Good' design and 'Bad' design? Are there any tools which can analyse your source code or assembly, and highlight code which do not follow Good Design Guidelines?

Before we answer these questions, let us understand what Software Design means.
When computer software students do their college projects, the software development life cycle is usually as follows.

  1. The students team up in a group of 2-4 and select a project.

  2. The students do a bit of brainstorming and then start the public static void Main () followed by all the coding.

  3. Once the project is complete, flowcharts are drawn, comments are added to code and the source code printouts are collated to create a project manual.

It's so simple. As they say "If it ain't broke don't fix it". These students enter the software industry with the same mindset and approach each project in the same. Many of these students will be working on small to medium level project and may use the techniques they employed as students all through their working life.

However some will join large organizations, where software development teams are usually large. A typical project, I've worked on included 15 developers, 2 project leaders, 1 project manager and 3 testers. But teams as large as a 100 are not unheard of.

Types of Projects


With huge enterprise projects, only the foolish will follow the same college student approach. Well, you may ask, why not?

Consider a project scenario at home. If you are to build a dog house for your pet, you would most probably just go the local hardware store and purchase a few planks and nails. Get to work with a hammer and you have a dog house ready. As long as the client (your pet) is happy with it, and it doesn't have any obvious bugs (leaking roof), you would have done a good job.
Now suppose you are building a house for your family. Unless you have a very good divorce attorney, heaven help you if you approach the problem like you did with the dog house. You would more than likely discuss the needs of your wife and kids in detail. Identify the number of rooms along with the size and shape. Draw a software blueprint, not only for yourself but also the local fire department and housing authorities, who may have specific requirements. Then you decide on the budget and timelines, in consultation with the other stake holders (wife and kids). Only then will you attempt to build your house
And finally consider the And finally consider the building of a 50 storey sky scraper. You will be possibly working with a large team of professionals. You will review hundreds of architectural designs. Structural engineers would be involved in integrating earthquake resistant technologies and fire proofing materials into the design.


When every other industry places so much emphasis on design, the software industry woke up pretty late to the benefits of Good Software Design.

The Software Industry wakes up

With the creation of an Object Oriented Programming language like C++, developers and designers were suddenly given this great tool for designing and writing code which was encapsulated, reusable and maintainable.

Object-Oriented techniques typically show relationships and interactions between classes and objects. However C++ does not describe the best way for two classes to interact with each other. Also they do not specify the final application classes or objects. Given a problem statement what's the best solution for it.

In October 1994, the book Design Patterns: Elements of Reusable Object-Oriented Software, by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides (Gang of Four) was published. This book brought software design out into the open, and companies began adopting them in the software development life cycle.

What are the Design Pattern?

A software project module can be broken down into many smaller modules until each module do not need more than 10 classes to make it work. Now if you come across one of these problem statements, how will you approach it? Brainstorm, create class diagrams and come up with a design which you think will get the job done. However do you think you have created the world's best design or could there be a better design to the same problem? Possibly, there could be unless you yourself have 10+ years of software designing experience.

There are many such problem statements which have been solved before using different designs by many different programmers/designers. Over the years, some of these designs were found to be better than others.

Design Patterns documents these commonly occurring problem statements and describes a general solution to the problem. The key thing to remember here is general solution to the problem. This means Design Patterns is not a readymade design which can be directly transformed to code. It is just a description or template of the solution. The Gang of Four defined Design Patterns as follows.

A design pattern is a general reusable solution to a commonly occurring problem in software design.

You can read more about design patterns at
http://en.wikipedia.org/wiki/Design_pattern_(computer_science)

You can also read about the book by the Gang of Four at
http://en.wikipedia.org/wiki/Design_Patterns


Microsoft .NET Framework design guidelines

Another small but significant aspect to designing and writing good software programs, is the adherence to standards and naming conventions.

In the good old days, large software companies had their own standards for software programming including variable naming conventions.

Why have these standards and naming conventions at all?

  1. They reduce the effort needed to read and understand source code. All stake holders who follow the same standard can easily understand code written by another programmer.
  2. They enhance source code appearance.
  3. Many certifications (for example Microsoft Certified Software), requires your code to follow certain standards.

Microsoft introduced the .NET Framework design guidelines to help companies converge to one standard, rather than maintain their own proprietary standards. This not only helps reduce the burden on programmers, but companies find it easier to move people between projects and also integrate new employees faster into a project team.

You can read about all the .NET Framework design guidelines at
http://msdn.microsoft.com/en-us/library/ms229042.aspx
. I love reading all this stuff but then, being lazy programmers, I know most of us would rather automate this.

Microsoft has released, FxCop which is a free static code analysis tool for .NET. It checks managed code assemblies for adherence to the Microsoft .NET Framework Design Guidelines. Since FxCop analysis the compiled assemblies (dll), i.e. MSIL, it works with any of the .NET languages includes C# and Vb.Net.

 

Installing and Configuring FxCop

  1. After installing it, start FxCop from the Start Menu or the Desktop icon to see the following screen.



  2. I am using a C# Console Application project called ConsoleApplication1 and a C# Class Library project called ClassLibrary1 to demonstrate FxCop. In the ConsoleApplication1 project, add reference to the ClassLibrary1 Project.

    ConsoleApplication1 has a single class Program.cs with the following code.

    using ClassLibrary1;

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Class1 obj = new Class1(28, 9500, "John");
                obj.Show();
            }
        }
    }

    ClassLibrary1 has a single class Class1.cs with the following code.

    using System;
    namespace ClassLibrary1
    {
        public class Class1
        {
            private int Age, _sal;
            public string Name;
            //Constructor
            public Class1(int Age, int Salary, string Name)
            {
                this.Age = Age;
                this._sal = Salary;
                this.Name = Name;
            }
            public void Show()
            {
                Console.WriteLine("Name={0}, Age={1}, Sal={2}", Name, Age, sal);
            }
        }
    }

  3. Create a new FxCop Project and save it in the same folder as the solution whose assemblies you want to analyse. Also name the project file as follows. In our example the solution is ConsoleApplication1.sln so I named the FxCop project ConsoleApplication1.sln.FxCop. (Note: This step is not a requirement but doing this will help you integrate FxCop with Visual Studio 2008.)

  4. Right click on the Targets section and select the menu option [Add Targets]. Select ClassLibrary1.dll from either the debug or release folders inside ClassLibrary1\bin. The FxCop window will look as follows.



  5. Press F5 or Click on the Analyse Tool Button to display the analysis report as follows. Clicking on the analysis entry to the right, will display details in the properties section at the bottom of the window. Click on the location link to open the offending line within visual studio.

Conclusion

Developers who implement 'Good Software Design' in their day to day programming activities:

  • Earn better ratings during appraisals
  • Get bigger bonuses, and,
  • Their resume looks so much more impressive.
Something which entry level developers should understand is that it takes many projects to understand and appreciate the need and benefits of Good Software Design. And it may take years for developers to create great Software Design solutions.

So hang in there. Every day is a learning experience.