clojure-style-guide icon indicating copy to clipboard operation
clojure-style-guide copied to clipboard

Why "use clojure.string instead of interop"?

Open rkc-rkc opened this issue 5 years ago • 5 comments

From the doc ==>

(clojure.string/upper-case "bruce")

;; bad
(.toUpperCase "bruce")

But looking at clojure.string/upper-case

user=> (source clojure.string/upper-case)
(defn ^String upper-case
  "Converts string to all upper-case."
  {:added "1.2"}
  [^CharSequence s]
  (.. s toString toUpperCase))

it is but a call to interop. It will be nice to know why one is preferred over the other.

rkc-rkc avatar May 07 '20 15:05 rkc-rkc

Two reasons:

  • reads better (subjectively)
  • portable across Clojure implementations. All interop calls tie you to the underlying platform.

bbatsov avatar May 07 '20 15:05 bbatsov

* reads better (subjectively)

Style guide by definition is subjective and this is fine with me

* portable across Clojure implementations. All interop calls tie you to the underlying platform.

That had not crossed the mind. Thanks. May I then suggest the title "Use platform agnostic methods to improve portability"

rkc-rkc avatar May 07 '20 16:05 rkc-rkc

Yeah, I totally agree this should be formulated in a more generic manner.

bbatsov avatar May 08 '20 07:05 bbatsov