+ 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; }

5th May 2018, 3:50 PM
Alberto Cini - Noviia
Alberto Cini - Noviia - avatar
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?
5th May 2018, 4:24 PM
Kirk Schafer
Kirk Schafer - avatar
+ 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.
5th May 2018, 4:12 PM
Hatsy Rei
Hatsy Rei - avatar
+ 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.
5th May 2018, 4:39 PM
Timon Paßlick