Movatterモバイル変換


[0]ホーム

URL:


Principles Wiki

User Tools

Site Tools


principles:keep_it_simple_stupid

Keep It Simple Stupid (KISS)

Variations and Alternative Names

Context

Principle Statement

A simple solution is better than a complex one, even if the solution looks stupid.

Description

TheKISS principle is about striving for simplicity. Modern programming languages, frameworks and APIs have powerful means to create sophisticated solutions for various kinds of problems. Sometimes developers might feel tempted to write “clever” solutions that use all these complex features. TheKISS principle states that a solution is better when it uses less inheritance, less polymorphism, fewer classes, etc.

A solution that follows theKISS principle might look boring or even “stupid” but simple and understandable. TheKISS principle states that there is no value in a solution being “clever” but being easily understandable.

This does not mean that features like inheritance and polymorphism should not be used at all. Rather they should only be used when they are necessary or there is some substantial advantage.

Rationale

A simpler solution is better than a complex one because simple solutions are easier to maintain. This includes increased readability, understandability, and changeability. Furthermore, writing simple code is less error-prone.

The advantage of simplicity is even more significant when the person who maintains the software is not the one who once wrote it. The maintainer might also be less familiar with sophisticated programming language features. So simple and stupid programs are easier to maintain because the maintainer needs less time to understand them and is less likely to introduce further defects.

One reason to create more complex code is to make it more flexible to accommodate further requirements. But one cannot know how to make it flexible or if that flexibility will be ever needed.

“When you make your code more flexible or sophisticated than it needs to be, you over-engineer it. Some do this because they believe they know their system's future requirements. The reason that it's best to make a design more flexible or sophisticated today is to accommodate the needs of tomorrow. That sounds reasonable, if you happen to be a psychic.” - Refactoring To Patterns - Joshua Kerievsky.

Another reason to create more complex code is to make optimizations. An optimized code is a more complex code. Pareto principle applies also in code: a program spends most of the time in a small portion of the code, so it will be wise to concentrate the effort to optimize only that part of the code. Another best practice is to follow the “Three rules of optimization”: (1. Don't, 2. Don't… Yet, 3. Profile before optimizing), which make sense: to optimize only the code with performance problems. (First author: Michael A. Jackson)

Strategies

This is a very general principle, so there is a large variety of possible strategies to adhere more to this principle largely depending on the given design problem:

Caveats

Origin

The principle was coined by the American engineer Kelly Johnson referring to the requirement that a military aircraft should be repairable with a limited set of tools under combat conditions1).

The principle of striving for simple solutions sometimes is also called “(rule of) simplicity”2) which was also prominently stated by Tony Hoare in his Turing Award lecture: “I conclude that there are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.”3)

Evidence

Accepted: This principle is widely known and accepted in practice. See for example Jargon File:KISS Principle

Examined: While the preference for simple solutions can be considered trivially intuitive, there has been some work relating simplicity or rather complexity and certain quality attributes. But as there is no universally applicable complexity metric and not even a commonly agreed upon clear definition of simplicity, research is bound to examine certain aspects ofKISS independently.

The following hypotheses can be stated:

All these hypotheses can be examined with respect to different complexity metrics.

Hypothesis 1 is true by definition. If the solution cannot be implemented quickly, it is not simple.

Though hypotheses 2 and 3 are not true by definition but they can be regarded intuitively clear. Nevertheless, there is some research. In4) a system was improved in two steps resulting in three variants of the same system. Several metrics show that the improvements reduced complexity. 36 programmers with varying experience conducted three different maintenance tasks and their performance was measured. The results indicate that the improvements also improved maintainability. Several other studies support the correlation between complexity and maintainability5).

Furthermore, software cost estimation techniques are partly based on complexity judgments6). So complexity—although this normally relates the complexity of the problem and not to the complexity of the solution—is a known cost factor which may be accounted to maintenance.

Lastly, hypothesis 4 is likely to be false. Several studies relating complexity metrics and post-release reliability show that module size in lines of code predicts reliability at least as good as the McCabe metric (also called cyclomatic complexity)7). Assuming cyclomatic complexity correctly depicts the complexity of a module, reliability should not the reason for applyingKISS.

Relations to Other Principles

Generalizations

Specializations

Contrary Principles

Note that many principles are contrary toKISS. This means that it is worthwhile to considerKISS when considering one of those. Nevertheless this does not mean that this is true the other way around. When consideringKISS, one wouldn't want to consider all principles that have complexity as a disadvantage. So here are those needing consideration:

Complementary Principles

Principle Collections

OOD Principle Language
General Principles
MLKISSMIMCDRYGPRoE
Modularization Principles
MPHCECV
Module Communication Principles
TdA/IELCDIP
Interface Design Principles
EUHMPLSUP
Internal Module Design Principles
IH/EIAPLSPPSU

Examples

Example 1: Fuzzy Simplicity

Simplicity is a blurry, partly subjective measure. Sometimes it is difficult to tell what is simpler. The following example shows that:

publicString weekday1(int dayOfWeek){switch(dayOfWeek){case1:return"Monday";case2:return"Tuesday";case3:return"Wednesday";case4:return"Thursday";case5:return"Friday";case6:return"Saturday";case7:return"Sunday";default:thrownewIllegalArgumentException("dayOfWeek must be in range 1..7");}} publicString weekday2(int dayOfWeek){if((dayOfWeek<1)||(dayOfWeek>7))thrownewIllegalArgumentException("dayOfWeek must be in range 1..7"); finalString[] weekdays={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"}; return weekdays[dayOfWeek-1];}

Both methods do exactly the same thing. They return a string representing the weekday. Just the implementation is different. Both versions may be seen as simpler than the other depending on the view taken.weekday2 has less statements and less execution branches. Complexity metrics measuring these aspects (e.g. thecyclomatic complexity) will therefore preferweekday2.

On the other handweekdays1 uses less language features (just switch, return, and exceptions whereasweekdays2 needs if, arrays, arithmetic, return, and exceptions). Furthermore inweekdays1 the relation between input and output can be seen directly and it's clear how it works by just seeing the method. But for understandingweekday2 there are more details to think about. This is especially true for the range check at the beginning and the index computation in thereturn statement. Clearly this is not particularly difficult but these are aspects which are more difficult than in the other version of the method.

So it's not objectively clear which of the two implementationsKISS prefers without saying which complexity metric to apply. But this ambiguity is not a problem since principles are not meant to be unambiguous and objective. Eventually a human developer has to decide which solution to implement and the principles only give guidelines.

Description Status

Further Reading

Discussion

Discuss this wiki article and the principle on the correspondingtalk page.

4)
Virginia R. Gibson and James A. Senn:System Structure and Software Maintenance Performance
6)
Barry W. Boehm:Software Engineering Economics, IEEE
7)
see Albert Endres, Dieter Rombach:A Handbook of Software and Systems Engineering, p. 168ff.
principles/keep_it_simple_stupid.txt · Last modified: bychristian


[8]ページ先頭

©2009-2025 Movatter.jp