Click here to Skip to main content
15,881,715 members
Articles / All Topics

Testable Code vs Clean Code

Rate me:
Please Sign up or sign in to vote.
4.89/5 (10 votes)
7 Jul 2015CPOL2 min read 22.1K   4   4
Testable code versus clean code

clean code

I wrote my first blog article 1.5 years ago, and it was about how TDD made me a happy developer. In that article, I talked about how TDD helps me write clean code, refactor code without breaking it, decrease debugging time and improve my career.

I’m still a Test Driven Developer, but when I wrote the first post, I thought that clean code is testable code, and vice versa. However, I don’t think anymore that these two are the same thing. In my first days of TDD, my goal was to test everything so I could have a high test coverage, now I know this is wrong, but back then, I was really happy to see that my code coverage was over 95%. The problem is that in order to reach a high coverage like this, you have to make sure everything is testable, so I ended up creating interfaces for almost every class that I used, in order to be able to test the code.

Let me give you a better example of what I’m trying to say. If you remember, I made a few articles about doing TDD with Windows Phone, and I said that in the test project I can’t reference any Windows Phone libraries in order to be able to test it, because my test project was a simple class library, not a Windows Phone Test Project. If you have a method that calls a phone number, you would normally do something like this:

C#
public void CallUser(User user)
{
    PhoneCallTask phoneCallTask = new PhoneCallTask();

    phoneCallTask.PhoneNumber = user.PhoneNumber;
    phoneCallTask.DisplayName = user.FullName;

    phoneCallTask.Show();
}

But I couldn’t do it because I can’t test the method, so I have to create a wrapper for PhoneCallTask and add it in my ViewModel through constructor injection.

C#
private readonly IPhoneDialer _phoneDialer;
class MyViewModel
{
    private readonly IPhoneDialer _phoneDialer;

    public MyViewModel(IPhoneDialer phoneDialer)
    {
       _phoneDialer = phoneDialer;
    }

    public void CallUser(User user)
    {
       _phoneDialer.Call(user.PhoneNumber, user.FullName);
    }
}

The code is shorter, but it’s a lot of extra work that doesn’t add any real benefit. In my example, creating a wrapper for PhoneCallTask could be useful in the future, because I’m not dependent on the Windows Phone SDK anymore. If they decide to change the way of calling someone, I just have to change the implementation, not the interface, but the chances for this to happen are very slim, so if you’re not going to need it now, don’t do it.

Clean code has its benefits, it will make your code easier to understand for someone else, and it will make it easier to change and refactor. But testable code will make your product more reliable, so you shouldn’t chose between these two, but find a balance between them. If you want only clean code, I say you should write the tests around the code, and if the tests are more important for you, then you should focus on making it testable. If there’s one thing that I learned as a programmer is that there’s no “42” in software development. Stop thinking that all the code should be clean or that all the code should be testable, there’s no such thing.

The post Testable code vs clean code appeared first on Bogdan Bujdea.
This article was originally posted at http://thewindev.net/testable-code-vs-clean-code

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Feel IT
Romania Romania
Hey! I'm Bogdan Bujdea, a software developer from Romania. I started to learn programming since my first year of highschool with C++. During my time in college I tried various languages like Java, C#, Haskell, Javascript, Python, etc. but my favorite remains C#.
I started my career with Thinslices as a Windows 8 developer, but now I work for Feel IT Services as a Windows Phone developer.

Comments and Discussions

 
GeneralNice concise article, but I think you might have been right first time :0) Pin
OneWinsto9-Jul-15 1:28
OneWinsto9-Jul-15 1:28 
Firstly thanks for writing the article, it is concise and illustrates your point clearly.

However I think you were right the first time; clean code IS testable code Blush | :O )

To clarify, it depends what you mean by 'clean'; but I think most people would think of the practices advocated by the likes of clean coders, Bob Martin et. al. and following SOLID principles.

In your first code sample your view model has a dependency on the concrete PhoneCallTask; this is a violation of the Dependency Inversion Principle. Your re-factor changes the dependency to be on an abstraction. I realise you do point all this out in your article, but my point is that the second (more testable code) is also more 'clean'; at least with respect to the references I gave above.

You are correct that there is always a trade off and at some point we 'draw the line' on how far we keep things loosely coupled. However I think that is the trade off; when do I stop with the 'clean' and loosely coupled not 'clean' vs 'testable'
GeneralRe: Nice concise article, but I think you might have been right first time :0) Pin
Bogdan Bujdea11-Jul-15 10:23
Bogdan Bujdea11-Jul-15 10:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.