Posted on ::

Suppose Fred is given a programming assignment. Fred types in some code, tries it, and it seems to work. Fred types in some more code, tries it, and it still seems to work. After several weeks of coding this way, the program suddenly stops working, and after hours of trying to fix it, he still doesn’t know why.

Fred may well spend a significant amount of time chasing this piece of code around without ever being able to fix it. No matter what he does, it just doesn’t ever seem to work right. Fred doesn’t know why the code is failing because he didn’t know why it worked in the first place. It seemed to work, given the limited “testing” that Fred did, but that was just a coincidence. Buoyed by false confidence, Fred charged ahead into oblivion. Now, most intelligent people may know someone like Fred, but we know better. We don’t rely on coincidences—do we? — The Pragmatic Programmer: From Journeyman to Master, p. 173.

The Pragmatic Programmer is a great little book, that shares space in my shelves with some other, not so little, icons of my generation: Clean Code, Code Complete 2, etc.

I first learned about programming by coincidence from one of my mentors, Pablo Graña. Pablo was a brilliant architect who took an interest in shaping young, wild coders and showing them the path to become engineers. One time I was "just trying to get something to work" and he said the magic words: "well, the problem is that you are coding by coincidence". Indeed I was.

That mental state where you are just trying to give the computer whatever it wants, just so that you programs does something that resembles working and you can rest your mind for a minute; that leads to coding by coincidence. It also may seem rewarding at first, but if you ask yourself "do I understand why this works?" you may be quickly disappointed.

Now, there is a time to throw stuff at the wall and see what sticks; it is indeed part of the learning process, and it usually goes away as you start to understand the patterns of who things work inside the little black box. It is, however, useful to check yourself and make sure you dedicate time and effort to internalizing those learning, so that the next time around you can code more and more confidently. The wording that the authors of The Pragmatic Programmer use is intentional programming, the practice of programming with purpose and deliberate thought.