New job, new application,new ideas!
After getting the hang of the application I am taking care of I star noticing the same code repeating. And after taking a look at some other web pages I see the same things repeating.
As a fair warning we are talking about javascript here so the work code is and will be used in its widest definition.
function foo(){
var element = $("elementId");
if(element != null){
//do something
}
}
or the classic one where an anonymous object is passed to a function and they check for each and every member of the object…
function bar(user){
if(user.name){
// use the users name
}
// dosomething else
if(user.surname){
// now it's safe to use the surname
}
}
And now imagine that a million times over all the files that compose the client site part of the application.
Yes that makes Dejan quite unhappy
So this is where ValyJs comes into play. I would not call it a complete product but I wrote up some helper functions that can aid those common scenarios.
Now you can write it like this.
function foo(){
valy.executeIfExists({
elementId : "elementId",
func : function(element){
// do your stuff here
}
});
}
Is this better? I think that it is more readable if nothing else. If nothing else it is consistent.
For the second scenario. We can check for all members up front.
function bar(user){
valy.exists(user.name);
valy.exists(user.surname);
// do your stuff
}
I to hope that this is better.
There is still a long way to go till I get this to version 1, currently I have it at version 0.1.1 and it is working.
What I need now is some creative input to get something that is actually usable.
So anyway you can go and check it out here: http://code.google.com/p/valjjs/