Does Clojure Tend To Encourage Small, Efficient Functions?

I have only two small Clojure projects, and other than writing tools on which nothing major depends, these projects will probably be the only ones in my current position. The rest will be done in Web languages and Perl. So, I enjoy a chance to enhance the Clojure projects.

I do not know why, and am not aware of any conscious prejudice, but writing Clojure code encourages me to create small functions, and external Clojure projects, like a utility project containing various odds and ends to handle sequences, time/date manipulation, and other useful stuff.

I have written enough Perl and Python code to appreciate what those languages do, but, for some reason, my functions tend to be longer and wider in definition than my Clojure functions.

Advertisements

4 Comments

Filed under Clojure

4 responses to “Does Clojure Tend To Encourage Small, Efficient Functions?

  1. Pingback: Clojure encourages small functions | Smash Company

  2. I think it’s the lack of ceremony. In other languages you’ve got keywords, braces, etc that each function definition needs. You feel like the function needs to be substantial to make all that ceremony worth it. It’s just parens in clojure, so it doesn’t matter whether it’s a separate function or inlined, it’s all the same, no ceremony.

    Being a non-object-oriented language helps too. OO makes you think more about the “behaviors” you attach to an “object”, you feel the need to make each behavior a complete thing of itself, and having lots of little helper functions seems to go against the paradigm. Whereas with Clojure a function is just, well, anything that you think would be useful to have as a function.

    Also Clojure’s focus on immutability helps. In other languages you often end up with long sequences of code that doesn’t make sense to break up into 3 functions because calling any one of those functions on its own would leave you in an inconsistent state. Even with mutations, the way Clojure does everything in transactions, it alleviates the need to keep everything in sequence.

  3. Bryan Lott

    Being someone who has written a lot of Python code and just stumbled on Clojure in the past few months, I can say for sure that it has encouraged me to write smaller functions. However, I now write smaller functions in *Python* as well. Learning Clojure has changed the way I think about programming in general and for that reason alone, I think it’s worthwhile for every programmer to learn Clojure.

    As for why Clojure encourages this, I think part of it is the visual complexity of writing a large function. If I glance at a large Clojure function (greater than say, 20 or 30 lines) my mind recoils in horror! However, if that large function is broken into 5 functions of ~6 lines each, I find that I can easily pick it apart, there’s visual definition between the ideas and concepts.

    With Python (I can’t speak to Perl as I’ve never written any), there’s more whitespace that allows your eye to wander easily over a function that’s 100 lines without reaching a visual overload and tripping that trigger in your brain that says “Danger Will Robinson! Refactor!”

    I think it really comes down to Clojure code being more “dense” in a visual sense than Python code.

    That being said? Part of me wishes I could write strictly in Clojure. It’s a beautiful language (visually and conceptually), and it truly feels like using a lightsaber vs. a blaster: http://xkcd.com/297/

    • Octopusgrabbus

      Thanks for commenting. I’m sure you can write small, tight functions in Python. For me, Perl is the worst. I have to keep reminding myself to break the code down into chunks. That paid off after re-writing our water utility billing system. You may have hit on visual complexity with Clojure. I tend to break up functions’ lines with newlines and add a lot of comments.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s