r/Clojure • • Feb 21 '18

[ANN] Programming Clojure, 3rd edition

https://pragprog.com/book/shcloj3/programming-clojure-third-edition
76 Upvotes

17 comments sorted by

View all comments

3

u/xtreak Feb 21 '18

Thanks a lot for the updated release. Going from 1.3 to 1.9 is a pretty big leap. Apart from using specs for testing is using specs for validation in :pre and :post along with using it in if clauses a good practice? I saw a talk from Rich about not using it in production which is mostly with respect to instrumentation I think.

Also if I am library writer how can I allow the user to turn on instrumentation for my library functions during their development so that they get better error messages during testing. Does doing instrument-all turns on instrumentation for the required namespaces as well?

10

u/alexdmiller Feb 21 '18

Apart from using specs for testing is using specs for validation in :pre and :post along with using it in if clauses a good practice?

It is perfectly fine to use spec to validate data in production, if that meets your performance criteria. I would recommend that you isolate this kind of testing to only data coming from external sources, not between internal calls though and make those validation checks explicit with calls to s/valid? or s/conform. Or use s/assert to make the validation checks toggleable via flags. For this kind of use, I would probably not use pre/post checks - you're likely to want more control around the error handling if data is invalid.

Also if I am library writer how can I allow the user to turn on instrumentation for my library functions during their development so that they get better error messages during testing. Does doing instrument-all turns on instrumentation for the required namespaces as well?

If you're a library writer, you can choose to deliver function specs either in the same namespace as your code or in separate namespaces. The latter gives the user more control over whether and if those specs are loaded. They can choose to instrument your code by calling instrument at either the specific var, namespace (via enumerate-namespace) or for everything (by passing no args to instrument). It may make sense in some cases to provide a function specifically to enable instrumentation for the parts of your library that make sense.

3

u/dustingetz Feb 22 '18

Thanks, for everyone else here is some addl info on spec perf

https://groups.google.com/forum/#!msg/clojure/kLLvOdtPO_k/Pwtcn9KlCQAJ