I am a programmer and believe it or not, nothing satisfies me more than jumping straight into the code and hacking away. But I found that as the code base grew, it became easier to break logic / features in other parts of the application. I believe this can send a developer into a spiral of paranoia, not wanting to dive into code and be bold in refactoring. This is not healthy and can quickly bring about unmanageable code.
We need something we can trust, something to give us reassurance - to be that child in the code playground, knowing that our parents are watching over us, making sure we don't hurt ourselves or set fire to the swing.
Good points
TDD allows us to keep our modules safe from the unexpected. If I modify a methods implementation then I want to be sure that the function still produces the same expected output when I inject certain arguments.TDD also allows you to write awesome modularised code!
If you are writing unit-tests then ideally you should using TDD anyway. This means that you write your tests first so that you consider the design of your code.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | [TestMethod] public void RomanNumeral_8_Test() { // Arrange var expectedResult = "VIII"; var number = 8; var converter = new NumberConverter(); // Act var result = converter.ConvertToRomanNumeral(number); // Assert Assert.AreEqual(expectedResult, result); } |
I take this as far as instantiating objects and invoking methods in my unit tests before I have even created these classes. BECAUSE THAT'S JUST HOW I ROLL! Then using a few shortcuts I can create implementation in moments. This allows me to think of code in abstract modules; I can design my program in classes and methods and spend time coming up with good names.
A few days later, I realise that I could have implemented the roman numeral conversion using a dictionary instead of writing multiple conditions. TDD gives me the safety net to carry out this change without worrying about whether I have broken a module. The change is shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public class NumberConverter { private Dictionary<int,string> romanMap = new Dictionary<int, string>(); public NumberConverter() { romanMap.Add(1,"I"); romanMap.Add(2, "II"); romanMap.Add(3, "III"); romanMap.Add(4, "IV"); romanMap.Add(5, "V"); romanMap.Add(6, "VI"); romanMap.Add(7, "VII"); romanMap.Add(8, "VIII"); romanMap.Add(9, "IX"); } public string ConvertToRomanNumeral(int number) { if (number >= 10 || number <= 0) return ""; return romanMap[number]; } } |
If however you jump straight into the code (not thinking too much about modularisation) and write a set of classes which contain methods that have 30+ lines of code, then I am quite certain you will find difficulty in creating tests for this code. You will probably end up refactoring a few classes, breaking logic elsewhere and causing some serious trouble. Long story short - ALWAYS write your unit tests first.
Now that you have a completed set of unit tests for a dozen classes, You can be confident. You can dive into the code, refactor like a madman (or woman) and then run the unit tests to see if you have broken anything.
Bad(ish) points
However, TDD is not perfect and it does have it's problems. For example, you have written a large number of tests because you are a TDD boss and you have a large code base. However, for some reason you now need to undergo a large refactor. You do so and run the tests - 30 tests are now failing. Fixing these tests may take as long as the refactoring job itself and as a result it has doubled the amount of production time in producing this change.Some see this as bad, however, I see it as a small price to pay for writing SUPER-MAINTAINABLE code! Plus, if you didn't have these tests, you probably would have decided against the refactor job in the first place.
Another problem with writing unit tests (if not done properly) is that you have a large set of tests that can take up to 5+ minutes to execute. This is bad. Tests should execute almost instantly. You will probably find yourself having to run these tests very often, probably every time you commit your code into source control.
Don't let your unit tests damage your productivity!
Overall
TDD is great! It enables you to consider the design of your code first, your code becomes very modularised as it needs to be in order to be testable and it ensures you don't break stuff.However, TDD can eat into your productivity and slow you down. But I don't personally see this as a bad thing as we are writing super maintainable code that will ultimately save us time in the long run.
To sum up:
Good:
- Keeps modules safe from unexpected changes.
- Write modularised code.
- Design your code first.
Bad:
- Large refactor jobs may mean time spent fixing lots of tests.
- If tests take long to run, it may slow you down.
No comments:
Post a Comment