6 ms·
Quick question: is it enough to create guards/checks in languages like Ruby or Javascript to make sure that a parameter that is passed in is the type you expect
by rajangdavis 9y ago
Quick question: is it enough to create guards/checks in languages like Ruby or Javascript to make sure that a parameter that is passed in is the type you expect? I try to set default values in the method/function declarations to at least return an empty set if I can live with it.
Only time I have had a dynamic language do weird things is with PHP, but that was due to my lack of understanding of how PHP handles empty strings.
- always_good 9y agoOne problem with runtime checks is that they of course only work at runtime but you also must manually assert against them. A bulk of your test suite now exists just to execute runtime paths through your code to trigger your guards at all. Then your tests often have to duplicate the very assertions that you already specified in your guards. Another problem is that your runtime guards are often ad-hoc and can drift from your data model, turning into debt that you need to manually reevaluate and clean up. In my experience, there's nothing quite so permanent as a defensive nil guard that everyone is too timid to remove. But it's not clear what you mean by "is it enough". I definitely use runtime asserts in dynamically-typed languages like Javascript to try and fail as fast as possible in critical functions.
- rajangdavis 9y agoI meant "enough" as to ask "do I need to do more than writing a check?". You do bring up a good point with testing (which I don't always do, but I try to at least do runtime checks). I follow you on the data model part. I think that's where a test framework is super handy.