Programming by Coincidence

Mudit Maheshwari
Jul 10, 2017 · 2 min read

Programming by Coincidence means that you don’t know what you are doing. Your code relies on luck and accidental success. You should avoid programming by coincidence and start programming deliberately. Suppose you keep writing a program that works fine and solves all your purposes. One day, it suddenly breaks. And despite spending significant amount of time and effort you won’t be able to fix it. You won’t be able to get it right. The reason is simple, you never knew why it worked in the first place.

I would list some ways you can avoid programming by coincidence.

  1. Don’t rely on undocumented error or behavior. For instance, you may end up using a function of a unstable library and can get your code running. But because the library is unstable the function may be changed in the next release.
  2. Don’t take assumptions. Assumptions that aren’t supported by well-established facts lead to problems later. Assumptions lead to conflict. Don’t assume it, prove it.
  3. Always remember the big picture. Be aware of what you are doing. Don’t just code blindfolded.
  4. Test your application very frequently. Write unit tests for every new functionality you add. This way you will be able to compare the expected and the actual behavior. You will understand you code better.
  5. Never fear to make changes to your code when you think it is no more appropriate. Never miss a chance to refactor your code.

So, next time your code is running correctly and you don’t know why, STOP. That should ring a bell. Is it programming by coincidence?

Hope you like it. Any suggestions are welcome and would love to hear them.

Written by

Product Engineer @ Gojek

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade