I Mock Your Mocks
blog.boot.dev
blog.boot.dev
I don't disagree with his decomposition, it's correct. I also agree with his approach to unit and integration testing to touch the appropriate layers. It's just his knee-jerk and unjustified hatred of mocks I'm objecting to.
I also think it's best to make sure the majority of our code doesn't require mocks to test. But very few people do that either even if it's fairly simple.
I am fairly fresh with my two years of professional experience and I've spent a lot of energy worrying about whether there's something wrong with me that makes me want to refactor most of the code I read. I'm starting to lean towards the conclusion that it's not my fault, it's just that most devs write shitty code.
And I don't think that's javas fault.
func validateUser(user User) error
This is a bad way to validate data. It generally means that any new data added to this type won't be validated by default. It isn't too bad because it is a typed native data structure (must worse in things like JS where extra fields can slip in from the JSON) but it is better security practice to parse the data rather than just looking at it. So it would look something like this func validateUser(user User) (error, User)
Then the function carefully extracts and validates the input before copying it to the output. Anything that it doesn't know about doesn't get copied. This can be thought of as "whitelist validation" rather than "blacklist validation".Parse, don't validate.
func validateUser(user UnvalidatedUser) (error, User)
this way, you can't accidentally forget to validate your user before using it in the rest of your code
Further, if you want to validate that your X is barlike, all you have to do now is make sure you have some function f : Y -> Z. If you want to scrutinize your validation logic, then you need only look at all functions of that specific type in your program.
Admittedly, go is probably not a language with the best support for these kinds of constructs.