Fork me on GitHub
#clj-kondo
<
2022-05-31
>
Noah Bogart14:05:47

At my job, we have a lot of test assertions inside of let blocks:

(let [stuff (do-stuff a b c)
      _ (is (= 1 stuff))
      _ (is (= 2 (inc stuff)))
     other-stuff (do-more-stuff x y z)]
  (is (= 3 other-stuff)))
Merely to avoid having to nest let blocks. (Sometimes, the only test assertions are inside of let blocks lol) I think this a code smell and have written up a hook to warn on this. Would you be interested in this as an official lint?

borkdude14:05:29

No, I like this ;)

borkdude14:05:38

But maybe other people are interested and then I would be okay with adding an optional linter. I'll keep it in mind. Let's wait for this to come up more often, it's the first time I hear someone perceive this as a problem

👍 1
Noah Bogart15:05:46

Yeah, it's not bad necessarily, it's just not how I think it should be done and I find it hard to work with (because I can't write normal test stuff, I have to be aware of when I'm inside of a let block and add dummy binds _ , etc). Would you like me to open a github issue about this?

borkdude15:05:06

Yes, and then others could comment on that, that's fine

👍 1
seancorfield18:05:13

FWIW, we have 85 of these in our work codebase. It's a pretty convenient way to provide inline assertions in complex tests.

Noah Bogart19:05:53

Yeah, I wouldn't suggest this to be on by default, but it does seem worth having as a toggle for those who care.

borkdude19:05:23

I sometimes even prefer this style to not get deeply nested expressions

1
jacob.maine03:06:04

Anyone ever tried https://github.com/Engelberg/better-cond for this sort of thing? It’s advertised as being good at both avoiding rightward drift and sprinkling in side effects

seancorfield03:06:36

@U07M2C8TT I wouldn't consider better-cond as a solution to this particular "problem" -- there's no conditional work going on here.

seancorfield03:06:17

(my personal feeling is that better-cond is solving the wrong problem: refactor your code, don't add a weird macro to support your bad code 🙂 )

😂 1
jacob.maine03:06:27

@U04V70XH6 I agree, I’d be skeptical of better-cond in the main codebase, but I feel less hesitant about using a “weird macro” in test code. I’ve never used it, I was just curious if anyone had ever used it in the style where you https://github.com/Engelberg/better-cond#minimizing-rightward-drift

seancorfield03:06:31

Wow, I think that's horrific... Both versions...

seancorfield03:06:44

It's screaming for a refactor.

jacob.maine03:06:16

Perhaps, but again, we’re talking about test code, which doesn’t always get the same love. Again, I’m not advocating this style, just asking if it’s worked out for anyone. It’s clear… it wouldn’t work for you! simple_smile

1