-
Immutability Makes Everything it Touches Simpler
Read more: Immutability Makes Everything it Touches SimplerIt’s hard to write immutable objects. For example, it’s harder to write this: …than this: So why bother? Because immutability makes everything it touches simpler. Let’s look at an example that shows why. The Cache.ts file below stores values. It’s the .cache property in the HttpGateway.ts file. HttpGateway.ts caches requests to this.cache. Just skim these,…
-
Clojure Code Can Edit Clojure Code. Here’s Why That Matters.
Read more: Clojure Code Can Edit Clojure Code. Here’s Why That Matters.Clojure and Lisps can do something no other language can: Edit their own code. But JavaScript can create a string of JavaScript and eval() it. And JavaScript functions can return functions. So how is this different? This isn’t just generating code. Clojure can rewrite existing Clojure code. This can make Clojure easier to understand and…
-
No State Means Almost No Internal Dependency
Read more: No State Means Almost No Internal DependencyThanks to Yehonathan Sharvit and Bob Martin for this idea. State is why apps are so complex. When state is in multiple objects, you have to manage them: But what if there was no state, and instead… There was a single data map for the whole app? There’d be almost nothing to manage. Just functions…
-
Recursion: Start with the Base Case
Read more: Recursion: Start with the Base CaseThanks to Eric Grimson for this idea. When you’re writing a recursive function… Write the base case first. That’s where it returns a value, instead of recursing. To show this, let’s write a factorial function. A factorial is a “product of all positive integers less than or equal to n,” according to Wikipedia. The factorial…
-
Dependency Injection in Clojure
Read more: Dependency Injection in ClojureDependency Injection seems like an Object-oriented technique. Can you even do Dependency Injection in Clojure? Yes, you can do Dependency Injection in Clojure. Do you need a Dependency Injection library in Clojure? No, you don’t need a DI library. Instead of that… In a single composition root function, you can pass all of the dependencies…
-
Loop in Clojure
Read more: Loop in ClojureHere’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?
Read more: 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
Read more: Testing Problems Come From ArchitectureThanks to Logicroom for teaching me this. With testing problems, the fix is rarely in the tests. The fix is in the architecture. Most tests should be unit tests. With only some “integrated tests” (J.B. Rainsberger) at the edges. But usually, the system is too coupled to have unit tests. The tests make real REST…
-
Clojure Multimethods: The Open Closed Principle
Read more: Clojure Multimethods: The Open Closed PrincipleBertrand 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…