Fork me on GitHub
#architecture
<
2017-03-13
>
donaldball20:03:33

Does anyone else sometimes write protocols even when there is only one implementation? I sometimes find value in using the abstractions to write the calling code at a higher-level, without some of the incidental complexity obscuring the intent. But I’ve heard the critique a few times that protocols should have at least 2 impls to justify their weight.

seancorfield20:03:26

I have done that in a few cases — like you, where I felt the clarity of the protocol helped understanding of the code — but I would consider it a rare edge case rather than best practice.

donaldball20:03:11

The case I’m contemplating now is one where I had a mess of side-effecty code that was doing something like 4 different business jobs and I could not find a satisfying organization… until I went for a walk, realized I could express these as 4 different protocols, and it decomposed nicely.

donaldball20:03:03

I have had similar experiences when working with larger clojure codebases. At a certain point, the organizational benefit of protocols to clarify responsibilities carries some real weight, at least to me.

seancorfield20:03:49

I think @stuartsierra advocates for protocols as a code organization tool — see his Clojure in the Large talk?

donaldball20:03:35

I have, though it’s been a while. I think I find the argument more convincing now. 🙂

stuartsierra20:03:12

Don't make protocols with only one implementation.

donaldball21:03:18

Ha ha asked and answered

donaldball21:03:39

I’m having a hard time reconciling that advice with the design experience I’ve had a few times now tho

hiredman21:03:45

there are two ways to take that

hiredman21:03:20

one way would be, if you have only one implementation, write more

donaldball21:03:06

I think my pushback would be: 1. while it would now be fairly easy to remove the protocols and just keep the organization confined by namespace, I (apparently!) could not have gotten there without the design aid of the protocols

stuartsierra21:03:01

A protocol with only one implementation is just needless indirection which increases the effort required to read and understand your code.

donaldball21:03:19

and 2. the readability of the highest-level of code is, to me at least, significantly aided by having components named by role interacting, instead of the incidental clutter of values that would otherwise have been closed over by the impl

stuartsierra21:03:35

You can still treat the component as a logical unit.

donaldball21:03:11

Possible I’m just an outlier, or maybe protocols are simply for me a useful intermediate step

metametadata22:03:09

I often use protocols even with single implementations because protocol instances are easy to stub/mock in unit tests with smt. like clj-fakes: https://metametadata.github.io/clj-fakes/user-guide/#protocol-fakes

seancorfield22:03:20

@stuartsierra In your Clojure in the Large talk you advocated protocols, as I recall, but I think that was in conjunction with an assumption that you would also be defining mock components? Am I remembering incorrectly?

seancorfield22:03:06

I used protocols in the design of one of my libraries, as it helped clarify the API design — but I refactored the protocols away after using it for a while and establishing where there were likely to be multiple implementations (kept protocols) and where there weren’t (removed protocols). I did find protocols helpful to articulate my design tho’.

seancorfield22:03:20

(so that puts me somewhat in @donaldball’s camp)

stuartsierra23:03:09

Yes, if you have mocked versions of your components, then you have more than one implementation, so the protocol serves a purpose.