TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Refactoring Clojure

94 pointsby luu4 days ago

12 comments

jlarocco3 days ago
Coming from Common Lisp, I find both versions are pretty hard to read, and I find Clojure in general difficult to grok. Every example of Clojure I see looks like the author is &quot;code golfing&quot;. Why the instistence on such terse, cryptic identifiers? Every other Lisp, going back to the 60s, uses readable text names, and it just baffles me that Clojure decided to go a different way.<p>The sentence generator in my Markov text generator CL package looked like this:<p><pre><code> (defun generate-random-sentence (mtab &amp;key (first (random-elt (first-words mtab)))) (with-slots (prob-table) mtab (format nil &quot;~{~a~^ ~}&quot; (loop :for next-word = first :then (pick-random-word (gethash next-word prob-table)) :while next-word :collecting next-word :until (ends-with-end-sentence-punctuation-p next-word)))))</code></pre>
评论 #44002478 未加载
评论 #44004020 未加载
评论 #44001820 未加载
roxolotl3 days ago
I’ve been writing a lot of functional lisp lately for the first time and one thing that’s struck me is how easy it is to refactor. Because a form can always be reduced to its result, you can pluck them out, move them around, and evaluate them at any level of the tree. While this is true of functional programming in general the structure of lisp makes its super easy in a way it’s not in a Haskell or just functional JavaScript.
评论 #44001408 未加载
评论 #44001559 未加载
评论 #44002722 未加载
noodletheworld3 days ago
Seems fair, but refactoring is generally tricky and:<p>1) replacing a func with a simpler <i>recursive</i> func may or may not be ideal.<p>2) this fills me with no joy when I read it:<p>&gt; It seems easier to start from scratch as opposed to refactor it. The characterization tests above will help ensuring that the returned hash map stays the same.<p>Its enormously hmm… what word to use. Ambitous? Foolish? Brave?<p>Anyway, taking code, writing some tests for it and then <i>rewriting the entire thing</i> so it passes the tests you wrote only barely falls into “refactoring”.<p>If anyone refactored my code base by simply rewriting from scratch the parts they dont like, I would be upset.<p>Rewriting things is hard, and its … brave… to think you can just write a few tests and then if they all pass its all good!<p>That only works in very trivial examples, and really, doesnt show case the strengths of clojure at all.
评论 #44001143 未加载
gleenn4 days ago
In general, it&#x27;s a dream to test and refactor Clojure IMHO. Working with mostly static top-level functions, immutability by default, and mocking when necessary with the use of &quot;with-redefs&quot; makes writing tests easy. Also, the immutability makes it hard to break shared copies of memory which seemed to plague my earlier tangos with large Java code bases and standard OOP practices.
评论 #43999778 未加载
NightMKoder3 days ago
Usually the controversial decision for Clojure code highlighting is rainbow parens. This color scheme is horrific and unreadable (on mobile at least).
评论 #44000200 未加载
0_gravitas3 days ago
First few comments are nothing but shallow {rem,sn}arks on the formatting of the site, by users that don&#x27;t even have the excuse of being recent joiners. If this is how we lead by example then it&#x27;s no wonder why the quality of content+comments on this site are (imo) on the decline.
评论 #44000738 未加载
michaelteter3 days ago
The first thing I would do would be to take some of the inner forms and turn them into functions (single responsiblity). This isn&#x27;t even Clojure-specific.<p>Lots of little, simple functions, composed... You can still use the threading at a higher function, but instead of doing the actual work at each step in the pipeline, you call a well-named function which does what that step was doing. Yes, it may feel redundant, but it absolutely improves human comprehension because it reads like clear human instructions.<p>Tests become much simpler too.<p>The only two challenges I face with this approach is 1 - naming things, and 2 - organizing. The function names tend to become quite long, since they ideally describe what the function does.<p>I haven&#x27;t done this lately, now that the AI tools have become pretty great; I can imagine that an AI tool would be an excellent knowledge base for these many functions, and it would probably be very successful in providing additions or modifications given the clean, clear function names and brief contents.
评论 #44001268 未加载
lenkite2 days ago
The original code was very readable to me - after I copy-pasted it into a text editor without all the jazzing colors and horribly low contrast.
billmcneale3 days ago
&gt; Our mission is to take this code and make it readable:<p>Anyone else finding some irony in the fact that the provided code is unreadable not because of its structure but because of the highlight colors?
IceDane4 days ago
It&#x27;s kind of funny that this article starts by showing a completely unreadable code snippet, but not because of the code, but because of the syntax highlighting scheme. There is no version of that code, or any code for that matter, that is readable using that color scheme.
评论 #44001154 未加载
leelou23 days ago
This article hits on something I&#x27;ve been wrestling with in our codebase. The absence of static typing in Clojure makes refactoring feel like walking a tightrope without a safety net sometimes. I&#x27;ve found that building a solid test suite before any major refactoring is absolutely critical - it&#x27;s the only reliable way to ensure you haven&#x27;t broken anything when reorganizing code. What&#x27;s worked well for us is treating functions as the primary refactoring unit rather than trying to impose OO-style refactoring patterns. Breaking down large functions into smaller, well-named pure functions has provided the most bang for our buck. We&#x27;ve also found that leveraging spec for runtime validation gives us some of the safety of types without giving up Clojure&#x27;s flexibility. The tooling situation has improved significantly too. Cursive&#x27;s refactoring tools have been solid, and I&#x27;d be curious if others have had success with the newer REPL-integrated refactoring approaches mentioned in the article. Has anyone managed to set up effective continuous integration that catches runtime errors in untested paths?
arijun4 days ago
&gt; Our mission is to take this code and make it readable<p>You failed. Between the unreadable text colors and the word wrap, the code is incomprehensible. I cut and pasted it into a plaintext notes app and it was way easier to understand