November 26, 2018
I’m probably going to get this wrong.
Both Test First Programming (TFP) and Test Dirven Development (TDD) start with writing a test.
From what I can interpret from various sources on the internet, c2-wiki,stack-overflow,pythontesting.
TDD and TFP rely on the test/s being written out before hand, it’s possible this is the primary diffidence. With TFP the tests would be the entire acceptance / unit test, basically the specification of what the code has to do are written out. While with TDD the unit test is written more progressively as the code grows, after many small test and units that pass the tests are written the criteria of the specification if fulfilled.
Not the best wording, so I’ll show the difference.
Specification: When the first and last characters of the first parameter match those of the second return true.
// This covers the specification TFP style
describe("[TFP] First and last letters are the same", () => {
it("Returns true when first and last letters are the same", ...);
it("return false when the first and last letters are different", ...);
// now write the code
});
function tfp(firstWord, secondWord) {
const first_letter_of_first_word = firstWord[0];
const last_letter_of_first_word = firstWord[firstWord.length - 1];
const first_letter_of_second_word = secondWord[0];
const last_letter_of_second_word = secondWord[secondWord.length - 1];
return (first_letter_of_first_word === first_letter_of_second_word) && (last_letter_of_first_word === last_letter_of_second_word);
}
describe("[TDD] First and last letters are the same", () => {
it("When words are the same: return true", ...); // some code is written.
it("When start and end letters are different return false", ...) // another line is written;
it("When start and end letters are the same, but the last letters are not the same return false", ...); // another line is written
it("When both start and end letters are the same, return true", ...) // final line
});
function tdd(firstWord, secondWord) {
if (firstWord === secondWord) { return true; }
if (firstWord[0] !== secondWord[0]) { return false; }
if (firstWord[firstWord.length -1] !== secondWord[secondWord.length -1]) { return false; }
return true;
}
In the tfp example, the way in retrieval of the first and last letters is repetitive and could be modularised in to a function that returns just those letters. While the tdd example this would be an unnecessary step.
Variable arguments in c, and debugging mistakes I made when using them, and started reading the clean coder. I’m only up to the second chapter in the clean coder and I’ve already got half a dozen stick-notes stuck to the pages to highlight sections of interest.
Written by Marc McIntosh Find him on github