DCSIMG
Testable code – is it worth it? - Nati Dobkin
Testable code – is it worth it?

 

Recently I been refactoring some legasy code and writing a new one. I am using Rhino mocks to run my units and for that I have to write my code in a testable way. This approach makes my code looks not exactly the way I designed and want it to be. Eli Lopian wrote an article about this issue – "Stop Designing for Testability" and has some points to think of.

In my opinion, if we have to write very simple code to encapsulate some internal logic for our base class and make sure that there is no way some smart ass will override it, but still give an opportunity to reuse this code in the deriving class, like this:

public abstract class MyBase
{
    protected void DoSomeVeryImportentLogicThatCanotBeOverriden()
    {
        DoTheSameVeryImportentLogic();
    }

    internal virtual void DoTheSameVeryImportentLogic()
    {
        // logic goes there.
    }
}

Only to make the code testeble – we are missing something – this is not the way code should look like.

There is also the impact on performance of making all our methods as virtual, to be able to override them when we make out mock classes, I agree with Roy Osherove one this on and we are not dealing here with real-time applications so the impact of the vtable that created when we are performing polymorphism (you can read about it from Shimon's post) is not so painful. And as Roy explained in his post if this is your pain in the ass you can deal with it.

I don’t really like the idea of losing the concept of OOP and clear and simple code writing over testable code.

What do you think?

Published Saturday, November 17, 2007 11:31 PM by Nati Dobkin
תגים:

Comments

# re: Testable code – is it worth it?@ Monday, November 19, 2007 7:35 PM

I think that you can design for testability without losing anything, in fact, quite the opposite - you will gain a lot.

See Ayende's post on the manner:

ayende.com/.../The-Production-Value-of-Seams.aspx

In the specific case you showed - are you sure you can't test against other public/protected virtual methods that are in this class? Is there a really good reason for that method not to be virtual?

by dorony

# re: Testable code – is it worth it?@ Tuesday, November 20, 2007 12:15 AM

hi doron.

there is alot of bad things in making all of your methods virtual.

a. performance hit.

b. poor OOP design

c. that is just ugly :)

i've read the ayende's post, and i still does not agree with him.

please read these posts about this matter .

www.elilopian.com/.../design-and-testability-yagni

www.krokhmal.com/.../TDDApproachDoesRedefiningApplicationPurposeNecessary.aspx

# re: Testable code – is it worth it?@ Tuesday, November 20, 2007 11:45 PM

Hi Doron, I agree with you that thinking about making your code testable can sometimes improve your OO design but only sometimes. I really can't see how the design can be proven by changing every private method to internal virtual and extract Interfaces from classes that implements very basic logic which used maybe once or twice in your application and your application only.

I don’t mantling that you can test static members which, as I see it lately, all become single tones. And what the answer that I get when I ask the person who did it "what happened to put good friend 'static'" – that it can be tested. You call it good design?!

Shimon – great post, agree with every word.

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: