Indentation as a Complexity Metric
If you want to measure the complexity of your software, there is a lot of software, tools and software-as-a-service offerings available. These options can seem daunting and have a lot of onboarding time (the time it takes you to understand how to use them and get meaningful results).
If none of these things work for you, don’t despair. There is a simple way to get a high-level view on the complexity of your software. And it’s language-agnostic. It doesn’t care whether you write CSS, Ruby, Java or something else.
You see, when developers structure their code, they use indentation as a seperation. Some languages/developers use tabs and other spaces. I personally like to use 2 spaces for one indentation step. Others use 4 spaces (that’s common in JavaScript as far as I know). In the end it doesn’t matter, because every indentation increases the complexity by one.
Like this:
def my_function () {
if (this_is_true) {
do_a_thing()
else if (another_thing_is_true) {
if (one_more_check?) {
// we are really far into the rabbit hole right now
do_a_different_thing()
}
}
}
If you count the indentation for this pseudo-example-code (a heavy mix of Ruby and JavaScript, just to make you not try to read the code but understand the concept), you see that the biggest indentation is already at three. I adhere to the rule to never indent SCSS files more than three times. And that might already be too much. SCSS-lint counts these indentations and reports them as errors. You could use the same metric for any other language.
If you or your developers indent code more than 2 levels, there might be a problem lurking that you should investigate.
This check can probably by done by non-developers as well. You just have to trust your eyes and be able to count to 3. Or 4? 8? You can google “Pyramid of doom” and will get a nice view how this can look in its worst case.
Have fun counting indentation!
Yours, Holger