The Artima Developer Community
Sponsored Link

Heron-Centric: Ruminations of a Language Designer
The Problem of Overspecification
by Christopher Diggins
July 19, 2006
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 vector& values)
  {
     size_t sum = 0;
     for (size_t i=0; i < values.size(); ++i)
     {
        sum += values[i];
     }     
     return sum;
  }  
So 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: By overspecifying the solution to a problem you tie the hands of a compiler and prevent it from doing anything clever, unless it can unravel your intent from your instructions. Generally that is a losing battle.

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.

Talk Back!

Have an opinion? Readers have already posted 25 comments about this weblog entry. Why not add yours?

RSS Feed

If you'd like to be notified whenever Christopher Diggins adds a new entry to his weblog, subscribe to his RSS feed.

About the Blogger

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.

This weblog entry is Copyright © 2006 Christopher Diggins. All rights reserved.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use