Saturday, January 27, 2007

Programming by Wishful Thinking

I'm currently reading an excellent book called Everyday Scripting. I'm really enjoying it as a nice primer on whipping things up with Ruby. When I hit chapter 7 the author began to discuss a programming style that I have used for a while now. I had no idea this simple technique had a name. Many of the things I've picked up throughout my career I've learned from books, articles, reading code, and still others I've devised from first principals. This particular practice I picked up while pair programming with this super smart guy Peter Seibel. Apparently it's called "Programming by Wishful thinking."

The idea is that when you have some code to write you first do so as if methods existed already to perform the high level operations. Say you want to write some code that tells you how many songs and albums you have by a given artist. Programming languages don't have methods do to this baked in but you pretend they do. An extreme example of the practice would be:

I came up with this example in about thirty seconds so I hope it isn't to opaque.

You may then go on to write the above methods in the same way:

and so on and so on. Anyway, don't know how many people out there use this style but I've found it really helpful when I need to keep focused on the task/flow at hand without getting to distracted by the details of an issue. Maybe it will help someone else too.


  1. All the time! Thanks for posting this I also didn't know that it has a name.

  2. Actually, this method is quite common (what I know, at least I use it and some of my colleagues).
    Thus, I would call it "intentional" rather than "wishful".
    To give a sort of strange and maybe too far analogy: Test-Driven Development (TDD) is about writing your tests first, and then caring about the business logic.
    I think, coming from a high-level view and then going down to a more detailled level is a natural way for organized persons :-)

  3. I was taught to program using this style about 10 years ago in my introductory programming course where Smalltalk was the language of instruction.

  4. I thought it was called "top down"

  5. I don't think anyone was saying this is new. I was only pointing out two things:

    1) That I didn't know it had a name
    2) That I have found it useful

    I used to do a bit of Smalltalk back then too. It's a lot of fun. Probably why I picked up the Ruby book

  6. wow, pair programming with Peter Seibel must've been a blast. His book on lisp (PCL: Practical Common Lisp) is probably one of the best programming books ever written. And wishful thinking is praised by Gerald Jay Sussman (co-authored, with Hal Abelsson, the book 'Structure and Interpretation of Computer Programs') as a foundation of programming (he shows it off using Scheme though). Wishful thinking is extremely easy on Lisp, specially because it lets you go beyond the actual language, if it imposes limitations to you.

    Not that you didnt know that already (if you paired programmed with Seibel, you probably know :D ) but well, comments are for everybody.

    As some grandmother probably said somewhere sometime: "Nothing new under the sun". All you have to do is give it a name :)

  7. Intention-Revealing Interfaces
    may be.See DDD

  8. I'm the author of the book cited. I got the name from Abelson and Sussman. (Don't want anyone to think I claimed the phrase was original to me, much less the idea.)

    I do like "wishful thinking" over other descriptions, though, because it promotes the right Devil-may-care attitude: "Why *of course* the perfect method for me exists, so I'll write my code the way it should be, rather than distort it to meet what's available... Oh rats, it doesn't exist. Guess I'll have to write an adapter."

  9. Its funny. I am reading 'Everyday-Scripting' too and I am thankful that I got a name for this technique which I like a lot.
    In fact, my first aha moment in software design was when I discovered this technique on my own.