r/Clojure • • Oct 02 '15

Writing friendlier Clojure

http://adambard.com/blog/write-friendlier-clojure/
79 Upvotes

5 comments sorted by

14

u/[deleted] Oct 02 '15

I'm very happy to see a post like this! I've been waiting for one for a long time.

As counter-intuitive as it is, I think it's a problem in the Clojure community that it's made up of a lot of smart people. Their biggest flaw being that they're too smart to fail to understand their own spaghetti. Failing to see that two months down the line, when they (or a coworker) needs to fix a "small" bug, it'll take them three times longer for no reason.

This is how legacy code is written.

Java might be verbose (and believe me, I'm not a fan either) but they got something right with the emphasis on design patterns and clean code. We could all learn from that.

Programming is a social endeavor. Even if you are the only person who reads your code. Ever wrote a function to, six months later, not understand a single thing about it? Ever read a function by a coworker and wondered WTF he was thinking?

Writing a function that is more than 20 lines long is rude. We should all be mindful of that.

OP, keep em coming :)

4

u/hu6Bi5To Oct 03 '15

Indeed.

I've been shouted down in here and /r/programming before now for saying this, but it's both: a) relevant, and b) a critical issue for the sustainability of Clojure. Clojure is at the stage of development where it attracts the "special snowflake" (also seen in Ruby between 2005-2010), who tend to reject any readability criticism as a completely unreasonable attack of their individual awesomeness.

It's also a bit of self-delusion to think this spaghetti code is caused by being "too smart" either. Making things understandable is, to be fair, very difficult; it's more that they're not smart enough to make the intention of their code clear.

And it should be clear. All the advanced high-level features of Clojure should make things more obvious, not less. If you ever find yourself saying "well maybe you're not skilled enough to understand it" you have taken a very wrong path.

Disclaimer to the above: this is obviously when referenced against an appropriate benchmark, a brand new programmer will be confused anyway, and some application domains require specialist knowledge.

The way to tell the difference is: can everyone in your team understand what everyone else in your team is doing?

5

u/skratlo Oct 02 '15

Well done refactoring. Thank you

3

u/CurtainDog Oct 03 '15

It's hard. I still don't like in the final refactoring that Markov data calls make Markov mapping calls sentence to word list. That kind of nesting suggests a failure of composition (or perhaps composability).

The core issue in my opinion is that core clojure lacks a good data structure to represent the processed text. If we had a weighted directed graph to represent the data then the functions would fall out more naturally.

0

u/[deleted] Oct 02 '15

[deleted]

2

u/TweetsInCommentsBot Oct 02 '15

@martinfowler

2015-10-01 18:33 UTC

Example of refactoring Clojure, extracting into functions, using threading ops (collection pipelines) by @adambard

http://adambard.com/blog/write-friendlier-clojure/


This message was created by a bot

[Contact ^creator][Source ^code]