I'll be speaking at Ankara JUG, 24 Jan, on HTML5.
If you are interested and in Ankara details are here.
Thursday, December 27, 2012
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;
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;
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:
To create methods for our 'Matrix' object and call them:
Anonymous Objects or Structs
A nice feature used is creating sort of an anonymous objects (I think these are more like structs in C);
Closures
One last thing is that JS has some nice support for functional programming;
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.
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 & ScopesIf 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 'i' on 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 ?
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:
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 ?
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.
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.
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.
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.
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...
Monday, November 28, 2011
Recognizing The Letters as Being Drawn With The Mouse
While playing-out with a Kinect device, I think I will be writing more about this later, I had an idea. I could detect letters being drawn with the mouse and convert them to key presses enabling the user, use the mouse like a keyboard. It's basically same problem like they do with these pens.
So while drawing the above with the mouse my app. prints "BABA" on where the cursor is. There are two parts to this problem, hard part is obviously recognizing what actually is written. Easy part is producing the key presses. Here are the libraries I used :
java.awt.Robot
A Robot is actually created with automating tests in mind. It has the methods for moving, clicking the mouse, producing key presses and capturing the screen.
java.awt.MouseInfo
MouseInfo class gives you the location of the mouse. Unfortunately with just Java I couldn't find a way to tell if any of the mouse key's are being pressed out side the java application window. Here are some discussions on that. Apparently one would have to use a native library. Anyway I didn't really need to handle clicks at the beginning anyway.
How to Recognize The Characters
My approach is capturing the changes in the movement of 'x' and 'y' axis.
Here is how 'x' and 'y' axis moves while a the letter 'A' is being drawn :
One might also choose to compare the values of increases and decreases in order to eliminate some false matches.
I use 90ms mouse position samples to compute the differences from the previous location. If the differences are less than 10 pixel I ignore them.
Here is how the 'B' recognition function looks like :
I found this approach working quite well even with not taking account if the mouse button is pressed, though that would be required to match some simpler characters like 'I' or '.'.
java.awt.Robot
A Robot is actually created with automating tests in mind. It has the methods for moving, clicking the mouse, producing key presses and capturing the screen.
java.awt.MouseInfo
MouseInfo class gives you the location of the mouse. Unfortunately with just Java I couldn't find a way to tell if any of the mouse key's are being pressed out side the java application window. Here are some discussions on that. Apparently one would have to use a native library. Anyway I didn't really need to handle clicks at the beginning anyway.
How to Recognize The Characters
My approach is capturing the changes in the movement of 'x' and 'y' axis.
Here is how 'x' and 'y' axis moves while a the letter 'A' is being drawn :
One might also choose to compare the values of increases and decreases in order to eliminate some false matches.
I use 90ms mouse position samples to compute the differences from the previous location. If the differences are less than 10 pixel I ignore them.
Here is how the 'B' recognition function looks like :
@Override
public void execute(Double x, Double y) {
if (state == 0 && (y < 0 || (y == 0 && prevY < 0))) {
state++;
} else if (state == 1 && x > 0 && y > 0) {
state++;
} else if (state == 2 && x < 0 && y > 0) {
state++;
} else if (state == 3 && x > 0 && y > 0) {
state++;
} else if (state == 4 && x < 0 && y > 0) {
state++;
} else {
state = 0;
setIndex(saveI);
saveI++;
}
if (state == 5) {
setResult(getIndex());
breaked();
}
prevY = y;
}
This function is continuously fed with ordered X and Y axis values. State variable is increased if the difference is expected, reseted otherwise. When the state 5 is reached than we know that it's a 'B'.I found this approach working quite well even with not taking account if the mouse button is pressed, though that would be required to match some simpler characters like 'I' or '.'.
Monday, October 31, 2011
Hot R&D Topics At Itea 2011 co-summit
Last week I attended the Itea2/Artemis co-summit. It's a showcase of Itea funded R&D projects.
This year apparently most projects are focused on embeded software (Artemis), M2M Networks and Green Energy.
There are several projects that focus on M2M architecture/applications, including ours A2Nets, which would allow M2M devices to connect and exchange information with each other. Notable applications would be determining when your bus will be at the bus stop or cars passing by sharing traffic data with each other.
It seems, at least in europe, smart readers will be more common in the future. There are numerous projects trying to build and manage different metering networks such as electric and gas metering.
MetaVerse1 was also an interesting project which attempts to enable interoperability among virtual worlds. One of the products of the project is the MPEG-V standard.
This year apparently most projects are focused on embeded software (Artemis), M2M Networks and Green Energy.
There are several projects that focus on M2M architecture/applications, including ours A2Nets, which would allow M2M devices to connect and exchange information with each other. Notable applications would be determining when your bus will be at the bus stop or cars passing by sharing traffic data with each other.
It seems, at least in europe, smart readers will be more common in the future. There are numerous projects trying to build and manage different metering networks such as electric and gas metering.
MetaVerse1 was also an interesting project which attempts to enable interoperability among virtual worlds. One of the products of the project is the MPEG-V standard.
Subscribe to:
Posts (Atom)
