Is there an idiomatic way to avoid long Clojure string literals?

clojure, idioms

Solution

You should probably store the string inside an external text file, and read the file from your code. If you still feel the need to store the string in your code, go ahead and use `str`.

EDIT:

As requested, I will demonstrate how you can read long strings at compile time.

(defmacro compile-time-slurp [file]
  (slurp file))

Use it like this:

(def long-string (compile-time-slurp "longString.txt"))

You may invent similar macros to handle Java Properties files, XML/JSON configuration, SQL queries, HTML, or whatever else you need.

Problem

Various Clojure style guides recommend avoiding lines longer than 80 characters. I am wondering if there is an idiomatic way to avoid long `String` literals. While it's common these days to have wide screens, I still agree that long lines should be avoided. Here are some examples (I'm tempted to follow the first): ``` ;; break the String literal with `str` (println (str "The quick brown fox " "jumps over the lazy dog")) ;; break the String literal with `join` (println (join " " [ "The quick brown fox" "jumps over the lazy dog"])) ``` I am aware that Clojure supports multi-line `String` literals, but using this approach has the undesired effect of the newline characters being interpreted, e.g. using the `repl`: ``` user=> (println "The quick brown fox #_=> jumps over the lazy dog") The quick brown fox jumps over the lazy dog ```

Original source