The Wisdom of Juan

August 21, 2008

Over the past few weeks I’ve been blessed with the opportunity to work with an awesome guy by the name of Juan Ruiz of Hyro. He’s an exceedingly wise man and has taught me a lot about his trade as a user experience architect. Amongst the many gems that he’s enlightened me with lately, 3 in particular have stood out:

#1

“When presenting/unveiling new designs for review never create an atmosphere that suggests the design is final. Make a point that it is unfinished and invite suggestion. This way rather than criticise, your audience is more likely to buy in and take ownership of the design and thus aid in improving it”

#2

“When taking notes in a meeting always confine them to or collate them into a single list. This way you can succinctly wrap up the meeting with a clear summary of what was discussed and perform a final check that everyone is clear on any decisions made. It also has the added bonus of making the people in the room feel like they were truly heard.”

#3

“You can more easily convince a person of your argument and debunk theirs through the asking of questions rather than simply stating your point of view. Questions have the ability to expose the holes in ones arguments without mounting a direct assault upon the person. Carefully chosen questions will allow the opposing party to convince themselves of the soundness of your own position without causing them to lose face. Furthermore, a person convinced of themselves will thereafter champion your cause their own, as you have created the illusion that they arrived at that conclusion of their own violition.”

    I had a question from one of my coworkers a few weeks back about some basic OO techniques in JavaScript. Seeing as this is a key question from most people getting into JS from a classical OO background I thought I might just share a powerful little concept called the “Module Pattern” to get you all started.

    The module pattern (first identified by Douglas Crockford) exploits the fact that JS only has one kind of scope: function scope. Forget blocks, or any other kind of scope resoultion techniques you might know from other programming languages – it ain’t here. JS allows you create functions within functions (i.e. nest them), and hence function scope is also nested. Any nested function can see anything in the parent function and so on up the tree. It does this through the use of closures which is far to big a topic for this short article, you’ll just have to accept the fact that it works.

    Okay so why is this important? Well JS has a major flaw (some would say feature) in that it doesn’t have any sort of PPP (public, private, protected) functionality. Everything is public. Everything. So how do we achieve any sort of meaningful encapsulation you ask? Isn’t information hiding critical to the design principle “open for extension, closed for modification”? Well the module pattern is one solution to this problem.

    Here’s how it works:

    MyObject = function() {
    
        var myPrivateField;
    
        function myPrivateMethod(someValue) {
            // look ma I'm a closure
            myPrivateField = someValue;
    
            // assigning a private member to a public one
            pub.myPublicField = myPrivateField; 
    
            return "I'm a private method";
        }
    
        /*
         * this is where we define the object's public
         * interface using another bit of JS
         * magic: the object literal
         */
        var pub = {
            myPublicField: null,
    
            myPublicMethod: function() {
                return "Hello World!";
            },
    
            myPrivateMethodWrapper: function(someValue) {
                // calling a private/hidden method
                return myPrivateMethod(someValue);
            }
        }
    
        // here we override the constructor's return value
        // with our public interface object
        return pub;
    
    }
    
    var o = new MyObject();
    o.myPrivateMethod();   // wont work it's private/hidden
    o.myPublicMethod();    // works!

    I know that this will completely weird out some of you OO purists, but when JS is the only thing you’ve got (e.g. in the browser) it’s a pretty useful technique for gaining some sort of PPP control over your objects API.