Is it better to control variables and parameter “only” (mostly) in the upper areas of the call hierarchy
or
to save each class individually against wrong parameters, because you don’t know who may call it in the future.
I am in the moment a little ambivalent. But i tend to write some more ifs than leave classes unprotected.
What do you think?
If your method is part of the public interface of a class (either public or protected) then you cannot trust the parameters that you are receiving, and therefore you should validate them. On the other hand if the method is private, you are in total control of the arguments you are passing and if you are sure you never pass invalid data then you can safely assume that they are right all the time and avoid any code to validate them.
Now, what exactly is part of the public interface of your system may not be evident to anyone but yourself. Even when a class has public methods, the class where they are declared is totally encapsulated and inaccessible to other users, it may may be impossible to extend it or use it outside a given context, in those cases, your level of encapsulation could help you decide if you are truly in control of what is being passed as parameter and then you can decide if it is safer to validate the data or not in a given context. But in the moment you believe you can lose sight of what is being passed as parameter, then you should validate it, IMHO.