Pages

March 26, 2014

Understand why

So - You writing code in a TDD manner, which means that basically you're writing the tests first, waiting on them to fail, and then writing the code which makes them pass.
Damn, This makes you feel so good, right? It's like a fucking rush of satisfactory that runs through you, making you feel like you're fulfilling your roll in the world, you're executing your work to the max. You tell yourself that hey! You can do that forever. No, seriously - FOREVER.
But have you ever wondered why you feel this way?

What exactly taps into our, some-might-claim, childish emotions and pats us so warmly on the back?
Most of us feel this way and usually don't give a flying fuck about the end product, not to mention the end user of the software their creating, and this puzzles me greatly - what makes some of the smartest people I know develop such an addiction to this an immediate, shallow gratification feeling?

Let me try to ware the psychoanalyst hat for a sec and make some assumption on why a developer feels so good about a passing test and try to drilldown to find the true nature behind this behavior

Is it the quality?
Sadly I would have to say it is rarely the quality increase which makes developers so satisfied with a passing test. I saw many tests which don't check anything and pass successfully, leaving their author happy like pig-in-shit with the work he has done. In some cases developers would even knowingly cheat to make a test pass, although they will later claim that this has damaged their satisfaction since our conscience cannot be tricked that easily. Sadly The build can.
Personally I believe that the motivation for tests should come from the desire for quality. Why you desire quality? Because you want your customers to enjoy and have value from using your product.

Is it the game?
Writing software in TDD is very much like playing a self-game. It's like you're challenging yourself, much like playing Solitaire, putting targets and then trying to hit them with a stone from afar. And you have monitoring which kind of represent a leader board where you can see how much targets you did hit (coverage), which sometimes brings developer to an absurd state where their testing meaningless chunks of code like Getter/Setter just to have their coverage up.
The game does have contribution to developers satisfactory, but playing it without seeing the big picture can sometimes distract you from the real goal, that is making the end product better for the user. After all, all your code can be covered with tests and you will be top of the list, but the product doesn't sell because it is a piece of crap. Better start packing your cubical up cause your company went broke, but hey, don't forget to take the leader board with you!

Is it the mini-goals?
You know these lovely "todo" lists? Research have shown that people enjoy "checking" a task as done so much, that sometimes they register task they've already completed just to have a "check" next to it, while other tasks that really needs to be attended are left neglected. You know what I'm talking about… yes, you do.
Same with the tests. You set up small tasks to be completed, e.g. "should fetch user information" and you right away start working on it in order to put a "check" over it. You like it so much that sometime your tests become meaningless and have no benefit to the code quality or maintenance.

Is it our perfectionism?
You love to see thing done to perfection. Most of us who deal with software do (some don't and they should get fired. I mean - now). So you look at your coverage report and you eye starts to twitch for you see that you're missing 3% to your 100% code coverage. You don't mind that the code is well tested and that 3% is redundant and having them tested will not benefit the code whatsoever. You want to reach that 100%. A sheer waste of your time and build time, but damn - I just have to get this thing 100% green.

Tests are not the goal, they are the means
Yeah, let's remind ourselves once again that tests are the means in getting our goal which is a good and maintainable product releases. No seriously, you have to stop dancing around glorifying the hammer you're holding when all you have built with it is a huge pile of horseshit.
And for that matter, I strongly believe that developers should stop alienating themselves from the product. It's not only the tests which are the means but the code itself. It is your means for creation. The paint of your brash, the nail and hammer. We should concentrate on the painting. You have never seen a child showing off his color palate to his parents, right?

So why then?
Because it's easy and well defined.
If you look closely at the tasks you've recently had you'll find it that you tend to avoid those which are hard to complete or those which you have a vague idea on how to accomplish them. Tests are neither. Tests are well defined tasks which are basically easy to complete. Of course you like doing them. Hell, you're even get credits if you make a lot of them.
Why the hell won't you write them like crazy?

Conclusion
Tests are good. Write them. Responsively.
You're not a monkey. Always question the world around you and try to understand… why.