KISS Principle (Keep it simple stupid)
KISS - an acronym for “keep it simple stupid” is a design rule that every professional developer should apply to their code. It reduces complexity and improves readability as well as maintainability by aiming for the simplest solution.
What does KISS stand for?#
KISS stands for Keep it simple stupid. It is the first usability principle of product design but is also used as a coding principle. Legends say that the acronym was minted by Kelly Johnson, a lead engineer at Lockheed. Johnson handed his team of design engineers a handful of tools and challenged them to build a jet aircraft that was fixable by what an average mechanic in the field under combat circumstances would have.
What does KISS mean?#
The key design principle itself, however, is as simple as its name indicates: Keep it simple. This can be simple. But: there are countless ways to describe something in a complex way, but there might only be one that is precisely and short. It's not only about simplifying things. You must be able to take a step back and view whatever you just did from one's perspective, who is totally clueless. You might have already engaged this problem by writing code and reviewing it some months after that. Many lines are unreadable crap because you weren't able to take a step back and simplify the concepts.
As we mentioned earlier, KISS isn't a principle that is originated in computer science. It can be applied to design processes in general, such as product or software design. We, in this case, will focus on how to apply simplistic design to code itself.
Code is not written for machines#
The code you write and the code you actually execute largely differ, depending on the language and compiler or interpreter used. In fact, machines couldn't care less about your code. All the programs and scripts you write are for humans. You write it only once but read it possibly a thousand times. Thereby it's your responsibility to make this as easy, and thereby quick, to read as possible. Your code design objective thereby should be to offer the best user experience possible.
Simple doesn't always mean short#
Another mistake that is often made dealing with the KISS principle is that people try to make code as short as possible. But the shortest solution is rather seldom the easiest to read. Consider the following "Non-KISS" example:
f = lambda x: 1 if x <= 1 else x * f(x - 1)
Now look at this version:
def faculty(number: int) -> int:if number <= 1:return 1else:return number * faculty(number - 1)
Sure, the first version is also readable, but it is considerably harder to do so. Adding a few descriptive names, type hints, and splitting the line into multiple ones gets clearly easier to read. The main goal in design should always be to be as easy to understand as possible. One of many common bad practices of developers is to create fancy and unreadable "oneliners". There is absolutely no reason to do so. This will most certainly lead to someone, if not themself struggling to decipher it.
How to apply the KISS principle?#
To apply KISS to design and code, there hundreds of ways but, I want to provide you with some ideas:
Simplify and explain concepts#
Try to code as simple as possible, unfold your concepts, name things properly and explain with comments where the code itself can't (Most of the time, it can and should!).
Use descriptive names#
As easy as this sounds, trying to find names that indicate something's true meaning can be very challenging. You may try this by telling someone a name and let them assume what they think that is. Don't be afraid of long variable or function names. As stated earlier, the machine doesn't care and won't use your name anyway.
Deconstruct problems into smaller ones#
Something that does real magic to the readability of your code is deconstructing the problem and solving just one simple one with each function. Try refactoring your code and solve a single problem at a time, but with a maximum of precision and readability.
Did you ever try to explain your code to a dumb duck? No? Try duck debugging! By doing this, you may not only find bugs, but you may also find gaps in your or someone else's understanding of the current code.
Learn more about Clean Code#
Many principles and ideas of clean code build upon the KISS principle. By applying those concepts, you may find it easier to identify the simplest solution.
- The origin of KISS - https://en.wikipedia.org/wiki/KISS_principle#Origin
- Kelly Johnson - https://www.lockheedmartin.com/en-us/news/features/history/johnson.html
- Meaningful Names - https://code-specialist.com/code-principles/meaningful-names/
- Rubber Duck Debugging - https://rubberduckdebugging.com/
- More Posts on Code Principles Blog - https://code-specialist.com/code-principles/