Fork me on GitHub
John Oerter14:07:37

Is this an idiomatic way to switch between different states of my app? I'm using cond with a keyword atom for each state.

[:main.inner.cover.flex-1 {:role "main"}                                                                                                          
     (let [state @app-state]                                                                                                                          
             (= :goal-input state) [goal-input the-goal app-state]                                                                                          
             (= :goal-timer state) [goal-timer the-goal]
             (= :goal-retro state) [:h1 "Goal Retro"]))]  


I would use case since all of the keywords are static.


Regarding the usage of the ratom itself - I think it's OK. But it's a very small case, it's hard to do something "bad" here.

John Oerter14:07:56

Ah perfect, thank you. If I use case then I wouldn't need the let anymore to deref the atom right?


Yep. And all those =.

John Oerter14:07:23

Nice. Thank you!


Ah, there's one difference between cond and case that may be important. With cond, if state is not equal to any of the keywords, the value will be nil. But with case, there will be an exception.

John Oerter14:07:23

Ah okay. But I can provide a default as well, right? Also, are keywords the best way to represent these states? Is there a better way?


Yeah, but the default needs to be explicit. Just something to keep in mind. Well, it depends. Seems fine in this case.

John Oerter18:07:10

Thanks! I appreciate the help

John Oerter18:07:41

Yeah coming from c#/typescript it feels weird, but I don't know of a better way at this point

David Pham05:07:32

You could also use multimethods or reitit for doing the routing.