Clean Code

105 views
Skip to first unread message

João Paulo Souza Soares

unread,
Nov 12, 2018, 5:35:31 PM11/12/18
to 4Clojure
I'm going through the exercises and looking for other's solutions to learn better approachs and more about the most used functions. What is bothering me is that I'm following a lot of people and I don't see mantainable code; code that one would commit in production; that's simple, easy and fast to understand. I see that the site value more the small code (code golf) than readablility.

So I would like to know good references and examples of Clean Clojure Code, it would be perfect if you know some user with solutions like that.

Leif

unread,
Nov 16, 2018, 3:36:37 PM11/16/18
to 4Clojure
Hi, João.

I agree that it's unfortunate that the code-golf solutions aren't separated from the others.

Luckily, I think that the code-golf solutions are actually close to the ones you would see in production code (minus the helper function inlining and one-letter variables).  The reason I say that is because using the built-in seq functions instead of explicit recursion usually leads to shorter code.

I would recommend following some people who write very functional code, step through their solutions to see what each expression does, refactor their code to break out helper functions, explicitly name some intermediate values (maybe), and rename variables to be explanatory.  I think the result would be close to good production code.  I recommend the users chouser, cgrand, and gajomi.

I (leifp) tried to keep my code clear and explanatory, but I wrote most of it when I was learning clojure, so a lot of it is too low-level to be good production code (a warning sign is the use of loop, lazy-seq, cons).  Still, if each helper function was rewritten at a higher level of abstraction, it is probably how I would write production code.  So it might help you.

The real place to find good production code is widely-used open source projects.  I would still recommend chouser and cgrand (but on Github now) as well as ztellman, weavejester, and ptaoussanis.  And the clojure.core / Cognitect team, of course.

Hope that helps,
Leif

João Paulo Soares

unread,
Nov 17, 2018, 11:42:02 AM11/17/18
to 4clo...@googlegroups.com
Thank you for the reply, I'll definitely check the users and the repositories :) 

I just didn't understand very well your comment about loop, lazy-seq and cons. Are saying to abstract them in helper functions to make it cleaner to production code?



--
You received this message because you are subscribed to the Google Groups "4Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to 4clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Leif

unread,
Nov 17, 2018, 5:32:48 PM11/17/18
to 4Clojure

When I said “helper functions,” I just meant “user-written functions.” I call them helper functions because the solution to each 4clojure exercise is one function. Sorry for the confusion.

Using loop etc. are a sign that you are writing low-level sequence manipulating code that probably can be replaced by one or two clojure.core functions. E.g.

solution by beginner used to imperative programming:

(fn solution [coll]
  (Iet-fn [(filter-out-nils [xs]
                (loop [res [] xs xs]
                  (if (empty? xs) ; done
                    res
                    (let [[x & r] xs]
                      (if (nil? x)
                        (recur res r) ; don't want nils
                        (recur (conj res x) r))))))] ; add x to end of result, recur on rest
    ... (filter-out-nils coll) ...))

solution once you know more clojure:

(fn solution [coll]
  ... (remove nil? coll) ...)

Sometimes an exercise makes you re-implement a core function using loop, lazy-seq, etc, but otherwise they are not usually needed.

Bill Allen

unread,
Nov 17, 2018, 9:10:31 PM11/17/18
to 4clo...@googlegroups.com
I'll pitch into this thread. João, you could do a lot worse than look at Leif's solutions to the problems. I know that's what I did when I was doing them. Come up with your own solution and follow Leif and a few others to check your's agains theirs. It's better than a course would be.
Message has been deleted
Message has been deleted

Alexey Perekhodov

unread,
Jan 9, 2020, 11:44:55 AM1/9/20
to 4Clojure
I would recommend jafingerhut's solutions. Besides understandable code they also contain good comments.
And amalloy's (one of the site founders) solutions may be noted, I suppose.
Reply all
Reply to author
Forward
0 new messages