The Law of Code Attraction
I have made an observation the other day about a curious correlation between Test Driven Development (TDD) and what is nowadays called the “Law of Attraction”. This law roughly states that you will attract into your life whatever things or circumstances you repeatedly think about and desire. While I don’t agree with how this concept is very aggressively marketed today, I tend to believe that it works.
A key element in the law of attraction is faith. If you repeatedly think about something that you would like to have (or have happen), and you have absolute faith in getting it, you will get it, the law says. This technique can be put to use most efficiently by repeatedly thinking about the desired thing as if it would already exist (thereby exercising absolute faith). For example (modern examples for this law seem to be notoriously monetary), instead of saying, “I have faith that I will have a million dollars”, one would rather say, “I have a million dollars”, ideally repeating this several times a day.
By stating the object of our desire as if it were already a reality, we create a strong tension between the current reality (not having whatever we desire) and the desired reality. Our persistent and absolute faith then compels us to take action that will resolve this tension, by moving us towards the reality that we desire. Depending on your believe system you might also say that this action is guided by god, the universe, your subconscious mind, the divine, your muse, inspiration, etc.
If you are a programmer, and you are still reading, congratulations! Because now, let’s think about Test Driven Development, where usually one goes about writing a test first, that is, before the code that would make it pass even exists. So by writing this test, we pretend that the code that will make it pass (or even compile) is already there. Our test talks about the desired object, the code, as if it were already written. In other words, we express absolute faith that the code will eventually be written. In fact, our failing test starts to attract the code that will make it pass.
Now, after we clearly stated what we want, we run our compiler and/or test framework and immediately get in touch with the current reality. The test will fail. Since we have a desire to make it pass, and absolute faith that we can make it pass, the tension between the current reality and our desired reality will compel us to write the code necessary to make the test pass. Every test becomes a mini-fulfillment of a clearly stated desire. That might just be the reason why writing failing tests and making them pass is so satisfying.