Keep It Simple
In software development, code often becomes more complex than necessary, making maintenance and updates harder. The “KISS” principle helps reduce problems to their core and avoid unnecessary complexity.
This principle applies on multiple levels: from individual functions to entire applications.
The Principle at an Abstract Level
At a high level, it means that a program or tool should do one thing and do it well. Linux provides a great example: many small programs each handle a single task. Combining them allows for solving more complex problems efficiently.
Example: Find all running „ssh“ processes
ps aux | grep ssh
user 38351 0.0 9552 2436 pts/2 S+ 22:23 0:00 grep --color=auto ssh
Analysis
Each program does a simple job. ps prints the processes, while grep can be used to search content. Combined with a pipe, you can search the processes.
The Principle at the Implementation Level
Even simple tasks can be implemented in multiple ways. Let’s look at checking whether a value exists in a list of numbers in C++.
Variant 1 – Basic for loop
bool contains(const std::vector<int>& v, int target) {
for (size_t i = 0; i < v.size(); i++) {
if (v[i] == target) {
return true;
}
}
return false;
}
Variant 2 - Using library function
bool contains(const std::vector<int>& v, int target) {
return std::find(v.begin(), v.end(), target) != v.end();
}
Analysis
- Variant 1 is explicit and maybe easier to understand for someone, who is new to the language; you can follow the loop step by step.
- Variant 2 is more concise, leveraging the STL library; experienced readers instantly understand the logic.
Which variant do you like more? Let me know in the comments.
- The core principle remains: write code focused on the task, avoiding unnecessary complexity.
Why Simple Code Matters
Code is read far more often than it is written. Maintaining or extending a program requires understanding it first. Simple code is easier to maintain for yourself and for others.
Comments help, but ideally, code should be self-explanatory.
Writing Simple Code is Hard
Many developers mistakenly think simple code reflects a lack of skill. But it requires consciously removing unnecessary complexity. Experienced programmers often try to show off clever solutions, which can make code harder to understand.
The guiding rule: Each piece of code should do one thing.
- At the function level: a method should have a single responsibility.
- At the application level: software should serve one clear purpose.
Conclusion
Simplicity is not a lack of skill, it’s a sign of maintainable, professional software development. Keeping code simple saves time, reduces bugs, and improves collaboration.
Tip: With each new function or feature, ask: “Am I making this as simple as possible?” and remove anything unnecessary.
Top comments (0)