Category: Functions

  • Immutability Makes Everything it Touches Simpler

    Immutability Makes Everything it Touches Simpler

    It’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,…

  • No State Means Almost No Internal Dependency

    No State Means Almost No Internal Dependency

    Thanks 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

    Recursion: Start with the Base Case

    Thanks 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…

  • 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…