The KISS Principle in Programming


5. Mai 2021

Situation: the developer did not understand the task, got confused with the code, and thwarted the project deadline. This can happen for a variety of reasons, but often problems arise from a violation of the KISS principle. Let’s figure out what this means.

What is KISS

The KISS principle is when you take a problem and solve it in a simple way:

  • Don’t include the entire library if you only need a couple of functions.
  • Do not include redundant functions unless requested by the customer.
  • Don’t use redundant classes and methods.
  • Don’t overload the interface or do complex business logic.
  • Do not perform other actions if they do not affect the operation of the project.

When you don’t do too much, simple understandable code and a reliable program appear that solves the customer’s problem.

The abbreviation KISS stands for “keep it short and simple”. It was invented by aircraft designer Clarence Johnson shortly before World War II. He demanded simple drawings and instructions from his engineers — it was important that using these documents, front-line aircraft mechanics could independently deal with most of the damage and repair the aircraft.

To do this, the engineers had to discard complex terminology and write as simply as possible. Later, the KISS principle migrated to the design documentation of the US Navy, spread to different areas, and now it has become an integral part of programming.

The KISS principle: If something can be done easier, do it. We are talking about code, but it works in any other area.

Why do you need

Often programmers measure their success not by the quality of the finished application, but by the number of lines of code — they look at the amount of work done. If the program turns out to be short, the author feels dissatisfied and forgets about the time that he spent studying, algorithms and testing.

Using the KISS principle, attention is shifted from the workflow to the result. When the program is running and coping with the task, it doesn’t matter how many lines there are.

If you accept this for yourself, then you begin to use simple solutions that you previously avoided. This creates a high-quality compact code that is easy to maintain. If you don’t accept it, most likely you just don’t want to do the work, the meaning of which is not very clear to you.

Everyone chooses what suits him best. For example:

  • Write a code, and everyone will immediately see what a cool developer I am and how much I know. It was not in vain that I graduated from programming courses and solved problems (Wrong approach).
  • I am already a senior, and my code should be somewhat different from Jun’s code. I can’t write an ordinary program that every trainee can handle (Wrong Approach).
  • Write a code that will solve the problem and will be understandable to other developers. Suddenly I get sick — I need someone to serve him (Correct Approach).

How to use it

Step 1. Learn the generally accepted standards of your programming language. For example, for Python — without basic knowledge, you won’t be able to create simple code that everyone on the team will understand.

Step 2. Learn to understand the task correctly: you must understand under what conditions the work will be considered complete.

For example, the topic of this article is the KISS principle in programming. We are writing it for beginners, so it is important to answer only three questions: what is the KISS principle, why is it needed, and how to use it when writing code. If everything is clear in the text, the work is considered completed and you can finish.

With programs, the situation is similar: first, we define the final goal, then we make a list of steps to achieve it, choose the tools, and only after that we go to work. This way we will know what to do and why and which simplest version of the code to use to solve the problem.

When you get to grips with a problem, don’t act like many newbies: don’t become self-contained, and don’t pester your colleagues excessively.

Wrong approach: A beginner does not understand the task and does not ask colleagues for help: he writes the wrong code, receives a lot of comments, and constantly redo everything. This slows down product development.

Wrong approach: A beginner does not try to understand the task and immediately turns to experienced colleagues for help — he exploits soft skills and puts others in a position where it is inconvenient to refuse.

Correct approach: To understand the task, you need a balance between hard and soft skills: first, try to cope yourself, mark problematic points, look for answers, make a compact list of incomprehensible questions and go with them for help from colleagues. Programmers are loyal and friendly people without prejudice to the Juniors. But the human factor remains: no one wants to be a nanny if the person has not even tried to delve into the task.

Step 3. Analyze the finished project: you need to understand what function each piece of code performs and how it works. Leave the simple code and rewrite the complex or send it for refactoring.

What is the bottom line?

And in short and in a simple way, then you need to remember and start putting into practice three main things:

  • The KISS principle is when you do simply and only what the customer or employer asks for.
  • The KISS principle is when you write code that will be understandable to other developers even after ten years.
  • The KISS principle is when you understand the task and clearly understand when the work can be considered complete.

Try it — everything will work out!