Pages

Thursday, December 27, 2012

Ankara JUG, HTML5

I'll be speaking at Ankara JUG, 24 Jan, on HTML5.
If you are interested and in Ankara details are here.


Friday, July 6, 2012

Javascript Tips

Running, Testing & Debugging your JS
I use Google Chrome for my JS development. Click "Tools > JS Console" and you can put break points, trace, watch variables and see the log/error messages that your JS produces. Chrome allows you to print your own logs to it's console through a 'console' object which is internally defined, I used to use 'alert()' function which was a pain;
 console.log("we are here");  
Variables & Scopes
If you use the 'var' keyword a new variable will be created in the current scope. If you just do a variable assignment JS will start searching the variable starting from the current scope to upper (caller) scopes. If it finds the variable it won't create a new variable and assign the value to that variable. If it can't find any previously declared variable it will create a variable in the top level (global) scope.
Here is an example;
 for(i = 1; i <= 3; i++) {  
      print()  
 }  
 function print() {  
      for(i = 1; i <= 3; i++) {  
           console.log(i);  
      }  
 }  
Here I am using 'i', as I usually I do on my 'for' loops, but the 'i' variable is actually created only once on the top scope. Once the 'print' loop is done upper loop also exits, so 1, 2, 3 is logged only once.  If we append a 'var' to 'i' on the first loop nothing will change. If we append a 'var' on  to 'ion the second loop there will be two different 'i' variables on different scopes hence the 1, 2, 3 will be printed 3 times.
So if you are looking for "C like" scope behavior best you could do is not forgetting to use the 'var' keyword.
Declaring Classes & Methods
You can do object oriented programming in JS. Here is an Object constructor:
 function Matrix(ray, rlen, clen) {  
   this.ray = ray;  
   this.rlen = rlen;  
   this.clen = clen;  
 }  
 Matrix m = new Matrix([],2,2);  
 console.log(m.rlen)  
whit the 'this' reference we are assigning parameter values to object fields and than we can reference them through the '.' operator.
To create methods for our 'Matrix' object and call them:
 Mx.prototype.findMin = function () {  
 // code to find the min  
 ...  
   return min;  
 }  
 m.findMin()  
You can use the 'this' to reference the fields, from methods. These are the very basics I mostly use. There is also support for inheritance, private fields etc. but I found the notation a bit complicated.
Anonymous Objects or Structs
A nice feature used is creating sort of an anonymous objects (I think these are more like structs in C);
 var user = {  
     name: "mca",  
     numOfLogins: 10,  
     roles: []  
 };  
Here we can use the '.' operator to access the fields as usual. Also JS uses '[]' to denote arrays which has methods like 'pop', 'push' etc.
Closures
One last thing is that JS has some nice support for functional programming;
 var f = function(n) {  
      var r = 1;  
      for(var i = 2; i <= n; i++) {  
           r *= i;  
      }  
      return r;  
 }  
 alert(f(3));  
 var f3lazy = function (g,n) {  
      return (function(g,n){  
                return function() {return g(n)};  
           })(g,n);  
 }(f,3);  
 alert(f3lazy);  
 alert(f3lazy());  
Variable 'f' is assigned with a factorial function. We could use it as a parameter to other functions or just call it like it's done with 'alert(f(3))'.
Below that is something you could do which is more tricky. Variable 'f3lazy' is assigned to a function which will call the factorial function with parameter '3' when called with no parameters. It's an example of how you could create lazy functions through functions already there.

Friday, May 25, 2012

Using container objects instead of fields ?

You know the POJOS, the one with getters/setters ?
 public class UserPojo {  
   private Serializable id;  
   private String login;  
   public Serializable getId() {  
     return id;  
   }  
   public void setId(Serializable id) {  
     this.id = id;  
   }  
   public String getLogin() {  
     return login;  
   }  
   public void setLogin(String login) {  
     this.login = login;  
   }  
...
 }  
What now if we need to serialize this to XML or JSON. Instead of using something like Xstream maybe we could do it like this :
 public class UserPojo {  
   private JSONObject jsonObject;  
   public UserPojo() {  
     jsonObject = JSONObjectHelper.newJSONObject();  
   }  
   public Serializable getId() {  
     return (Serializable) JSONObjectHelper.get(jsonObject, "id");  
   }  
   public void setId(Serializable id) {  
     JSONObjectHelper.put(jsonObject,"id",id);  
   }  
 ...  
 }  
We are building the JSON object as setters are called. No need of a reflection api . Although now we are dependent to JSON or XML or whatever... I could think of runtime proxy generation code that would replace the getter/setter code but I really don't like class definitions that hang in memmory. May be some spi stuff and a more general api could be introduced which would let your POJOs store the data in whatever format you choose.
As for me I think this is a good fit to use with MongoDB because know I can convert my POJOs easily  to MongoDB's DBObject:
 (DBObject) JSON.parse(hasJSONObject.getJSONObject().toString());  
and fill up my POJOs with MongoDB data:
 User user = new User();  
 user.setJSONObject(dbobj.toString());   
And again a good fit for JAX-RS  in which case It would convert my POJOs without going through some reflection process.
It would even be better if I could configure my POJOs with an api to store data both within a JSONObject and a MongoDBs DBObject without a compile time dependency.
Looks like a simple way to do things ?

Thursday, February 16, 2012

Cloud Foundry vs Google App Engine

Over the past months I had some experience on the Cloud Foundry and the Google App. Engine. Below are my reviews on them, mostly on a developer perspective, not on performance or on cost. Another note is that CF is still in beta so it may seem a bit unfair comparison but as long the philosophy stays the same I think points I made will hold.

Google App. Engine
Appengine is the first one I tried to deploy. You have a nice web console to control your instances and application. Google offers you api's to replicate your data across instances. Google also offers you some nice api's like the Channel api which you may use as a serverside push.

Cons; being an experienced Java developer I wasn't excited to the fact that I had to add an appengine.xml on my jar even though I wouldn't be using any service from Google.

I discovered that Google does not give you access to some JDK classes, like the NIO stuff. So you can't just use/do anything you like.

HttpSession's are not enabled by default, you have to enable it on your appengine.xml. Google apparently wants you to use it's own api's, DataStore stuff in particular. Once you are committed to Google it does not seem easy to move away from it.

Lastly HttpSession.contextDestroy method does not get called, yet... So you have to do some more stuff like suggested here. Makes me thing what else they have omited ?

Cloud Foundry
With cloud foundry you get tomcat instances. I configure/manage my apps using an ruby app. called 'VMC'. Community is helpful questions asked are answered pretty fast. It follows the Java standards. Rarely something thats working on my tomcat instance does not work within the CF. Provides services like Redis, MongoDB ...

Cons are it's still in beta.
You can't purchase instances yet.
To access the services I had to use CF api though there may be other options that I don't know, anyway still it seemed less invasive than Google to me.

Lastly
Although CF is still in beta I believe it offers a better, non-invasive environment compared to GAE.

Tuesday, January 10, 2012

QMass M1

It has been a while since I wrote anything on QMass. Here are the updates :
  • Grid now tries to persists it's values, asynchronously, on given databases. Currently MongoDB is the default but this would be pluggable.
  • I have tried various scripting languages for the console. I was not very happy with Groovy because it had too many dependencies and kind of a overkill for my needs. I replaced it with "JavaScript" which is by default available with sun JDK6. Not 100% happy though since it may not be available by default on other platforms.
  • To minimize the dependencies I moved to java.util.logging from log4j and commons-logging. I replaced the commons-logging with a little module I named 'yala', which stands for 'yet another logging api'. My two cents to java logging hell...
  • Since I release less often, from now on I decided to name my versions as M1, M2 and so on...