Press "Enter" to skip to content

The Quick Quit pattern

In these days of automated builds, automated testing, automated deployment, etc we tend to forget the importance of a good code review strategy. All this automation could easily mislead to the idea that you are in control. When those green balls appear, you get a smile upon your face, lean back and just watch them a few moments.

That is, until you get back to reality and have to fix a bug in a piece of code that no one dares to touch. Because, let’s face it, all this automation didn’t help us at all to write good code. And at the end of the day, that is what matters most. When you start thinking about writing good code, the first thing you have to answer is; what do we define as “good” in this matter. You could write (and a lot did) books full of this definition, but that’s not what I want to talk about today.

I want to take one piece of this definition and explain how some simple adjustments can improve your code. The thing I want to talk about is “readability”. Each line of code you write will be read tenfold (or more). So it is important that your code is easy to read. In terms of readability the “indentation level” is very important. If the level get’s too high, your code will be hard to read. Take this piece of code (ahhh finally, some code):


private bool _isUserDefined = false;

public void CalculateDifference()
{
	Console.WriteLine("Do you want to ceil or floor?");
	var input = Console.ReadLine();
	if (!string.IsNullOrEmpty(input))
	{
		if (_isUserDefined)
		{
			foreach (var calcParam in calculationParameters)
			{
				// do the calculation
			}
		}
	}
}

It might be difficult to understand what conditions should be set to actually execute the calculation. When you start reading this code and finally arrive at line 13, your brain kept all the if statement in “memory”. You need that info because you will ask yourself “when will this code execute?” the whole time. Now let’s compare this with the next code sample:


private bool _isUserDefined = false;

public void CalculateDifference()
{
	Console.WriteLine("Do you want to ceil or floor?");
	var input = Console.ReadLine();
	if (string.IsNullOrEmpty(input))
	{
		return;
	}
	
	if (!_isUserDefined)
	{
		return;
	}

	foreach (var calcParam in calculationParameters)
	{
		// do the calculation
	}
}

Way easier to read right?

There are a lot of articles on this topic on the internet, but I didn’t find any name for this pattern. That’s why I came up with my own. I refer to this as the “Quick Quit” pattern :). If it already has a name, I’d love to hear it in the comments.