March
27

Why TDD Works For *Me*

Posted In: Design, TDD by rauchy

Having been involved in test-driven projects over the past 2 years, I found myself explaining the advantages and disadvantages that test-driven introduces to software projects to many people whom I taught, trained or coached.
I always stuck to the usual points, for instance, “design by usage”, “safety-net for your code”, “faster drilling to the heart of the problem”, “provides documentation for your code” etc. These are all great advantages for the project, and everyone in the project (including management) eventually enjoys them.

Lately I have been asking myself – why is TDD good for me? How do I profit from it? What’s in it for me? And most important: Why do I deliberately get myself into TDD adventures and even dare to say that I enjoy it?

Well, the answer is simple: Test Driven Development allows me to be an idiot, and keep getting paid for it.

When writing code, I face two tightly-related problems: Functional requirements (making sure my code does what it’s supposed to do) and design requirements (making sure the code is simple, efficient, coherent, extensible etc.).
Solving both these problems is a big headache, and I often find programmers failing to provide a solution for both of them. I often find poorly designed code which answers its requirements or well-designed code which does not.

TDD allows me to split this headache into two manageable problems.
Following the Red-Green-Refactor mantra carefully, I start with writing horrible, horrible code which answers the requirements. I continue making it all the way to the green stage and continue iterating until I have a hefty, sloppy method which answers all its requirements.
From that moment on, I do not need to worry about the functional requirements any more as my code is already obliged to solve them by unit tests, and any slight shift from that obligation will immediately come to my attention.
This is the part the real fun sets in. I have the freedom to design (actually, redesign) without worrying about breaking functionality. Like a lucid dream, you can do whatever you want, without the risk of getting hurt.

This way I can concentrate on each phase separately:
When answering requirements, I just have to make sure the code does what it is supposed to do, even at the price of code duplication, inefficiency and plain ol’ dumb code.
When refactoring, I have a well-set environment that allows me to apply design techniques without worrying about functionality.

This raises a question – can the role of the modern developer be split to two as well? A less-experienced developer will handle functionality and a more experienced developer will refactor?

If you liked this article, kick it on DotNetKicks.com !

3
March
23

Using Files In Unit Tests

Posted In: TDD by rauchy

One of the ten commandments in Unit Testing states “Thou shalt not access files during unit tests” (check it out here).
I can’t agree to that entirely, as I have been involved in a system whose main functionality dealt with files. In order to make that system unit tested, we had to permit file access for our unit tests. But these days I am working on a project which heavily relies on text analysis, and for this I find CommentReader extremely comfortable.
In a sentence, CommentReader allows you to use text from your test’s comment block inside your unit test. For example:

/// <input>
/// This is my input 
/// </input>
[ Test ] 
public void YourTestName() 
{ 
     string input = CommentReader.GetElement( “input” ); 
     // input == “This is my input” 
     ... 
} 

This way you can make sure your test resources goes along with your test (if you make sure to enable Xml comments, of course) and no other test.
I think its a very elegant solution, as it immediately states what the test’s intentions are. You don’t have to open Xml documents for that any more.

http://www.bestbrains.dk/dansk.aspx/Artikler/Using_Files_In_Unit_Tests

0
March
16

The article discusses the crooked way that the Singleton pattern is used in many software systems. I’ll admit it - I made this mistake many times.
If I have to choose one thing to take along from this article, it is that Singleton should not be applied freely on types which access is to be restricted to, but only for those rare cases which the type models a real world restricted object, like a class which encapsulates access to a configuration file or a queue.
Certainly, Singleton should not be applied to every stateless class, use static classes for that.

http://aabs.wordpress.com/2007/03/08/singleton-%e2%80%93-the-most-overused-pattern/

0
March
16

The Action Delegate

Posted In: C# by rauchy

Another post from Jeremy Jarrell. (the guy who posted the Predicate Object article)
This article briefly descirbes a way to execute custom code on every member of a collection using the Action delegate.

http://jeremyjarrell.com/archive/2007/03/08/13.aspx

0
March
6

The Predicate object

Posted In: C# by rauchy

Bet you didn’t know about this one.

This short article shows a nice way to perform custom filtering on arrays using a simple delegate. Search and replace.

http://www.jeremyjarrell.com/archive/2007/03/05/12.aspx

0