Showing posts with label Clojure. Show all posts
Showing posts with label Clojure. Show all posts

Saturday, January 18, 2014

Clojure/Compojure/Ring , a new experience in developing web applications


Its been more than 10 years working on java based applications mostly web applications mostly using a range of frameworks from pure servlets to spring web flow. Except in case of a Servlet you definitely feel that there is this heavy stuff that is surrounding your code.

Although I have liked the convention based approach of Ruby on Rails and Grails on JVM  the introduction to Clojure web development with Compojure was an amzing experience and I had my doubts too.

A Simple Log View application I planned to develop for my work place sounded perfect for me to put it to test. Here are my observations

Good

  1. Super Simple way to test the functionality using fine grained functions at REPL
  2. Ability to attach to a running application using nREPL giving me never before speed of development. Being there in the application always is an awesome feeling, somewhat similar to SmallTalk environment
  3. Intellij Idea with Cursive Plugin is an amazing one for me, considering my java background and long standing incompetence in mastering EMacs.
  4. Enlive as the template transformation/generation tool is something i wanted for Java all the tim
  5. Lein is the best project build/dependency tool  I have ever used (I still love Gradle for java though)


Bad (or Ignorance)
  1. Not very sure about the idiomatic way to manage state in a web application. In Java world I was used to store values in session without even thinking
  2. SQL Korma like libraries provide great way to access database using a consistent API but  there is no easy way to handle transaction roll backs in case of a Optimistic Lock Exception like the way I was used to in Hibernate/JPA world using the version field and checking update counts.

Wednesday, May 29, 2013

Debugging a running Clojure Application

Clojure is fascinating.  Particularly for a programmer coming from a generation which is dominated by Object Oriented "Design" and C like syntax being more "readable". I was disturbed by the idea that I am unable to understand the Clojure code at first glance and I have to confess it really took couple of weeks to get my brain read Clojure code more fluently.

Coming from Java platform greatly spoiled by best of the developer tooling, Clojure development using Emacs is difficult (No offense to the tool as a tool is as good as its user). Let alone introducing the language to your friends in your work place is challenging, lack of "easy" IDE and debugging facilities would be very difficult to sell.  I am happy that I was wrong and my favorite IDE , IntelliJ IDEA has decent Clojure support and getting better every day.  One key task of any java developer is to debug the running application with less fuss and ...... Here is how they do it...

This setup consists of following :

  1. Run the Project using Leiningen in debug mode
  2. when required attach Intellij to the running application
  3. set the break points and fix/inspect the state of the application.
  4. Modify code and press Ctrl+Shift+F9 to hot replace the code and see changes immediately.
Let us look in to details:

Run the Project using Leiningen in debug mode

  • Add dev profile to your project.clj with the following jvm options

                   :jvm-opts ["-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5010"]
 
                Where 5010 is port number that is specified in Intellij Remote Debug Configuration
  • Add an alias to project.clj to be able to run project in debug mode using lein debug as follows  
             :aliases {   "debug" ["with-profile" "dev" "run"] }
  • whenever you run lein debug the standard run configuration will be executed with the jvm options which will allow you to attach debugger from IntelliJ Idea
When required attach Intellij to running application
  • Create a Remote Debug configuration to attach to the port specified in step above (5010 in example) and make it selected configuration and click debug when required
Other two steps are self explinatory.


Now Clojure development feels more natural and less demanding from tooling perspective given the great quality of  La Clojure plugin from JetBrains... which I know will keep getting better as every day passes.

I hope it would help budding Clojure enthusiasts in Java world.