• Loop in Clojure

    Loop in Clojure

    Here’s a way to avoid recursive helper functions… By using loop in Clojure. Sometimes recursive helper functions seem needed, like fibonacci-iter: (I ported this to Clojure from Structure and Interpretation of Computer Programs, licensed CC BY-SA-4.0) That fibonacci-iter helper function is only needed for recursion. For example, the public (defn fibonacci [n] only needs an…

  • When Does A Function Do 1 Thing?

    When Does A Function Do 1 Thing?

    Functions should do one thing. They should do it well. They should do it only. Robert C. Martin in Clean Code You probably agree, but what’s 1 thing? What about the core.clj file’s -main function? That does almost everything in the app.  Definitely more than 1 thing, right? Here’s how you know if it does…

  • Testing Problems Come From Architecture

    Testing Problems Come From Architecture

    Thanks to Logicroom for teaching me this. With testing problems, the fix is rarely in the tests. The fix is in the architecture. As J.B. Rainsberger said, unit tests are the best. With only a few integration tests at the edge. But usually, the system is too coupled to have unit tests. The tests write…

  • Clojure Multimethods: The Open Closed Principle

    Clojure Multimethods: The Open Closed Principle

    Bertrand Meyer’s Open Closed Principle states that adding new behavior shouldn’t change existing code. It should only extend code. Clojure has a great form for that: The Multimethod For example, here’s a data store similar to Redis®. You can run commands like: That works fine. But now, we want to implement an “ECHO” command: And…