Wednesday 25 September 2013

Venturing into the Revealing Module Pattern with JavaScript


I came across Addy Osmani's blog post about Learning JavaScript Design Patterns the other day. Loved it!

I think I'm one of those silly developers that gets super-duper excited when I see something that I like and then get to put it in practice. Well, I did exactly that. :)

I loved what Addy said about design patterns:
"Patterns are not an exact solution. It’s important that we remember the role of a pattern is merely to provide us with a solution scheme. Patterns don’t solve all design problems nor do they replace good software designers, however, they do support them".  
Let's get into it then ..

I was writing a rule engine for a part of our application. As you can see in the code below, the sp.editor.rules namespace contains the methods such as MeetsButtonCriteria that I can call.

 
$(function () {
    if (sp && sp.editor) {
        // Initialise the rules namespace
        sp.editor.rules = sp.editor.rules || {};
        
        // Private methods
        sp.editor._AreMultipleNodesSelected = function () {
            // Logic goes here.
        };


        // Rules for buttonize
        sp.editor.rules.MeetsButtonCriteria = function (editor) {
            if (sp.editor._AreMultipleNodesSelected(editor)) {
                // More Code Here ..
            } else {
                // More Code Here ..
            }

            return true; // Temp Return True
        };
    }
});

The namespace also contains other helper methods that MeetsButtonCriteria calls such as _AreMultipleNodesSelected. In the code I simply used a convention that private methods begin with an underscore.

While that seems to do the trick, I still found that rather clunky and non-defensive. This is where I thought that the Revealing Module Pattern might help.

The main benefit of the pattern is that the pattern allows the syntax of our scripts to be more consistent. It also makes it more clear at the end of the module which of our functions and variables may be accessed publicly which eases readability.

So this is what the modified script looks like.
 
sp.editor.rules = function () {
    // Declare the Private Functions Here!
    
    var areMultipleNodesSelected = function () {
        // Logic goes here.
    };
    
    var meetsButtonCriteria = function (editor) {
        if (areMultipleNodesSelected(editor)) {
            // More Code Here ..
        } else {
            // More Code Here ..
        }

        return true; // Temp Return True
    };

    // This is what gets returned (REVEALED).
    return {
        MeetsButtonCriteria: meetsButtonCriteria
    };
}();

Easy-peasy na? Also, just a word of advise, I forgot to convert sp.editor.rules to a self executing function and spent like 15 minutes trying to figure out what was wrong. Yikes! So don't forget that.

Sunday 8 September 2013

Hack-ED with Kirk Jackson & Andy Prow


TechED 2013

I recently attended 2 amazingly fun filled Hack-Ed sessions at Microsoft's TechEd, Australia. The speakers were Kirk Jackson and Andy Prow. And as promised, there was live hacking on stage and awkward humour that will made us cringe (in a good way). Not to mention, I won a chocolate bar! Yay!

I have posed a link to both their sessions below but I though I'd quickly touch on a few important things that they spoke about. I also highly recommend you follow their blog at http://www.hack-ed.com/.

Kirk and Andy went through a couple of recent security breaches around the world (I've listed a few below). But what was really funny was that most of them were easily preventable.

I know you've probably heard this before, but as web developers, security should not be an after-thought to the development process, but rather an integral part of your design. I highly recommend that you have a read through the Open Web Application Security Project (OWASP). They have recently updated their list of top 10 vulnerabilities to look out for, available at https://www.owasp.org/index.php/Top_10_2013-Top_10.

Interesting Security Breaches

Now here are some of the interesting attacks that Kirk and Andy mentioned.


Some interesting types of attacks


Security Sessions at TechED

Hack-Ed: Wheedling and Cajoling your way to Success (http://channel9.msdn.com/Events/TechEd/Australia/2013/ATC232)


Hack-Ed: Develop your Security Spidey Sense (http://channel9.msdn.com/Events/TechEd/Australia/2013/ATC233)




Thursday 5 September 2013

I got 99 Problems but Visual Studio ain't one


Stuff from Microsoft TechED Australia 2013

So this is my first post from this year's Microsoft TechED. This being my first time to an event such as this, I've been super pumped and excited all along. The excitement and energy at the event is almost palpable.

The sessions I have attended thus far have been crazy fun. I must admit, when they previewed new features in Visual Studio 2013 I almost felt week in the knees!

Visual Studio 2013 comes with a swanky new CSS and HTML Editor with amazing type-specific intellisense. They've really taken intellisense to the next level with intellisense support for your JS Object model.

And added crazy new features, features like "Browser Link" can be described best as black magic. It leverage's SignalR and allows you to EDIT ACTUAL SOURCE CODE FROM WITHIN THE BROWSER! Say what!!!

Most of the older zen-coding features still remain, but even better, the "Format Selection" feature is so much more intuitive (I know it seems trivial but I really struggle with badly formatted code).

By the by, Visual Studio 2013 RC has been released and is available at [http://www.microsoft.com/visualstudio/eng/2013-preview#story-2013preview]. Don't forget to check out the Channel 9: Visual Studio 2013 RC video. You also might want to have a look at Scott's post about browser link (http://www.hanselman.com/blog/VisualStudio2013RCForWebDevelopersOneASPNETBrowserLinkAndOurDirection.aspx)

Loved Colin's sessions on WebAPI. Got some great pointers that I'll share in a subsequent post. Here's a sneak preview ..



Brendan's "Real Time web applications" demo of what one can achieve with SignalR was pretty impressive too. The source for his demo is at  http://t.co/EFCcrlqkoo. Brendan mentioned some interesting libraries like DurandalJS, PostalJS, Toastr, RequireJS (tutorials coming soon)

Just attended Scott Guthrie's "Building Real World Cloud Apps with Azure". Boy, does he have a presence! So much cooler to see/hear him in person instead of streaming him! Links to the source code from his presentation coming soon!

Finally a shout out to Tony Gooderham. Without your tweet I wouldn't have this title!

The sessions I plan to attend/have attended this week are as follows:

Tuesday, September 03 2013

3:15 pm - 4:30 pm
Developer Kick-Off Session: Stuff We Love Andrew Coates, Mads Kristensen, Brady Gaster, Ed Blankenship, Patrick Klug Arena 1A options

4:30 pm - 6:00 pm
Keynote: Blink and you’ll miss it Adam Pisoni Arena 2 options

6:00 pm - 9:00 pm
Welcome Reception

Wednesday, September 04 2013

8:15 am - 9:30 am
What’s New for ALM in Visual Studio 2013 and Team Foundation Server 2013 Adam Cogan, Damian Brady Central A

9:45 am - 11:00 am
What’s New in Visual Studio for Web Developers Mads Kristensen Arena 1A

11:30 am - 12:45 pm
Designing API-Enabled Applications Colin Bowern Meeting Rooms 5&6

11:30 am - 12:45 pm
Cross-Device Notification Services with C#, Xamarin and Windows Azure Mobile Services Simon Waight Arena 1A

1:45 pm - 3:00 pm
Developing Connected Apps with Windows Azure Mobile Service: Overview Nick Harris Central C

3:30 pm - 4:45 pm
Real-Time Web Applications with SignalR Brendan Kowitz Central C

5:00 pm - 6:15 pm
So, You Want to be a Professional Windows Phone Developer? Nick Randolph Central C

6:30 pm - 7:45 pm
Continuous Delivery - The Agile End to End Story for Developers & IT Pros! Morgan Webb Arena 2

Thursday, September 05 2013

8:15 am - 9:30 am
Building Cross-Platform Mobile Apps David Burela Central A

9:45 am - 11:00 am
Becoming a C# Time Lord Joseph Albahari Central A

9:45 am - 11:00 am
Cross Platform Mobile Web Development Brendan Kowitz Meeting Rooms 5&6

11:30 am - 12:45 pm
Climbing the Agile Testing Ladder featuring Visual Studio 2013, TFS 2013, Microsoft Test Manager 2013, and Lab Management 2013 Adam Cogan, Damian Brady Meeting Room 7

1:45 pm - 3:00 pm
Building Real World Cloud Apps with Windows Azure Part 1 Scott Guthrie Arena 2

3:30 pm - 4:45 pm
Unit Testing, Code Coverage, and Code Clone Analysis with Microsoft Visual Studio 2012 Richard Angus TLC - Theatre 1

5:00 pm - 6:15 pm
Load Testing with Team Foundation Service Anthony Borton Meeting Room 8

6:30 pm - 10:30 pm
Networking Event Thursday Networking Event Movie World

Friday, September 06 2013

8:15 am - 9:30 am
Using AngularJS in an ASP.Net Application Paul Glavich Meeting Room 6

9:45 am - 11:00 am
Hack-Ed: Wheedling and Cajoling your way to Success Kirk Jackson, Andy Prow Meeting Room 7

11:30 am - 12:45 pm
Hack-Ed: Develop your Security Spidey Sense Kirk Jackson, Andy Prow Meeting Room 7

1:45 pm - 3:00 pm
Azure Mobile Services Deep Dive into Node.js Scripting Glenn Block Meeting Room 6

3:00 pm - 4:00 pm
Closing Presentation: Cloud Computing: Own Tomorrow Scott Guthrie, John Azariah, James Miles Arena 2