Editorial note: I originally wrote this post for the Infragistics blog. Click here and check out the original at their site. There are a number of authors worth checking out that write for them.
Have you ever found yourself running your code to test out some behavior when you noticed something unrelated and thought, “that’s odd?” Maybe you wanted to verify that clicking “run” kicked off the process it was supposed to, but you noticed that the “cancel” button was randomly green for a second when the window opened. “That’s odd,” you thought.
After verifying that the process was kicked off properly, maybe you re-launch the application to see about that odd, green cancel button. But when you open the window this time, nothing. It’s the normal color, with no sign of the green you noticed before. Again, you thought, “that’s odd.” And maybe, at that point, you shrugged, chalked it up to some weird OS rendering burp, and moved on.
You never knew why the button went green, and you never knew why you couldn’t reproduce it. This is a relatively benign version of the phenomenon known as “programming by coincidence.”
“Programming by coincidence” was coined in the book, “The Pragmatic Programmer,” and they define it as “[relying] on luck and accidental successes” rather than programming deliberately. In the case of the mysteriously green button, the accidental success is the fact that the problem just kind of vanished on its own.
It’s probably no great surprise that the Pragmatic Programmer’s stance on programming by coincidence is “don’t do it.” It’s my stance as well, and I imagine a lot of you reading agree. And yet, it’s something we’ve probably all been guilty of at one time or another. Read More