Fork me on GitHub
#code-reviews
<
2023-11-02
>
λ02:11:15

Hello, I would appreciate any suggestions on how I can improve my code. This code connects to multiple IRC servers, it can optionally connect using SSL and joins multiple channels. https://tildegit.org/fn/irc-bot/src/branch/main/src/irc_bot/core.clj

phronmophobic02:11:50

(doall (map connect servers))
making a lazy sequence just to immediately realize it is fine, but I would prefer (into (map connect) servers) or (mapv connect servers).

phronmophobic02:11:56

It's a little unintuitive that connected? and disconnected? aren't negations of each other.

phronmophobic02:11:38

I don't see any coordination for writing to a connection. This could cause interleaved writes in a multi-threaded context (which may or may not be a problem for your use case). One option might be to use a java.util.concurrent.ConcurrentLinkedQueue or similar.

phronmophobic02:11:49

Generally speaking, there are few operations that could be run from any thread. I might consider something like having a queue for each connection that make sure operations like disconnecting, joining, logins, etc aren't interleaved.

phronmophobic02:11:28

Personally, I reach for core.async for this kind of thing, but it's not everyone's cup of tea.

phronmophobic02:11:12

join-all-channels returns a lazy sequence. I would prefer doseq over for in this context.

phronmophobic02:11:00

connect initializes the connection with the :channels key from server , but doesn't otherwise join a channel or deal with channels. I would probably have connect ignore :channels from server and include that info in conn elsewhere (like join)

λ04:11:43

Hey Adrian, I appreciate you responses, I am completely new to programming, I ask that you forgive me but what could go wrong in this case of uncordinated writes to the Socket’s output stream. I haven’t fully wrapped my head around a lot of the Java classes that I am using in this code.

λ04:11:14

The way connect currently works feels awkward and I am not sure if I am modelling things correctly. Good call on connect? & disconnect these are experimental and I have noticed odd behaviour with connected? returning through even though the socket is closed.

λ04:11:59

This is my first project in Clojure and my aim is to learn how to model problems, as a professional engineer would. So any feedback towards that would be greatly appreciated.

phronmophobic07:11:59

> what could go wrong in this case of uncordinated writes to the Socket’s output stream. for (.println (str msg "\r")) , it basically means that it would be possible for multiple different messages to be written with characters interleaved: part of one message, followed by part of another, followed by part of another, etc. In some cases, it might be unlikely for short messages to be interleaved, but I find that it's usually best not to rely on implementation details.

λ13:11:36

IF Each OutputStream is on a dedicated thread. Would this still be an issue?

phronmophobic15:11:46

yea, that should work.

Ryan Martin05:11:42

Hi folks, Beginner here 😅. I made a url shortener to learn about web development in clojure. I had some trouble gluing the libraries at first, but now it seems to be working fine now(?). Here's the repo: https://github.com/rmrt1n/urlfrog I would love some feedback on the code and the project structure! Thanks!

phronmophobic17:11:08

It seems like creating a duplicate slug should return an error if it randomly chooses a slug that already exists, https://github.com/rmrt1n/urlfrog/blob/82c86db1ee5b6043ddaa5f36fd29a7e02a36a32a/src/urlfrog/routes/shorten.clj#L27

phronmophobic17:11:31

It doesn't seem like there's a uniqueness constraint on slug ids for your xt db.

phronmophobic17:11:28

The logic for creating a slug is coupled to the request handler. I recommend having a standalone function for dealing with slugs.

Ryan Martin05:11:13

I see, thanks!

moe12:11:37

some style and factoring ideas for shorten.clj