Fork me on GitHub
#code-reviews
<
2022-04-25
>
dgb2319:04:39

A small snippet: (or (some-> req :path-params :slug db/page html-ok) (not-found)) My gut feeling here is that this is too javascripty and not idiomatic clojure. html-ok and not-found are rendering some html and put that into a ring response. To me this expression seems clear in isolation. Do you agree?

Cora (she/her)19:04:16

does html-ok return nil if no page is found?

dgb2319:04:19

not this is less about overall structure (which is ad-hoc at this point) and more about the general approach to do this kind of thing.

dgb2319:04:34

db/page returns nil

Cora (she/her)19:04:42

I'd use an if-let myself

dgb2319:04:44

which is why I used some->

dgb2319:04:53

instead of or ?

Cora (she/her)19:04:16

fetch the page in an if-let and either render ok or not-found

Cora (she/her)19:04:33

I think that's clearer

Noah Bogart19:04:54

yeah, having the two branches of the if be the render calls makes sense to me

dgb2319:04:19

ah - you mean the db/page into the if-let. because it confuses the reader that this is typically the thing that will short circuit?

dgb2319:04:51

right that makes sense ty

dgb2319:04:18

much better ty:

(if-let [page (some-> req :path-params :slug db/page)]
    (html-ok page)
    (not-found))
However in general - would or be used this way at all or should it really be thought of as evaluating to a boolean?

dgb2319:04:38

That is kind of my bigger question. I’m used to using || and ?? in JS a ton.

dgb2319:04:17

const thing = maybe || default;

noisesmith19:04:09

I use or to apply defaults, eg.

(or (f foo)
    (g bar))
vs.
(if-let [result (f foo)]
  result
  (g bar))

dgb2319:04:50

Right, similar to how get works

dgb2319:04:19

But I get that in the above case the if-let should be used

dgb2319:04:29

ty everyone ❤️

noisesmith19:04:28

you could use or there, it's safe to put the html-ok call inside the some-> chain unless db/page returns false

👍 1
noisesmith19:04:39

it's a question of style of course