Programming by wishful thinking and why we need to read

May 03 · 4 mins read

Having been influenced by the idea of TDD (even though I have never used it in a project) and the idea of “respecting levels of abstraction”, (and the Clean Code book of course), in my recent tasks at work, because we do not have unit tests, I just make myself imagine that the UI layer (the Presenters, Controllers, etc.) contains the tests.

Then I try to start writing code on the user interface level of the app (the Presenters, Controllers, etc.).

I then ask myself, “At this layer in the app, how do I want my classes and methods/functions to look like?”

Then I try to call functions (which are not yet implemented) with as little set of parameters as possible – only those parameters that I think are needed at this particular point in the app.

For example, at the level of the user interface (the Presenter, Controller, etc.), I start to call functions like this…

bookmark(item); or item.bookmark();

… or this …

int numberOfItems = someObject.getNumberOfItemsOn(date);

… even though they are not yet implemented.

When they look good already, I start to implement them.

public class SomeObject {

    ...

    public SomeObject(..., ...) {
        ...
    }

    public int getNumberOfItemsOn(Date date) {
        ...
    }
}

Then… Last night, while reading Chapter 10 of “Growing Object-Oriented Software Guided by Tests”, I learned that someone (or sometwo :smile: ) has already named this kind of process before.

Abelson & Sussman named it “programming by wishful thinking”.

“… you write some code that uses a component, before you implement the component. This helps to discover what functions and data you need, and following this discovery will lead you to more simple and useful APIs.” — from “Wishful Programming” post by Bart Bakker

This reminds me that there is nothing new under the sun. When [we do not read that much and] we think we discovered something new, we have to remember to assume that someone might already have discovered it before, and even formalized it or have already given it a name.

This also reminds me of one of the reasons why we need to read (or watch video lectures):

The problems that we encounter today might have already been solved by someone else. We don’t have to do everything by trial and error. We have to learn from the experiences of others.” – Ryan Holiday

(If you have comments about my English, and other things, please tell me : smile:)

Buy Me A Coffee