DEV Community

Discussion on: Do you even unit test, bro ?

Collapse
 
bootcode profile image
Robin Palotai

TDD aside, the reason why we do or don't write unit tests depends on our experience.

Novice developers don't write tests since they believe their code is correct. Or they don't care if their code is correct. Or they don't know they should care. When they face enough edge cases and production bugs, they start to appreciate them.

I'll run a learning to drive analogy - novice developers are like driving students, bumping into stuff on the crash course every now and then.

Somewhat experienced developers are like drivers with a fresh licence - best practices still afresh, taking good care. Writing unit tests and all. Did you know that fresh drivers have low accident rate, until after 2 years in?

Experienced developers have confidence and don't fall into pits that often. Those 2 years have passed, and they are getting a bit reckless. Why write tests if I really trust my code? So they don't, and everything works fine for a while.

Until everything gets super-messy. Then they learn the second purpose of testing: to prevent code that was once working to be ruined.

Without testing, a future developer (maybe your future self) will inevitably change some detail that alters previous functionality. This might stay hidden for a while, only to be twice as surprising when the fireworks erupt.

(If you would like to read more like this, follow me here or my handbook Programming Without Anxiety, which will have a section about testing as well)

Takeaway: if you leave a codepath untested, you are willing to accept its functionality changing without notice in the future, and willing to bear the costs of debugging.

Collapse
 
juyn profile image
Xavier Dubois 🇫🇷

Can't agree more, I like the driver analogy.
I'll just add that developers don't write unit tests until they work in a team.
Until then, you can have control on the base code. It will be fine, unless you forgot something, but, hey, that's not that often.
But, when a coworker mess with your code and change how it behave, unit test is the only way to keep controle