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.


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.

  • 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?
    – John V
    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. 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.) Jan 10 '13 at 9:43
  • 3
    Of course, this answer has "everything must be a member function" glasses. C / C++, for instance, don't have this behavior. Jan 10 '13 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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