DEV Community

Discussion on: TDD is Not for Me

Collapse
 
joshhadik profile image
Josh Hadik • Edited

I think TDD is a really good practice for beginners because 1) It gives you a better understanding of what to test and how to write those tests and 2) It forces you to learn how to break down large complex problems into smaller more manageable "blocks" of logic. #2 is more important.

Once you get good at those two things, I think TDD is just another tool in your toolbox, sometimes it makes sense, sometimes it doesn't. Sometimes TDD helps a lot by forcing you to write clean code and focus on one small problem at a time, and sometimes it's better to just get lost in the code for an hour or two to solve a problem without worrying about writing tests every five minutes.

Personally my goto code "process" looks like this:

  1. Start with BDD (which is basically TDD but instead of writing unit tests you write feature tests)
  2. Allow myself to get lost in writing the code for an hour or so till I pass the feature tests (usually the code is pretty sloppy and contains several "megafunctions" at this point)
  3. Write unit tests for the code I just wrote and use those unit tests to refactor the code into smaller more manageable functions (or "units".)
  4. Document the code, add comments, and focus on renaming functions and variables to make the code as readable as possible.

I find this process helps me a lot by allowing me to focus on different activities (which usually require a completely different mindset) for larger chunks of time, rather than shifting constantly between testing and programming. Instead I start by defining the scope of the problem I'm trying to solve (feature tests), then focus on programming and problem solving to pass the feature tests, then shift gears to unit testing and refactoring the code to make it cleaner, and finally focus on the documentation and readability of all the code I wrote. Then I write new feature tests for a different problem scope and rinse and repeat.