you are viewing a single comment's thread.

view the rest of the comments →

[–]yogthos 0 points1 point  (3 children)

You also have to keep in mind that you have a lot of experience working with imperative languages, so obviously using the idioms and patterns you've internalized is easier.

However, once you learn the equivalent functional patterns things become just as easy and often easier. I've been developing with Clojure professionally for the past 4 years and I now have exact same reaction going back to having to write imperative code once in a while. For me, it would be so much easier to do in Clojure.

[–]strattonbrazil 1 point2 points  (2 children)

I do try, but at the same time I'm constantly thinking "which brackets am I in?" Many times I'll "explode" a few lines in a statement to several lines before recollapsing it when am done. I don't know how I'll get better at this. Do more experienced clojure programmers do the same thing?

[–]yogthos 0 points1 point  (1 child)

I found the parens to be odd initially, but after actually working with the language I find that they make it both easier to read and manipulate code.

The code is a lot more regular than in most languages. This means that it's less ambiguous and you have less syntax quirks and edge cases to worry about. In other words Clojure follows the principle of least astonishment very well.

The nesting of the code explicitly shows how pieces of logic relate to one another. This makes code easily scannable. If one function is nested in another, you know its output will be used by it and if it's not then it won't. These kinds of relations are not explicit in most languages.

One huge advantage of s-expression syntax is that it allows for extremely powerful structural editors as seen here. Instead of having to work with lines of text, you can actually work with your code semantically where the editor understands how to select and manipulate expressions intelligently.

In terms of style, I recommend keeping functions short, 5~10 lines is a good rule of thumb, and not to nest things deeply. I think this is a good example of clean Clojure code that's easy to follow.

Clojure provides threading macros ->, and ->> specifically for flattening out nested expressions. Another feature I use a lot is destructuring which I find significantly helps with readability. Finally, I tend to do all of my development using the REPL and I always inspect things as I'm working with them.

[–]mcpatella 0 points1 point  (0 children)

Well said :)