Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I would have two questions related to cyclomatic complexity:

  1. Can I use this metric for a whole app? I guess I cannot, as it would be incredibly large number, considering functions calling functions etc. Is it just for module/method/unit?

  2. In Visual Studio, for starting app with just calling the Main method, the cyclomatic complexity value is 2. Why? There is just one possible way if I run the program - it ends.

share|improve this question

1 Answer 1

up vote 10 down vote accepted

The cyclomatic complexity of the most basic console application is 2 for a simple reason: aside the Main() method, there is also a constructor. It's like writing:

public class Program
{
    public Program()
    {
    }

    public static void Main()
    {
    }
}
  • The first path is to create a new instance of the Program class. This path is taken by default to launch the application.

  • The second path is to call the static method Program.Main(). This path can be taken if the application is used as an ordinary library by another application.

Note that this applies to any non-static class having a static method. For example:

public class Hello
{
    // Even if the constructor was omitted, it will still be added by the compiler.
    public static void SayHello()
    {
    }
}

will have a cyclomatic complexity of 2 as well.

Also note that if you really want to have an application with a cyclomatic complexity of 1, you simply declare Program class as static, as in the following piece of code. I have no idea why the default template in Visual Studio forgets to add both public and static keywords.

public static class Program // ← See, we replaced a non-static class by a static one
{
    static void Main(string[] args)
    {
    }
}

As for your first question, you don't use cyclomatic complexity as the sole factor. Maintainability index, with its quite tricky formula, is a better metric, but even maintainability index is not something you can fully trust and rely on to say that a piece of code is "good" or "bad".

It's not disturbing to have a cyclomatic complexity of a few thousands for a small project, and it doesn't make too much sense to use it for class, assembly or project scope; what would be much more disturbing is to obtain a high cyclomatic complexity for a specific method.

share|improve this answer
    
Thanks, I did not realize that (as for the point 2). For the first one, also CC calculation is useful really only for modules or algoritms, correct? –  user970696 Jan 10 '13 at 7:58
2  
@user970696: it's useful for methods (including getters and setters of properties). If you have a method with a cyclomatic complexity of 5 000, this is a hint that something might be totally wrong with this method. –  MainMa Jan 10 '13 at 8:06
    
Also, the cyclomatic complexity gives you a lower bound on the number of test cases you need to achieve 100% C1 code coverage. In @MainMa's example, you need at least 5000 non-overlapping asserts to achieve 100% C1 coverage. (The same applies to NPath complexity and C2 coverage.) –  Jörg W Mittag Jan 10 '13 at 9:43
2  
Of course, this answer has "everything must be a member function" glasses. C / C++, for instance, don't have this behavior. –  Billy ONeal Jan 10 '13 at 13:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.