keskiviikko 11. tammikuuta 2012

TDD or better known as Test Driven Development

In Test Driven Development, the idea is to create test cases before you create the application code itself. Simple as that, or is it? It seems a lot of people don't get the idea behind TDD and instantly ask how can one create test scripts for a code that doesn't exist yet. Let's go over this process and learn the true power and efficiency you obtain with test driven development method.

So you open your favorite IDE (Zend Studio or Eclipse PDT in my case). You know you want to create a user class so you first define functions for it by writing down the specifications of course. You can the class skeleton, and functions to set and get user's last name.

Now you need to write down a test script for the class. But how exactly and what should you test for? Well, have a look at yours specs. You have a User class and according to your specifications, you have getter and setter for the lastname.

<?php
  class myClassTest extends PHPUnit_Framework_TestCase
  {
    public function testClass()
    {
      $user = new User();

      $this->assertEquals('User',get_class($user));
    }
  }

Now you have the very beginning of your test script. It's doesn't test much yet, but it will do for now. Now if you run this test with PHPUnit, you will get an error due to non-existing class file. So what do you do next? Create the class file itself.

<?php 
  class User
  {
  }

Yes, now we have a class file created and when we run the above test script again, we get a success this time. We tested that the instance of $user is class of User.

Getting the point now? First we write the test scripts based on the specifications and then little by little we add to the class and test on each iteration that all of those tested areas are added to the class and passing the objectives defined.

This method, of course, requires great amount of effort put down on writing down the specifications. This, however, should not come as a surprise to anyone dealing with web development.

But do you really want to know the great thing about this method? Haven't figured it out yet? When you get your class completely written and ready, you already have test cases created for that class!

Tell me how many times you really have time to create test scripts after you have written the classes? Even after 12 years of web development career, I have not seen one developer who writes test scripts with great joy and pleasure. If writing down test cases based on specifications and then writing classes to satisfy those test cases would be a way of development, we would be that one step better developers.

torstai 5. tammikuuta 2012

Don't Repeat Yourself

In movies, have you ever heard of saying "Don't make me repeat myself!"? We all hate saying the same thing to someone again and again, but why is it that we tend to use the same block of code multiple times across our applications?

Today was an interesting day for me, as I got to observe bunch of talented developers speaking about how things are done and what should be concidered while doing it. One thing they discussed was DRY development method, in others words, Don't Repeat Yourself.

In other words, what they were talking about was the usage of copy/paste to generate whatever you are trying to generate. In order to develop maintainable code, one should never copy function from another place to have a certain outcome. The code should be written in such a way, that the information, or a process, is obtained from one single place and used where ever needed. If you need to somehow refactor this certain process, it is an easy task due to the fact that logic behind it needs to be refactored only in one place.

I have seen, and yes, used myself the bad habit of copying something from another place in order to get something accomplished fast. Later on, I noticed it wasn't the smartest way of approaching the problem, thus generating more refactoring to be done whenever time allows.

I knew how this problem exists but I never knew it has a term... until today.