I hunted a bug today that ultimately resulted from the following snippet somewhere in my code (where I was trying to filter for only "PRIMARY KEY" constraints in a list):
(filter #(= (% :constraint_type "PRIMARY KEY")) aListOfconstraints)
Instead of the correct:
(filter #(= (% :constraint_type) "PRIMARY KEY") aListOfconstraints)
I.e. the bug was the combined effect of a map taking a default argument in case the key is not found, as in:
({:a 1 :b 2} :a 0)
... and the equal functions accepting only one argument and returning true:
(= 1) ; evals to true
Is there any tool I could have used instead that would have prompted me to this sort of valid, but suspicious code? Or perhaps some best practice I am not aware of?
You can look to kibit - tool that tries to perform static analysis of clojure code, using rules built on top of core.logic. It should be not so hard to add new rules, but this tool also have limitations - see project's description.
The best practice you're looking for would be a unit test checking the proper behavior of your code.
A for the lint tool you could take a look at Eastwood, but in your case it would be difficult to provide a custom check, that could be used widely.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With