How to assess complexity of your software

No Silver Bullet — Essence and Accidents of Software Engineering
Fred Brooks

This is one of the famous quotes by Turing Award-winning Fred Brooks. The complexity of software is tough to define. It is a measure of your program which is partially about the size and partially about understandability (clarity) of the code. A complex code does hinder on the habitability of your code.

Accidental vs. Essential

Fred Brooks, the author of the paper entitled "No Silver Bullet — Essence and Accidents of Software Engineering", distinguishes between two types of complexities: accidental and essential.

As the name itself suggest accidental complexity is about pieces of your program that are largely avoidable. For example: if an entity can retain its identity without some attributes than those attributes result in accidental complexity. It may be possible to abstract some of them into an abstract class, but do avoid leaky abstractions.

Great technique for getting rid of accidental complexity is checking whether something hasn't been already done in this area i.e. reuse software. Brooks also pointed out right language makes a heck of a difference i.e. if you can use Java, do Java and not VB6 or C++ (where you have to worry about deallocating memory etc.)

Essential complexity is the necessary things which are needed to solve the problem. It usually arises due to unknowns and uncertainties. These can be addressed by rapid prototyping.

Measuring Complexity

Cyclomatic Complexity

Cyclomatic Complexity counts the number of independent paths in (or part of) your program. ABC method can be used to approximate complexity:

  1. Assignment
  2. Branches
  3. Conditions

In a class, method or the entire program, count the number of assignments, branches and conditions and use \mbox{abc} = \sqrt{a^2 + b^2 + c^2}

Getting Simple

There are few rules of thumb to help get to simply the software:

  1. Favour shorter methods
  2. Favour functional style to loops
  3. Favour method calls to conditions
  4. If reusing parts of the code, check if creating a new abstraction is a suitable approach to reduce complexity and duplication