Jim Lynch Codes
  • Blog
  • About Jim

Writings about one coder's stories & experiences.

Clojure Namespaces: Modularity Without The Class-Based Hooplah!

12/16/2017

0 Comments

 
I grew up on "Algol" style languages like Java, C++, and ActionScript 3 where one of the first things you learn about is the concept of a class, a blueprint for an object. I guess I'm too young to remember a lisp world without Clojure so at first I was amazed that you could even have a programming language that didn't based its foundational architectural patterns around classes or object use classes! I just today was working on a project and found it refreshingly simple to just write some functions in a namespace so I could require and call them from another file. In this post we'll take a look at how to we import functions in a ClojureScript project using the build tool leiningen without classes or objects.

An Quick Example

Let's suppose I have this basically blank project for an AWS lambda function written in ClojureScript using the cljs-lambda leiningen template (and chopping off some of the "work-magic" code).
(ns basic-cljs-lambda.core
  (:require [cljs-lambda.util :as lambda]
            [cljs-lambda.context :as ctx]
            [cljs-lambda.macros :refer-macros [deflambda]]
            [cljs.nodejs :as nodejs]))

(deflambda cool-lambda-function [{:keys [magic-word] :as input} ctx]

  (js-obj "a" 1 "b" true "c" "hello there, caller!"))
In the above code we are declaring a namespace for this file, "basic-cljs-lambda.core", requiring a bunch of libraries, and then defining the lambda function with the name "cool-lambda-function". This function just returns a JSON object with some hardcoded values. 

Create A Function Somewhere Else

Suppose you now want to create a helper function, something that does side effects, something that does some calculations on the object that was passed into the lambda function, some function that does whatever! To stay organized I'll create a folder and name it cute-little-utils, and inside of that I'll put a file called string-utils.cljs in which I'll add a function called derpify. This function will be super useful because it will take a string and add the string "DERP" to either side of it, with spaces in between. 
(ns basic-cljs-lambda.cute-little-utils.string-utils
  (:require [basic-cljs-lambda.ok :as kk]))

(defn derpify [s]
  (str "DERP " s " DERP"))
Then you can simply load the namespace "as" an alias name and call the function in the code.  
(ns basic-cljs-lambda.core
  (:require [cljs-lambda.util :as lambda]
            [cljs-lambda.context :as ctx]
            [cljs-lambda.macros :refer-macros [deflambda]]
            [cljs.nodejs :as nodejs]
            [basic-cljs-lambda.cute-little-utils.string-utils :as s-util-baby]))

(deflambda cool-lambda-function [{:keys [magic-word] :as input} ctx]

  (.log js/console (s-util-baby/derpify "Hello World"))

  (js-obj "a" 1 "b" true "c" "hello there, caller!"))

Coding Without OOP

The problem with classes and object instantiation is that it encourages you as a developer to use mutable variables and local state freely which can quickly lead to a huge web of interconnected pieces that all modify each other's stuff to point where no one even know's what the heck changed what. In Clojure you can have your cake and eat it too- you are heavily encouraged to use the efficient built-in immutable collections yet you can use set! on mutable atoms if you really want to have mutable variables, but notice that we still don't need any class-based blueprints to do that. Playing the game of building a cute little army of classes that you spring to life mutate on the fly can be a fun way to personify programming, but when you want to get stuff done and end up with concise, maintainable code you should reach for Clojure.
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    ​Author

    Picture
    The posts on this site are written and maintained by Jim Lynch. About Jim...
    Follow @JimLynchCodes
    Follow @JimLynchCodes

    Categories

    All
    Actionscript 3
    Angular
    AngularJS
    Automated Testing
    AWS Lambda
    Behavior Driven Development
    Blockchain
    Blogging
    Business Building
    C#
    C / C++
    ClojureScript / Clojure
    Coding
    Community Service
    CS Philosophy
    Css / Scss
    Dev Ops
    Firebase
    Fitness
    Flash
    Front End
    Functional Programming
    Git
    Go Lang
    Haskell
    Illustrations
    Investing
    Java
    Javascript
    Lean
    Life
    Linux
    Logic Pro
    Music
    Node.js
    Planning
    Productivity
    Professionalism
    Python
    React
    Redux / Ngrx
    Refactoring
    Reusable Components
    Rust
    Security
    Serverless
    Shell Scripting
    Swift
    Test Driven Development
    Things
    TypeScript
    Useful Sites
    Useful Tools
    Video
    Website Development
    WebStorm
    Writing

    Archives

    August 2021
    February 2021
    January 2021
    October 2020
    September 2020
    May 2020
    April 2020
    February 2020
    January 2020
    December 2019
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015

    RSS Feed

  • Blog
  • About Jim
JimLynchCodes © 2021