Vexi PlatformVexi Platform
vexi: easy, extensible, flexible
Showing posts with label tips. Show all posts
Showing posts with label tips. Show all posts

Saturday, August 29, 2009

Vexiscript Destructuring & Iterating Values

I have made a couple of improvements to vexi script (our dialect of javascript).

1) Destructurings

Destructurings in Vexscript are the same as those introduced in Mozilla Javascript 1.7.

//Useful for swapping values
var a = 1;
var b = 2;
[a,b] = [b,a];

// Or for handling multiple returns
var a,b,c;
[a,b,c] = f();


2)Improved for-in loop

Its pretty straightforward

var o = {a:3,b:2,c:1};
for(var k,v in o){
    // ...
}

// one might use this convention for an array
var arr = ["a","b","c"];
for(var i,v in o){
    // ...
}

This is not the same as any of the methods (there are at least 2) you can use in mozilla javascript. They support the form for(var [k,v] in o), which is unfortunate at is makes it an ambiguous syntax. It could be seen as a destructuring of just the key (so its not usually possible to have an array as a key in javascript, but its still not a very good choice in my opinion).

The other method is for each(var v in o), here the each key word just changes the behaviour of the loop. This is not really ideal as it is not useful if you need both the key and the value (which is the case perhaps as often as not).

Thursday, April 30, 2009

Tip: never return in a 'finally' block

This one has had me a couple of times. When you return in a finally block, Vexi will not log the exception because the return indicates that the function or trap has concluded successfully.

The symptom is code not being executed fully - a premature stop somewhere. The premature stop to the code path is caused by an exception, e.g. accessing a property on a null object.

var somefunc = function() {
    var nullobj;
    nullobj.foo = "bar"; // error!
    return true;
}

/** misguided function to return true on success, false otherwise */
var sometest = function() {
    var ret = false;
    try { ret = somefunc(); }
    finally { return ret; }
}

Of course the above code path does look, as described, misguided. However in more complicated scenarios, especially when working with RPC, it can be easy to forget the intended notion of a finally clause - to clean up necessary resources - and slip in a return. Mostly it will be harmless but on the occasions that the code path embedded in the try clause throws an error, you'll find yourself dumbfounded as to why your code path is not completing and why there is no indication of the problem.

There is a simple rule to follow to avoid this problem (which can take hours to diagnose!) and that is to either A) never 'return' in a 'finally' or B) always preceeded a 'finally' with a 'catch(e) { vexi.log.info(e); }' to make sure the error is logged.

HTH!

Monday, March 09, 2009

Debugging Vexi Applications II

As we've used Vexi over the last year, we have added little features here and there to help us get the most of out it.

I'm always mindful of performance, so I don't like to use the 'try/throw/catch' method from the previous debugging tip in production code. However chopping and changing code in a complex environment can be a pain, especially when you are working with code that has a far reaching impact and you want to be able to get a general sense of what is happening as you use your application.

For this, I added the 'vexi.debug' property. A simple boolean on the Vexi object, which can be enabled by either running vexi with the '-debug' flag or by putting 'true' or 'false' to the 'vexi.debug' property.

This way, we can wrap fairly complex debugging operations in an if statement and avoid them in normal running. For instance, we introduced 'vexi.js.stringify(arg)' which takes a JS object and returns it as JSON. Doing this in heavily run code can be a bit of a waste of CPU power, but at the same time JSON is much more useful for debugging than the type/hash representation (e.g. object$1ab23e) of non-primitives.

if (vexi.debug) {
    var o = anExpensiveOperation();
    vexi.trace(info);
}

Also, there is the call to vexi.trace which is another new function. It is shorthand for:

vexi.log.info(vexi.js.stringify(o));

Of course, the ideal way to avoid days of debugging headaches is to do proper unit testing. It takes time to get it right, but once you have a non-trivial application, a suite of tests can save you no end of trouble and make sure your application remains solid after major changes to the code. See the VUnit documentation for more information on how to create tests.

Thursday, September 20, 2007

Debugging Vexi Applications I

Most scripting languages are very hard to debug, especially as the code becomes complex and the variety of files and structures interacting with eachother becomes somewhat hard to have mapped in the mind.

Fortunately VexiScript has kept some neat features from Java and some features of it's own that make debugging complex Vexi applications fairly easy! For instance, a full stack trace with line numbers always accompanies an exception.

Of course, it may be that you don't want to stop executing code to get an idea of where your code is up to. In VexiScript, we can also throw and more importantly catch exceptions, much like in Java - but in VexiScript these exceptions are really easy to handle - just pass them to a vexi.log function and it will handle the rest.

if (myval != correctval) {
    try { throw "incorrect myval: "+myval; }
    catch(e) { vexi.log.info(e); } // that's it!
}
// and the script continues uninterrupted

It is as easy as that. ;-)

Friday, July 27, 2007

Understanding Event Processing

Understanding how events are propogated throughout the box model can enable you to pull off a lot of neat tricks when implementing more complicated widgets and user interfaces. This basic template is an interactive demonstration. Add extra traps (blocking and non-blocking) to understand exactly how traps and events work together.

<vexi xmlns:ui="vexi://ui">
    <ui:box tag="outer">
        vexi.ui.frame = thisbox;
        <ui:box id="innerbox" tag="inner" />
        
        var showTag = function(v) {
            vexi.log.info(trapee.tag+": "+trapname);
            cascade = v;
        };
        
        $innerbox.Press1 ++= showTag;
        $innerbox.KeyPressed ++= showTag;
        thisbox.Press1 ++= showTag;
        thisbox.KeyPressed ++= showTag;
        $innerbox._Press1 ++= showTag;
        $innerbox._KeyPressed ++= showTag;
        thisbox._Press1 ++= showTag;
        thisbox._KeyPressed ++= showTag;
        
    </ui:box>
</vexi>

Wednesday, July 11, 2007

Changing Themes

I just made a change to the surface widget that makes theming really really easy should you want a custom widget theme.

<vexi xmlns:ui="vexi://ui" xmlns="vexi.widget">
    <ui:box theme=".path.to.custom.theme" />
    <surface />
    <ui:box>
        // your app starts here
        vexi.ui.frame = thisbox;
    </ui:box>
</vexi>

If you omit the box declaring your theme, vexi.widget.surface will apply a default theme for you. Nice and simple and no hardcoding - and it even opens the door for individual themes per surface with some more clever updates to vexi.widget.surface and the core.

For the curious, the way vexi.widget.surface automatically handles the theming is rather straightforward. If the theme attribute is defined, it applies to as a trap to the namespace vexi.theme, if not it just applies a default theme to that namespace:

// no theme has been specified by the user
if (thisbox.theme == null)
    theme = vexi..org.vexi.theme.win2k;
// set up the theme
vexi..vexi.theme ++= theme;
// apply the theme surface template
vexi..vexi.theme.surface(thisbox);

Fairly easy, eh? The downside is that you must apply vexi.widget.surface for theming to work but the reality is that few (if any) of the widgets will work without it being applied to the root boxes of your applications.

Get The Vexi Platform at SourceForge.net. Fast, secure and Free Open Source software downloads Sponsored by WEBenableIT Java Network Member