Fork me on GitHub
#clojure-spec
<
2022-04-12
>
Mark Wardle11:04:26

Realise this will be mostly a matter of opinion, but where do you place your function specifications? I tried them in a dedicated specs namespace, but I'm preferring them just above the function definition. Are there any disadvantages to keeping the function spec and the function together in the same namespace?

Colin P. Hill12:04:04

I strongly prefer colocating them with the functions. Tends to make the namespacing more meaningful and communicative, and (as a fringe benefit) means you're jumping around less when you change things.

Mark Wardle12:04:43

Thanks! That's helpful.

Colin P. Hill13:04:30

The only disadvantage that comes to mind is that it might lead to one namespace owning a spec which is later revealed to most properly belong elsewhere, perhaps in a more general-purpose namespace – but this is just a specific case of the general unsolved problem of changing knowledge and needs. It can also be worked around by moving the spec to the "right" place, leaving behind an s/def in the old place that just aliases the spec under the old name, and documenting the latter as deprecated.

Mark Wardle13:04:25

Thanks Colin. Makes sense!

seancorfield15:04:01

Another "vote" for having function specs directly above the function they describe. We use separate .specs namespaces for data specs. I talk a bit about that in https://corfield.org/blog/2019/09/13/using-spec/

👍 1
Mark Wardle15:04:46

Thanks Sean. That's a helpful blog post thank you. All makes sense!

Colin P. Hill12:04:05

If there are some specs used by a number of functions, I'll sometimes just put those at the top of the file rather than immediately before some particular function.

👍 1