DEV Community

Discussion on: Does TDD help you to write more SOLID code?

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.