→ ←
  • Learning Ember.js This blog post was published on November 13 2012. §

    Ember Coding Practices

    C++ is my primary programming language, and I only started picking up Javascript in a rudimentary fashion about a month ago.  Just like all languages-programming or otherwise-there are little things that make the language unique.  Learning these things makes you write better code.  For example, Effective C++, is the standard must read book that contains many helpful hints for writing better C++.

    Thanks to the internet these helpful hints are now in the form of blog posts.  Over the past day I’ve come across many best practices, and idiosyncrasies of javascript including the following:

    • Difference between Function Declaration vs Function Expressions.  Which segues nice to…
    • Hoisting
    • IIFE
    • Module Pattern

    I’ve learned quite a bit from reading these wonderfully written blog posts and they have definitely helped me understand certain parts of the Ember.js codebase that I previously didn’t understand.  The ideas presented above are present all over the codebase and I figured I would outline various occurrences of them.

    First, it is recommended to have one var statement per scope (Javascript has function scope which is unlike C++) at the top of your functions.  This is essentially what hoisting does so it is better to make this explicit to avoid confusion.

    This can be seen in most functions in the Ember codebase.  For example in Ember.Application.createApplicationView() all the variables are declared and initialized at the top of the function.  This is even done for variables that are used at the end of the function, in this case rootElement.  This goes against what I’ve learned where you should declare the variables close to where they are used for many reasons.  In other languages this is mostly done for scoping reasons, which don’t apply to javascript.

    Ember also uses the IFFE pattern quite a bit.  This can be seen in ember.1-0.pre.js, the built ember file.  A lot of the code is surrounded in the following

    (function() {})();

    I imagine these get constructed as part of the build process of ember which is something I haven’t completely worked out yet.

     

    Using the Glider theme.
    • Chicago Ember.js Meetup

      November 15 2012
    • A trace of an {{action}}

      November 14 2012
    • Ember Coding Practices

      November 13 2012
    • Controller creation automagic

      November 12 2012
    • Ember Hello World.

      November 11 2012
    • Building up an Ember App

      November 10 2012
    • Ember Routing Part 1

      November 9 2012
    • Ember Rules of Thumb

      November 8 2012
    • What does using Ember.get() buy you?

      November 7 2012
    • What’s this all about?

      November 6 2012