Comment by shawnz
Comment by shawnz 2 months ago
Sometimes I like to put the conditional logic in the callee because it prevents the caller from doing things in the wrong order by accident.
Like for example, if you want to make an idempotent operation, you might first check if the thing has been done already and if not, then do it.
If you push that conditional out to the caller, now every caller of your function has to individually make sure they call it in the right way to get a guarantee of idempotency and you can't abstract that guarantee for them. How do you deal with that kind of thing when applying this philosophy?
Another example might be if you want to execute a sequence of checks before doing an operation within a database transaction. How do you apply this philosophy while keeping the checks within the transaction boundary?
Maybe write the functions without the checks, then have wrapper functions that just do the checks and then call the internal function?