Contents
Meta
—
* still in an early stage
—
* still in an early stage
All non-trivial abstractions, to some degree, are leaky.1)
A solution is bad if a) the leakiness of abstractions is ignored (bad usage of an abstraction) or b) the benefits of the abstraction cannot justify the disadvantages created by its leakiness (bad abstraction) or c) the abstraction is more leaky as necessary (bad abstraction)
The law of leaky abstractions by no means says that abstractions are generally a bad thing. Abstractions are extremely helpful and software development without abstractions is virtually impossible. So abstractions are good and developers should strive for creating good abstractions. But abstractions are never perfect and this has to be kept in mind.
See also section contrary principles.
Joel Spolsky: The Law of Leaky Abstractions
In C++ string literals such as “test”
are of type char*
which means pointer to a character. There is some range in the memory where a sequence of characters is stored and there is a pointer pointing to the first character. These strings are unhandy to use so there are string classes in C++ which abstract from these low-level strings which are simply a heritage of the C programming language C++ is built upon. These string classes are much more convenient to use and create the illusion of a built-in string type.
#include <iostream> int main() { std::string s = "Hello"; s = s + " World"; std::cout << s << std::endl; }
In this code it looks like there was a native string type in C++. This is the abstraction. But the abstraction is leaky. The following code does not work:
#include <iostream> int main() { std::string s; s = "Hello " + "World"; std::cout << s << std::endl; }
So it would be better if C++ had a real native string type.
(this example is taken from 2)
Discuss this wiki article and the principle on the corresponding talk page.