DEV Community

Touré Holder
Touré Holder

Posted on • Updated on

Does TDD help you to write more SOLID code?

It's clear to most that SOLID principles help us to produce more testable code and that unit testing promotes following SOLID principles.

I'm wondering if driving development through tests (TDD) helps you to follow SOLID principles any more that testing after development.

What do you think?

Top comments (3)

sargalias profile image
Spyros Argalias

I like the question :).

My view is that TDD and SOLID aren't directly related, but TDD helps SOLID.

TDD forces you to write small units of code, one at a time, that are easy to test. This means code that is simple and decoupled, which is in line with SOLID (specifically the single responsibility principle and dependency inversion).

Further, TDD forces you to think about the public API of the code, since that's what your test will be testing. This results in a much better public API than the alternative. The alternative being that you code whatever idea you have, repeatedly, until the thing works. In the end, if you don't refactor, you're left with whatever the result was, rather than something you deliberately designed to be easy to use.

So yes, TDD helps you write better code in general. That kind of code is what the SOLID principles also aim to help you write.

webbureaucrat profile image

I've never met anyone who thinks TDD "has no effect..." Like, yeah, it's still possible to write crumby code, but it's a LOT harder to do.

nestedsoftware profile image
Nested Software

While I believe TDD, when used effectively, does have value, it's quite easy to write poor quality code using TDD. An example: