+ 3
doIt(âdon\ât do itâ);
Just an ethical question is it correct to understand before calling a function if an action is to be performed or can it be done inside the same function that manages the action? Function doIt(val){ If(val == âdon\ât do itâ) return; return Val; }
3 RĂ©ponses
+ 8
I like the pattern where a function checks that its parameters are sane before proceeding, i.e., within range, meets process conditions, etc. This helps protect against certain types of exploits and makes your code more reusable. Internal function checks should -- in my opinion -- be something like a status return related to the request parameters: Bad request, not enough memory, file not found, success, etc.
If you can know ahead of time that the function should not be called ("status" = "why did you call me?"), I think you're just wasting a call.
However, there may be special use cases (like pluggable modules, generic/ dynamic / on-demand objects, guess-the-function games, ...) where it is not easy/reasonable to put the checks in your main code line (because that would involve injecting logic at runtime). Here, "wasted" calls may be a necessary evil.
Thoughts?
+ 6
If the entire function is not supposed to be performed, it should not be called.
If your function may throw exceptions, handle them appropriately using the exception handling features provided by the language.
+ 2
For performance reasons, I like what I call the "trash in -> trash out" principle.
If you want the right answer, ask the right question.