DEV Community

loading...

Does TDD help you to write more SOLID code?

Touré Holder
Yet another indie hacking second-career coder
Updated on ・1 min read

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?

Discussion (3)

Collapse
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.

Collapse
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: ravimohan.blogspot.com/2007/04/lea...

Collapse
webbureaucrat profile image
webbureaucrat

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.

Forem Open with the Forem app