Why I dont use strict TDD

Test Driven Development (aka TDD) is a very useful practice that helps to reduce the number of bugs in production code. However, I do not utilize strict TDD as most others do. String TDD is all about excessive positive re-enforcement and also encourages writing useless code.

TDD dictates that, before you write any production code you need to write (and run) a test for the new functionality you are adding. This structure means any code you add will need to pass all previous tests. Fixing one bug, but breaking the app in numerous other places (for the most part)... just goes away. This is one of the main reasons to use TDD.

The two rules of TDD are:
1. Write tests before production code
2. Write the minimal code required to make the test pass

1. If you follow TDD practices strictly, you will run ALL tests immediately after creating them and before adding any production code that they would need in order to pass. Pretty sure I know the status of EVERY SINGLE initial test run. FAILED!

I'm all for positive reinforcement, but this seems excessive.

2. TDD also dictates that you must write the minimal amount of code required to make the test pass.

Consider the following example:
Problem: You need to get a list of items from the database. This could return 0, 1, or more items.

TDD dictates that we Write the Test first:

function test_one() {
list = array();
list = data_class:get_data();
assert(list.length, 0);
}

This test assumes we are returning a list of items. However, we have not added any data so the list should be empty and have a length of 0.

The minimal amount of production code according to strict TDD would most likely look something like:

class data_class {
function get_data() {
return [];
}
}

Except that this is completely a completely useless function! If anyone actually put this code into production they would have to almost immediately change it to something more meaningful.

I would consider the following to be the minimal amount of code required to pass the test:


class data_class {
function get_data() {
return db->get_from_table('table_name')->where('column', '=', 'value');
}
}

This code actually checks the database for the rows you are looking for and returns them. If there is data, you will get more than one row. If there is no data you will get no rows. It could actually be used in production. It will also return no rows when there is no data, but more importantly will return rows when data is available.

In summary, I really like the robustness that TDD brings to apps that are built under its guidelines. However, TDD uses excessive amounts of positive re-enforcement and also encourages writing useless and meaningless code. For these reasons, I do not practice strict TDD.

If you have arguments for or against TDD, I'd love to hear them. Please comment below.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Categories: