Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

Wednesday, April 18, 2012

Doto

I've always had an issue with code like this: (event though I do it all the time)


Nothing wrong with it, but wouldn't it be nice to be able to chain those calls and avoid declaring that local variable?

I am currently reading "The Joy of Clojure" and came across the doto macro in clojure, which enables you to do the above like so: 
   (doto (new java.util.HashMap) ; or just (java.util.HashMap.)  
         (.put 1 "One")  
         (.put 2 "Two"))  
Nice!  But alas, I am forced to use Java by day so I tried to figure out the best approach to do this.  The first one comes to mind is this:
 new HashMap<Integer, String>(){{  
     put(1, "one");  
     put(2, "Two");  
   }};  

Which works perfectly except that it assumes you are the one constructing the object.  So I tried a more functional approach:


I believe I've gone and made it worse :(

I also tried a js implementation, which is pretty redundant for maps/objects as object literals are built in:


I would love to see some alternate solutions/improvements or maybe event implementations in other languages!  


Thursday, March 15, 2012

Knockout.js

I am no HTML, CSS or JavaScript expert, but I have seen some nice JavaScript libraries the past few weeks. Nico introduced us to node.js and I am sure everybody is familiar with jquery. Kaveen also mentioned HighCharts. Yesterday Marlo introduced me to Knockout.js which helps to "simplify dynamic JavaScript UIs by applying the Model-View-ViewModel pattern". I am not going to try and show you some code examples, because I think the Knockout.js developers have done a very good job themselves. Just head on over to http://learn.knockoutjs.com and play around with their "interactive coding exercises". Let us know what you think about the Model-View-ViewModel pattern..?

Thursday, February 23, 2012

Building your own IDE

So, recently, I've been dabbling a bit in node.js / javascript. This has led me to the conclusion that these monolithic IDE's we use every day, are a Bad Idea (i'm looking at you eclipse). Because of the bloatedness of IDEA/eclipse/netbeans for javascript / HTML development I went back to basics. Basically building my own IDE using smaller, more specialized components. So, my basic setup is as follows:
  • Sublime Text 2 - text editor and syntax highlighting
  • jslint - Lint tool for Javascript code. It's basically a syntax checker, together with a bit of static analysis
  • npm - this is the node package manager. Think maven. It can also run your tests
  • guard - this is a ruby gem I use to watch for file changes. It can execute arbitrary commands whenever a file changes
  • libnotify/notify-send - libnotify is used to make popups on the desktop
Right, so those are the pieces. How do they fit together though? Well, easy. Guard is setup to watch my source/views and tests. Whenever any of these changes, the complete test suite is run using npm, and the output is received as JSON. I then parse the JSON to create a popup on my desktop, which instantenously gives me the results of the unit tests. This works so well that I haven't really touched the browser in the last week to test my javascript code. Here's the code for the test running/popup: Here's my guard file: The popups look really nice. They are also quite customisable (depending on your notify backend). Try
notify-send "Test" test
on your PC to see what it does. Anyway, first post! Here's an example of the popup: