Code minimalism: what is it and should you use it?

Code minimalism is about simplifying code by keeping a minimalist aesthetic.

The concept came about in the early 1960s when the minimalist’s art movement emerged in New York. Artists believed art had become stale and academic and sought to reveal the essence of their subjects by using as few elements as possible to create a maximum effect.

The principle of minimalism resonated with many other subcultures and has since been applied to aesthetics, politics, architecture, lifestyle design and business management.

The idea has also been adopted in the design of software – by removing lines of code, programmers are seen to improve their craft by creating more sophisticated, graceful code.

Most software developers would be familiar with some minimal coding conventions such as the DRY (Don’t Repeat Yourself) rule.

Occam’s razor, a problem-solving principle, suggests that among competing hypotheses that predict equally well, the one with the fewest assumptions should be selected. This principle aligns with minimalist coding practices and is now a common approach to software architecture. 

At inoutput, we are big believers in the Pareto principle, also known as the 80/20 rule. It asserts that about 80% of the effects come from 20% of the causes, or that 20% of the input produces 80% of the output.

In software engineering, the Pareto principle can be used to modify a software system to make it work more efficiently or by using fewer resources.

According to Coding Horror, the application of Spartan programming includes the frugal use of variables and adopts simultaneous minimisation of code across multiple dimensions including:

  • Horizontal complexity: The depth of nesting of control structures
  • Vertical complexity: The number of lines or length of code
  • Token count
  • Character count
  • Parameters: The number of parameters to a routine or a generic structure
  • Variables
  • Looping instructions: The number of iterative instructions and their nesting level
  • Conditionals: The number of if and multiple branch switch statements.

There are some benefits to writing in this minimalist way because it simplifies code, allowing for easier comprehension and less regression issues.

The fewer characters used in code and the simpler the command statements are, the easier the code will be to read and understand.

However, it is important to strike the balance between crafting simplified, svelte code and maintaining comprehension.

There’s no use writing clean, minimal code if it becomes too cryptic or hard to understand, or if it doesn’t take full advantage of the language.

The aim is to condense code elegantly while making that code work to maximum effect.

Code Minimalism Birds Article

Should you write minimalised code?

Aside from appreciating the aesthetic of smartly-crafted and simplified code, it is useful to strive for simplicity because, in this case, less can often be more.

Minimising code means there is a reduced reliance on a specific programmer which can allow for smoother work processes and project transitions, and the ability to work autonomously.

For example, if a programmer writes code for a business or project and is moved on, someone else will have to come along and read their code. If the code is not easy to understand, then the transition to a different programmer will take longer and likely be a frustrating exercise. Trying to make sense of someone else’s code could mean a nightmare when it comes to automating debugging. The more readable the code is, the easier it is to detect bugs and resolve them.

Simplified code is about more than just neat composition and arrangement. It’s about enjoying the application of a structured set of rules, a well-built foundation and forging complete trust in your tools. It’s about that feeling you get when simplicity and freedom meet.

Remember that we have to write code for other humans (not machines) to read and understand so a simple language is the key to effective communication.

Aim to keep your technique as minimal as possible with limited restrictions, classes with a single responsibility and avoid unnecessary comments.

An example of minimising code

Here is a hypothetical example of code in PHP – a method that returns the best Breaking Bad character from an imaginary list.

A common approach would be:

public function getTheNameOfTheBestBreakingBadCharacter() {
	$allTheCharacters = $dataLayer->getAllCharacters();
	$currentCharacter = null;

	foreach ($allTheCharacters as $theCharacter) {
		if($currentCharacter == null || $theCharacter>isBetter($currentCharacter)){
			$currentCharacter = $theCharacter
		}
	}

	return $currentCharacter
}

While there is nothing technically wrong with this code, many people would write it this way. It is clear and almost reads like pseudo-code. The method and variables are clearly named and almost overly verbose.

A more minimalist approach would be:

public function getBestCharacter() {
	$character = null;

	foreach ($dataLayer->getCharacters() as $char) {
		if($char == null || $char->isBetter($character)) {
			$character = $char
		}
	}

	return $character
}

The method name and the variable names have been shortened, yet are still clear and readable. The ‘getCharacters’ call has been moved inside the ‘foreach loop’ for clear scoping.

A very minimalist approach would be:

public function getBestCharacter() {
	return array_reduce($dataLayer->getCharacters(), function($a, $b) {
		return $a->isBetter($b);
	});
}

Here, the code has been reduced from its original 12 lines to only 5 lines. For a competent PHP programmer the code is still clear and readable. It also emphasises ‘learning your framework’, as ‘array_reduce’ in PHP which is a very handy function (or ‘reduce’ in all languages). And, although the variables are called $a and $b, this does not impact on readability as they are just confined to the inner scope of the reduce function.

Note that this is just an example. It is indeed a contentious issue and everybody has their opinion on coding standards. We are not necessarily sold on calling variables $a and $b even if it is tightly scoped.

Start minimising your code

In summary, code minimalism is about striving for simplicity, maximising benefit from minimum effort and reducing reliance to create greater autonomy.

The work will be perfected, and finished, not when there is nothing more to add but when there is nothing left to take away.



TELL US: Do you have any helpful tips for code minimalism?