Ok,this maybe too nerdy of a topic for here but that's why I love unit tests.
Basically I write a piece of code that gets this input and generated that output. I also make a test to verify that I get a certain output given a certain input.
Now if I spend all day futzing within that code , changing variable names, refactor and extract a large function to 10 small ones, decide to re-write all the SQL queries to linq arguments...I can fuck up and tests may fail. I fix the failing code to still pass the test. I know I delivered code that met the requirements, hopefully improved it, but I know I didn't fuck it up enough to not do what it's expected to do.
Plus source control...I mess around with code, my tests all pass...I commit it...I mess around more, can't get the tests to pass, oh well quitting time roll back to previous working commit. Boss may be mad I didn't improve it but at least I didn't break it. Zero gain day is better than negative gain...