Skip to main content
added 2 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Clojure code style Getting data from different services

I have a piece of code that tries to get some data from a different services, falling back to the next one if previous fails.

The code look a little bit ugly though, anythough. Any suggestions on how to rewrite it in a more concise way?

(defn get-data [query]
  (let [data-cached (get-data-from-cache query)
        data-service-1 (if (nil? data-cached)
                         (get-data-service-1 query)
                         data-cached)
        data-service-2 (if (nil? data-service-1)
                         (get-data-service-2 query)
                         data-service-1)
        data (if (nil? data-service-2)
                   (get-data-service-3 query)
                   data-service-2)]
    data))

Clojure code style

I have a piece of code that tries to get some data from a different services, falling back to the next one if previous fails.

The code look a little bit ugly though, any suggestions how to rewrite it in a more concise way?

(defn get-data [query]
  (let [data-cached (get-data-from-cache query)
        data-service-1 (if (nil? data-cached)
                         (get-data-service-1 query)
                         data-cached)
        data-service-2 (if (nil? data-service-1)
                         (get-data-service-2 query)
                         data-service-1)
        data (if (nil? data-service-2)
                   (get-data-service-3 query)
                   data-service-2)]
    data))

Getting data from different services

I have a piece of code that tries to get some data from a different services, falling back to the next one if previous fails.

The code look a little bit ugly, though. Any suggestions on how to rewrite it in a more concise way?

(defn get-data [query]
  (let [data-cached (get-data-from-cache query)
        data-service-1 (if (nil? data-cached)
                         (get-data-service-1 query)
                         data-cached)
        data-service-2 (if (nil? data-service-1)
                         (get-data-service-2 query)
                         data-service-1)
        data (if (nil? data-service-2)
                   (get-data-service-3 query)
                   data-service-2)]
    data))
Source Link

Clojure code style

I have a piece of code that tries to get some data from a different services, falling back to the next one if previous fails.

The code look a little bit ugly though, any suggestions how to rewrite it in a more concise way?

(defn get-data [query]
  (let [data-cached (get-data-from-cache query)
        data-service-1 (if (nil? data-cached)
                         (get-data-service-1 query)
                         data-cached)
        data-service-2 (if (nil? data-service-1)
                         (get-data-service-2 query)
                         data-service-1)
        data (if (nil? data-service-2)
                   (get-data-service-3 query)
                   data-service-2)]
    data))