Sponsored Link •
|
Summary
Two of the cardinal sins of programming are overspecification and underspecification. Today I will discuss the one most programmers are guilty of: overspecification.
Advertisement
|
Underspecifying the solution to a problem usually reveals pretty obvious bugs. The typical result is a either failure to compile, or the software throws an exception during some unchecked edge case. However there are no bells or lights that go off when you overspecify the solution to a problem. In fact it is commonplace.
Consider the following summation function in C++:
size_t sum(const vectorSo where is the problem? Well the problem is that you told the computer to iterate through the list and add each value to the result. That produces a correct answer but it is more specific than is needed. You are specifying the following superflous facts:& values) { size_t sum = 0; for (size_t i=0; i < values.size(); ++i) { sum += values[i]; } return sum; }
The culprit here is the language, which prevents you from specifying the solution as succintly and as precisely as you want. In Cat I would present the solution as follows:
def sum : (list<int>) -> (int) { [+] reduce }Reduce is an atomic concurrent program which takes an associative function and applies it to a list, like a fold function does, but in any order the compiler or runtime deems appropriate.
Have an opinion? Readers have already posted 25 comments about this weblog entry. Why not add yours?
If you'd like to be notified whenever Christopher Diggins adds a new entry to his weblog, subscribe to his RSS feed.
Christopher Diggins is a software developer and freelance writer. Christopher loves programming, but is eternally frustrated by the shortcomings of modern programming languages. As would any reasonable person in his shoes, he decided to quit his day job to write his own ( www.heron-language.com ). Christopher is the co-author of the C++ Cookbook from O'Reilly. Christopher can be reached through his home page at www.cdiggins.com. |
Sponsored Links
|