clojure-spec

Mark Wardle 2022-04-12T11:52:26.701739Z

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. Hill 2022-04-12T12:12:04.415979Z

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 Wardle 2022-04-12T12:31:43.206039Z

Thanks! That's helpful.

Colin P. Hill 2022-04-12T13:41:30.703559Z

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 Wardle 2022-04-12T13:42:25.617359Z

Thanks Colin. Makes sense!

seancorfield 2022-04-12T15:01:01.971619Z

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 Wardle 2022-04-12T15:03:46.500979Z

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

Colin P. Hill 2022-04-12T12:13:05.055959Z

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