Developer's Guide to "KISS" code

A short guide to keeping it simple, stupid

·

5 min read

Developer's Guide to "KISS" code

Hello buzdies! When talking about the principles of programming, the KISS principle is remarkable. Standing for "Keep it simple, stupid", it is the most accessible principle to apply in real-life situations.

What does it actually mean?

Source: Unthinkable

The meaning of KISS is as simple as it sounds: Keep it simple, stupid. It's not only about simplifying the code. You must be able to take a step back and view whatever you just did from another person's view, who is totally clueless.

If you have built applications before, you might have already engaged in this problem by writing code and reviewing it months later. Many lines are unreadable crap because you couldn't take a step back and simplify the concepts.

I myself have experienced this thing when I'm trying to do some updates on programs I made a long time ago. More often, I end up being confused about the code so the little things take a lot of time than it really does because my code wasn't simple at all.

panda.gif

Why use KISS?

You should probably ask why not use KISS, which is easier for me to answer.

There are many reasons to use KISS. KISS principle in software development allows developers to tackle the most significant difficulty of software development— implementing changes to the code written by either you or another person. I think this reason is super enough to write KISS code when thinking about my experience itself.

There are a few other scenarios that make us think again about writing simple and stupid code:

  • You can solve more problems, faster by just looking at them.

  • You will be able to build larger systems that are easier to maintain

  • Your code base will be more flexible, and easier to extend, modify or refactor when new requirements arrive

  • You will be able to achieve more than you ever imagined, with some beginner-like simple code rather than large complex lines of code.

  • You will be able to work in large development groups and large projects since all the code is stupid simple

KISSing your code

image.png

Remember what I said first, the KISS principle is the easiest principle to apply in real life. Everything you need to do is, think like an experienced beginner.

Who is an experienced beginner? Lemme explain.

Let's think about an experienced beginner in Python. What does he know about Python? Basic programming stuff like data types, loops, conditions, and how to work with a few libraries. But remember, he is experienced— he knows that he will need to modify and update every piece of code he's writing.

When he is asked to create a calculator, he just uses a few conditions, inputs, functions, and a little math. It might be long, but it's readable for anyone even a non-programmer. According to me, this is the best implementation of the KISS code. It's so stupid and simple.

Measuring programming progress by lines of code is like measuring aircraft building progress by weight. —Bill Gates

Here are practical tips to apply KISS in programming.

Avoid Global Functions or Elements

The Global States or Global Variables or simply Globals, refer to states or variables whose scope extends to the entire application. Moreover, their state can change at run-time which means, they are mutable.

Although Globals appear to be an easy way to share states between parts of the application without having to pass them through functions, they are detrimental to the implementation of KISS principles.

Globals give rise to significant problems such as Global name collisions, low testability, and concurrency issues. Furthermore, Globals add to the overall complexity of the code. They often act as hidden elements whose change of state is undeterminable.

Avoid the Lasagna Architecture

image.png

The Lasagna Architecture is the outcome of a very strict layering approach wherein each layer only knows about the layer immediately below itself. Rather than actualizing the intended ‘perfect’ system, this approach often leads to an unnecessarily complex one.

Such a system is immensely complex to read, and making any changes to it is difficult. For even the slightest chance, the developer has to check most sections of the code, determine if the change in one layer would affect another, visualize the functioning of multiple layers, face unidentifiable errors in verification, and so on.

Avoid Abstractions and Dependencies

image.png

Abstraction is a process that envelopes a complex code block in a layer of simplicity. However, despite assigning a simple name to call the function, this method makes the overall process inherently more complex.

The function might have numerous interconnected parts, dependencies, and implementations of which the developer might be unaware at the time of calling. In fact, when abstractions aren’t dealt with properly, they ultimately lead to a lasagna architecture, resulting in related issues and undesirable complexities.

Moreover, abstractions heighten the risks of improper or unnecessary coupling, which is a major deterrent to simplicity. The same applies to dependencies. Although dependencies are inevitable at times, it’s imperative to avoid them whenever possible.

Avoid Dead Code

image.png

A habit among developers is to include lines of code that have no present purpose but are intended to be useful in the future. These are dead codes. By making systems unnecessarily complicated, they do more harm to the system than good.

Unless a line of code has any immediate purpose, it’s always better not to use it.

Conclusion

The meaning of KISS is as simple as it sounds: Keep it simple, stupid. KISS has now become my favorite coding principle because it requires nothing but thinking like a beginner.


References