KnockoutJS vs AngularJS

Luckily (or unluckily ? ), I keep getting opportunities to work with everything  other than Angular. In my latest assignment, I have had the opportunity to work with Knockout. I have to say that I liked it and would have picked Knockout as my favorite framework if Angular did not exist.

People keep asking these questions and probably there is a good reason for it. Humans hate choices. If there are resources out there which help them make these choices easier, they like it. So, if you havent already tried the two frameworks, here are some comparisons / differences betwen the two.

Templating 

Angular and Knockout both provide DOM based templating.

Angular only supports HTML as its templating mechanism.

Knockout by default uses HTML as its templating mechanism ( which is where they both have DOM based templating ) , but Knockout also supports other string based templating engines like Handlebars etc (but I think hardly anyone uses that!).

Unobtrusive 

Knockout tries to please people who are interested in Unobtrusive Stuff.. http://knockoutjs.com/documentation/unobtrusive-event-handling.html

Angular doesnt believe in it ( or the authors of angular do not make any exceptions to provide for unobtrusive cases. )

Context 

In knockout, in order to fetch data from a context that is in the parent of your current context, you need to access it using `$parent`.

In angular, context’s inherit prototypically. So, to access something from the parent context, you dont usually have to use $parent . You only need to use $parent if you have another variable with the same name defined in your current context as well.

Directives or Custom Bindings 

Angular has directives. You can define custom elements, custom attributes, comments and classes.

Knockout doesnt allow you to create custom elements. You have to create all your bindings inside data-bind. ( I am not sure about this. Someone can correct me if I am wrong! ).

JQuery Dependency

Angular has its own version of JQuery called JQueryLite ( smaller jquery). If you include JQuery before Angular, it will use the JQuery you loaded.

Knockout does not depend on JQuery or create its own version of it. But, having said that, Knockout is a data-binding library. It does not have any opinions or implementations for doing Ajax or DOM querying. If you don’t include JQuery, you are free to do ajax and DOM manipulation either directly or with some other library.

Partitioning or creating new contexts 

Angular has a concept of controllers which allow you to create new contexts explicitly. Angular also implicitly creates context’s when using ng-repeat, ng-switch, ng-include etc. Angular’s controllers, services and its concept of DI make it extremely easy to partition and manage large applications.

Knockout doesnt allow you to create contexts explicitly (Unless you create custom bindings). Knockout implicitly creates new contexts when using for-each etc.. This also means that Knockout has no opinion regarding the overall structure of your application. Without careful planning its easy to “mess up” your code base with Knockout.

Browser support 

Angular supports all new browsers and its support goes back till IE8.

Knockout supports till IE6.

Batteries included 

Angular is a full fledged framework. It provides dependency injection, ajax, routing, cookies, promises and much more.. Angular is great for testing.

Knockout is a data-binding library. To go for a full fledged framework have a look at DurandalJS – which is a combination of Knockout, JQuery and RequireJS. Knockout was not built with testing in mind. So, it doesnt score soo many points in that department. But still, I think it wont be terrible.

Documentation 

Angular documentation covers pretty much everything it has. Angular is huge.. Its a shift in paradigm.. in the way of thinking itself. So, no matter how much documentation you have ( and we dont have enough ! ) you will always want more.. Certain core pieces are missing from the documentation.

Knockout’s documentation is the most praised in the community. But… ( yes, there is a but! )… Knockouts documentation for all the binding syntax is amazing, extensive.. But, when it comes to the utilities that knockout provides, there is zilch.. No where can you find documentation for those.. Either the documentation doesn’t exist or it is in such an obscure place that its hard to find.

IDE support and debugging support

Angular has amazing plugins available for all Intellij IDE’s. There is also a plugin available for Visual studio, which I believe is a port of the Intellij plugin. Angular also has a Google Chrome extension called Angular Batarang which allows you to select any element in the developer tools window and preview its scope variables.

Knockout doesnt have any popular IDE plugins which help in developer productivity.. I believe it is mostly because it used a single data-* attribute as opposed to Angular which uses different data-* attributes for different tasks.  In terms of plugins, Knockout too has a Google Chrome extension, but I am not sure it works as seamlessly as the Angular Batarang. I tried it, did not work very well for my use cases, so I dropped it.

If you want to debug knockout in your browser, here is a tip (Google Chrome):

F12 ( Open developer tools ) 
Select / Click on the element you want to inspect.

Open the console.

The current highlighted element is available in the console as

 $0

In the console, type

var context = ko.contextFor($0);
context

context will now have your context information for that element. This context will give you ideas of what and how you could use it in the data-bind attribute.

Community Support 

Ok, this is going to be controversial. But, I believe this is based on facts. So here it is.

Angular has a bigger community. No doubts about it. If you ask a question, there is a high probability you will get an answer.

Knockout has a comparatively much smaller community.

For me the measure of an open source projects community is a function of 3 things.

1. Github stars and forks

2. Stack Overflow questions

3. Google groups – number of posts per week and number of members.

Angular wins hands down in 1 and 2. In SO questions category, they are very close.

Ease

You should understand that Angular is a full fledged framework while Knockout is a data-binding library. There are way too many concepts in Angular and it forces you into a totally different way of thinking. This is not easy.

Learning knockout is easy. The simple things atleast. I think one of the problems with Knockout is that there aren’t many articles / blogs about it. Not in the 100′s atleast. If you need to do something obscure, you are on your own. Ofcourse asking about it on SO will help. The same is also true about Angular. But, there is a subtle difference. Angular is meant for these obscure things.. custom bindings etc.. So , over time you will find more and more documentation and blogs for it. All over the world wide web, if you look for Angular, you will see people talking about directives.. which are custom bindings.. Not the same case with custom bindings in Knockout. I am sure people use it, but they just dont talk about it much.

Variable Observation

Angular observes variables using a technique called as dirty checking. On the other hand, Knockout uses the Observable pattern. In terms of code

Angular HTML :

or

Angular javascript :

Knockout HTML :

<div data-bind="text : myContent"></div>

Knockout Javascript :

To change this content..

In Angular :

In Knockout :

Events and custom parameter passing :

Angular HTML

Angular Javascript

Knockout HTML

or

 

Conclusion

I love similes and metaphor’s.  In my Angular vs Backbone post, I said that if Angular could be consider a Motor Boat with all its complexities in machinery and mechanism, Backbone could be compared to a Life Jacket. Backbone forces you not to drown, which is why it was compared to a Life Jacket. Knockout’s focus isnt about your drowning. It is about your speed which is why it doesn’t care whether you drown or not. Images are better than words.. So here is the comparison.

Backbone : 

Backbonejs

Backbone is a life jacket

Knockout :

Knockout

Knockout is a surfboard with a motor

Angular : 

AngularJS

AngularJS is a cruise ship

Backbonejs vs Angularjs : Demystifying the myths

I love the way how each and every discussion turns into the war of the frameworks. I have worked with BackboneJS and AngularJS quite extensively and have come across most of their short comings in production. So, you dont really have to take this analysis with a grain of salt. I would be happy to take back anything that I have said if it turns out to be wrong. Lets get to the meat of it, then, shall we?

NOTE :  I am not trashing Backbone. It was a great tool earlier and in my opinion, its usefulness has diminished since the introduction of newer frameworks like Angular and Ember.

I know that people in general, still choose Backbone over Angular because it seems to be the safer choice, because you cant find as many X-framework developers as Backbone etc etc.. I am attempting to make an argument that Angular is a better choice in all scenario’s where you would use Backbone. Finding developers and how much you pay them is an organizational choice. But, my attempt here is to prove that, if you have 2 smart developers who have no knowledge of Angular and pick 4-5 Dev’s who know Backbone for a largish project, the investment of learning Angular will pay off in the long run. Less code to write means less code to test means less code to debug means less code to maintain. It also means a lesser page-weight at the end of the day. These are some of the advantages you should not ignore if you are making an objective decision.

Here are the myths and my explanations to counter them.

Backbone is very small / compact.

One of the first points that people quote, when they talk about the advantages of Backbone is its compactness. Its just xKB minified and gzipped. Lets see some stats, shall we?.

Angular ( 1.0.3 ) is 77KB minified. If you include Angular ( just that one file )it WILL work on its own.. It does not depend on anything.. If you include JQuery before Angular, then angular will start using JQuery for DOM manipulation. But, including JQuery is upto you. You dont necessarily have to.

Angular size

Backbone by itself is around 18KB minified (link). Now add in its dependencies Underscore ( 13.6KB minified – link ) and then we have JQuery ( 93KB minified)  / Zepto ( 25KB minified ) itself ( You cant use Backbone on the client side without one of these! )

To note here, Backbone has a much stronger dependency on JQuery than Angular. Some people might complain that even Angular can use JQuery or people generally tend to include JQuery along with Angular in projects etc etc.. People do, but its not absolutely necessaryA Front End project with Angular can make do without JQuery.

Here is what you get, when you decide to write an application with Backbone as your framework. 5 Classes. Model, View, Events, Collection and Router. And Backbone.Sync  ( but lets leave it out for now ) . Mainly, this is what constitutes Backbone itself. That’s it. These are all Base Classes. When you want any of these functionalities you extend these classes and make use of them.

If you think that this is it, then, you are wrong, again!. There are more things that you would have to add ( depends on what you are building! ) or write yourself! . I have in each scenario shown the additional weight ( in KB ) for each Backbone plugin you need.

Backbone is minimalistic.

Oh, this is the biggest farce in the history of mankind, perhaps second only to the belief that earth is the center of the universe. This is what I usually tell everyone who argues that Backbone is minimalistic and gives them a lot of wriggle space. If the framework doesn’t write the code, YOU have to.

If you have to write the code, you cannot stand on the shoulder of stalwarts / experts in the field. Also the community would have found the best way of doing some repetitive task. If you are an expert, then probably yes, you could. But even if you are an expert, you would be an expert in one particular thing or two. You cant be an expert at everything, which means standing on the shoulder of a collection of experts seems like a sane choice.

Oh well, but you argue “I dont have to write them on my own. I  can stand on the shoulder of stalwarts. Backbone has plugins.. Millions of them. I can choose to include the ones that I want, when I want them“. Hah, Intelligent. Please note that choices have costs associated with them. Each time you need to make a decision, it costs. There is an overhead in making that decision. Choosing Backbone means you have to make a hell lot of decisions, so the cost is high. The interesting question is : What do you gain by having the freedom to make these decisions? Let us see some of the choices you need to make :

1. Application Architecture / Layout Structure / Memory management: 

Backbone is a meta-framework. It provides only the “backbone” and no flesh. This means that you would either have to put in the effort to write your own along with the application or depend on something like Marionette (21KB)  or Chaplin (56KB) or Knockback ( to use Knockout! ) (32KBmin+gzip includes everything except jquery. ). If you don’t use these, then memory management, layout management, application structure, global event bus and such architectural decisions are left to you. You would think that this is good because it gives you more freedom to do whatever you want. I kind of disagree with this.

Just for comparison, in Angular.js, unless you are doing something extremely complex ( which wont happen unless you are writing something really really big! ) you would never have to worry about memory management, layout management or event bus. Its all taken care of for you.

2. Nested Models : 

Did you know that Backbone.Model by itself does not support Nested objects ? What I mean is, say if you have a Registration Form in your site and the form obviously took in the user’s details. How would you represent the user in a model? You would do something like

var userModel = new Backbone.Model.extend({
firstName : "John",
lastName : "Smith"
});

What if for some reason ( or for some other attribute )you wanted to do something like

var userModel = new Backbone.Model.extend({
name :  { first : "John",
          last : "Smith"
        }
});

How does Backbone notify you when something changes? With events. If something changes in the model, it fires a change event. In the first scenario you would do something like

userModel.set("firstName","Tom");

and Backbone would fire a change event, which you can catch as

userModel.on("change:firstName",firstNameChangeHandler);

How would you set the value in the second example? You would think that, you can do something like

userModel.set("name.first","Tom");

But, this is not possible in Backbone. So what you would try next is

userModel.get("name").first = "Tom";

This works for setting the value, but.. No Event is fired in this case, which means that , there is no easy way of notifying your views. This is something you have to fix because of Backbone’s opinion! What is the solution? Well, either you have to patch it and figure out a way of doing this in places where you want it to work by somehow working around and coding around this limitation or use something like NestedModel (2KB minified) or DeepModel or Backbone.Relational (seems to be the most popular solution! 56KB not minified!) or fix it yourself in your code.

The same is pretty simple in angular. You could either do :

$scope.firstName = "John";

or

$scope.name.first = "John";

It just works.

3. Data-binding.

There is no inkling of data-binding in Backbone. You have to wire up your view’s and model’s yourself. This is one of the most common tasks in any application and probably an opinion about this would have helped Backbone a LOT. Without data-binding ( and if you dont choose to implement a plugin or include one ), here is how you would do it for each view element <–> model binding.

var userModel = new Backbone.Model.extend({
    name : { first : "John",
             last : "Smith"
    }
});
var userView = new Backbone.View.extend({
    events : {
        "change input#firstName" : "changeFirstNameHandler"
    },
    initialize : function(options){
        //write initialization here.
        this.firstNameInput = this.$("#firstName");
        this.model.on("change:firstName",_.bind(this.onFirstNameChange,this));
    },
    changeFirstNameHandler : function(event){
        this.model.set("firstName",this.firstNameInput.val());
    },
    onFirstNameChange : function(){
        this.firstNameInput.val(this.model.get("firstName"));
    }
});

Note that, you will have to do this ( or something similiar for each binding you need… ) or use one of the data-binding plugin’s for Backbone. Your options in plugins are ModelBinder (11.1kb )/ CollectionBinder (5.5kb ) or Backbone.stickit ( 3KB ) ( Note : this does not have a solution for collection’s ).

Do I need to talk about data-binding in Angular? I guess not. If you dont know about it, check out Angularjs’s home page.

4. Non-Restful Backend : 

Backbone is meant for Restful backend’s. The idea behind backbone is that for each end-point on the backend you create an associated model on the front-end and you create, read,update and delete the resource at the end point using Backbone.Model. This is one of the strongest opinions Backbone has. Unless you are building an app that is dealing with a Restful backend that uses the http verb’s appropriately, you are in for some pain here. If you are not working with a text book defined Restful backend  - be ready to do a lot of tweaks and overrides to the Backbone.sync. You could technically use $.ajax but there is no clear cut place to encapsulate it, which is why most times using it feels like a hack.

Angular has $http which you can use to do anything you want – not just Restful. To make restful easier, angular has ngResource which is an additional file that you need to add ( 16KB min ). So it has its cost, but you can choose to not use it and use $http directly. Its easy either ways.

5. Validation.

This is, in my opinion, related to binding. But, to be fair towards Backbone, it does expose a function in the model to do validations. Ofcourse, like everything else in Backbone you have to write the logic there yourself ( which is fine in this regard! ) but the lack of binding’s irk’s us here as well. So, once again, its either, implement your own or pick and choose from Backbone.validation( 8.2KB min ) or Backbone.validations ( 9.5 unminified ) or Backbone.validator (8.2 non-minified) or any of the other ones you can find.

Angular includes validation, its built in and it has a few validations already implemented like email, max-length, min-length, required etc. If you want some other custom validations you can write your own or use this. ( Note :  You dont have to include the whole Angular-UI if you want to get just validation! This is the beauty of Angular. Everything is soo modularized that you can just take what you want and its extremely easy to pick that out. If you just pick out the ui-validate directive, then its just around ~20 lines of code so I wont bother mentioning the size!).

5. Templating.

A custom templating solution is not an absolute must for Backbone but generally people choose one over underscore’s default templating engine. Most commonly used templating solutions are Handlebars (60 unminified  - This is the templating engine used by emberjs and sproutcore!) / Mustache ( 15.6 unmini ) / Dustjs ( 25.8 mini ). If you dont include any of these templating engine’s and prefer to choose underscore over them, then you have no additional overhead in terms of file size but still you have to make that choice and that choice has a cost. The cost of deciding it.

Angular does not need any templating engine. It uses the most famous templating engine in the world  ( HTML ) by extending its vocabulary wherever necessary. Not just that, it converts your HTML into a DSL which also makes it easy for people to understand it.

Backbone is not opinionated.

Even if you use Backbone you cant do whatever you want and if you do it, then using Backbone seems pointless because, at its core, Backbone IS opinionated. You have to extend Backbone.Model and you have to extend Backbone.View.This IS an opinion as to how you should break up your views and how you should organize your Models. Additionally, I have already covered earlier some of the opinions that Backbone forces on you.

Angular is opinionated. It does not do any claims to be otherwise. There is an “Angular way” of doing things. One of the best is NO DOM MANIPULATION IN CONTROLLERS. Remember that, and all the rest will fall in place automatically.

Backbone has plugins.

This is true. There are quite a few plugins for Backbone, some of which I have already mentioned earlier. For each specific task you need to do, you pick the relevant plugin and use it. The thing with a plugin model is that most of these plugins are at different states of production. The thing you are not aware of at the outset is that, do any two of these plugins work with each other? If you are choosing one of these plugins for a major task ( lets say one of the tasks I mentioned earlier! ) that is a pretty big decision in terms of your architecture. It changes the way you write your code. What is the support level for this plugin? What is the amount of test’s that this plugin has? How is the community behind this plugin? Do a lot of people in the wild know about it / know how to use it? One of the things I have found is that, though people have generally flocked towards Backbone, due to the very nature of people who flock towards it, there is no general consensus of how to achieve a particular task which leads to fragmentation of brain mass, which leads to paltry support at best in case of most plugins. Backbone has a wiki which lists a few of the popular plugin’s.

Most of what you want is already provided by Angular. So, there are very few plugins. If you want something that is not already natively supported by Angular then check out Angular-UI. It has a pretty good list of directives ( the plugins of angular world!) and perhaps a few services and filters too. Like I mentioned earlier, you dont have to include everything from that library ( and probably should not too.. ). Only include the code necessary for the directive you want into your project.

Angular Shortfall.

To be fair, the only scenario where Angular has no solution natively is animating view transitions. Backbone does not have an opinion here either. But since it relies on JQuery for DOM manipulation it can use animations as a bonus. If you want animations in Angular, you would have to implement one, use jquery ( or its animation engine ) or pick some other animation library like greensock. Even then its not a trivial task. But people have done it ( see here and here )

Some shortfalls of Angular : 

1. Angular requires a deeper knowledge of computer science than most other frameworks. You need to understand the DOM at the node level, to understand angular properly while most other frameworks are happy to let you abstract that away at the level of selectors.

2. DI, services, no DOM manipulation anywhere other than directives, directives, filters are all new concepts to client side dev’s. Its all magic sometimes.

3. Form validation framework of angular is not perfect yet. It still needs some tweaks. Its almost there ( 90% ) but there is that 10% that is still not there yet.

4. No transitions / animations..yet… Its coming in the very near future. Angular now has support for animations (link).

5. Writing directives is not easy. Its the most complex part of writing Angular code.

Conclusion :

Here is an analogy. Starting a project is like being at sea. Your job is to get to land, not to complete the task of swimming. The difference between Angular and Backbone is the difference between a Motor Boat and a Life Jacket. A Life Jacket is great. It will perhaps aid you in swimming and probably protect you from drowning. On the other hand, a Motor Boat? Do you see the difference? Yes, if you have a Motor Boat and no Life Jacket then you have the fear of drowning, but, that’s only if you cant swim when the motor boat stops working for some reason. Its upto you to choose, whether you want a Life Jacket and swim all the way to the shore or use a Motor Boat and ride to it.

AngularJS Vs EmberJS

One of the battles we are seeing the developers fight these days has been for the Throne of JS framework. The interwebs keep fighting these wars and in the end there is never a proper victor. But still we persevere because that is the nature of humans. The contenders in this particular war are Backbone, Ember &  Angular ( there are other smaller colonies but for this discussion and for the sake of brevity we shall leave them out ). . Alteast, these are the 3 most popular MV* javascript client-side frameworks according to github watchers.

This post is not mostly about Backbone because a lot of people have already discussed it at length, come to various conclusions and moved on. Even the inspiration for this blogpost compares Backbone with Ember. I myself have written a longish answer on StackOverflow about the topic.

Disclaimer : I have never worked with EmberJS. So you might deem that I am not the proper person to talk about this. Continue at your own risk. 

This post is really a response to what the inspiration talks about. This is a response based on what I managed to assimilate based on that post. So lets begin…

Backbone and Ember

To set things straight first, after having gone through that post, one thing was clear to me. Ember apparently does , in fact, do more ( much, much more! ) than what Backbone does for any developer. One of the toughest things to do, when starting a project, is making choices. Ember takes care of that for you. It has already made choices and it has already prescribed those choices to you, which means that there is “an  Ember way” of doing things. You stray from this god-chosen path and you are left to yourself.

Backbone on the other hand does not make any choices for you which means that you have freedom to choose. Freedom means that you are in fact wasting quite a lot of time making these choices yourself ( and in the end taking responsibility for those choices ). One thing we have to remember is, the average developer out there is like a sheep. He will never know the boundaries of the grazing field. This means that giving him more freedom will eventually lead to him going astray and never knowing that he actually did. Even in restricted environments, you can do mistakes but lets just say that the chances are less. You would argue that , if there is a lot of freedom, the average developer will be more careful. Yes, he would be when he has infinite time and not when there is a manager behind his back pointing a gun to his head. So, restrictions are good. In fact, they are a blessing. Atleast now you can blame the framework.

Does Backbone have two way data binding? No. Ember does.

Does Backbone have composed views? No. Ember does. In fact backbone has no idea about your DOM. You are left on your own when it comes to DOM.

Does Backbone have a complex model structure ? No. Its model structure is the simplest possible. Its “observe” mechanism arises out of setters and getters that are defined on the base Backbone Model Class. Ember does have a mechanism for declaring complex model structures. It has a totally separate module (Ember Data ) to take care of that.

So far we have established that Ember is in fact a far superior choice to Backbone. Now lets try and take apart Ember, shall we?

Language

Ember is / has been put together with a lot of thought behind it. One of the major things that I noticed when going through trek’s post was that Ember used concepts like Ember.Object.extend(…). If you notice what is being done here, you will understand that we are trying to bring into a language a concept that it refuses to bring in. A class. In fact it explicitly calls it a class ( Note the use of reopenClass ). 

As opposed to this, angular uses a concept that defines javascript. Its prototype chain. It uses a $scope and each scope you create, prototypically inherits from its parent scope all the way upto the RootScope. Most of your activity is done on this object. There is no confusion regarding “this” because you dont really have to use it in most cases.

Data-binding and polluting the views

This is one of the most infamous complaints I have heard of in terms of what Angular / Knockout do to the HTML. In the name of 2 way data binding they pollute the beauty, the sanctity of HTML and add their own shit in between. In terms of templating systems, there are two kinds. DOM templates and String Templates. These string templates take strings , take the data and combine them together and produce a pure html which is then pushed into the DOM.  To those who talk about the pureness and sanctity of HTML / views, I have a few questions. Why the f**k do you really care about it? What you should be caring about is your productivity and how it looks / appears to your user. Lets say you do have some valid reasons. Have you had a look at the code you have to write to produce these templates in handlebars, mustache, underscore or jqtemplates? They are all ugly as shit and even worse. Just to add, computed properties do exist in Angular as well.

Composite Views

How do both the frameworks compare in terms of Composing views and nesting views inside of each other. As far as my understanding goes, Ember has a way of defining these using outlets (with names if you need multiple ). In your handlebars templates you define outlets which can further be installed into the current view as and when required. Swapping and replacing of these outlets is the way to go if you are into Ember. Angular, since it does everything with HTML itself and calls itself Enhanced HTML provides you more attributes that are self explanatory. If you see an attribute called ng-switch or ng-repeat what comes to your mind? Yeah, you are right. They both are responsible for exactly what you thought.

This is how you define a repeated element in handlebars / ember

<code>&lt;script type="text/x-handlebars" data-template-name="contributors"&gt; 
{{#each person in controller}} 
{{person.login}} 
{{/each}}
 &lt;/script&gt;</code>

This is how you do the same thing in angular

<code>&lt;span ng-repeat="person in persons"&gt;{{person.login}}&lt;/span&gt;</code>

Which do you think is cleaner? Ofcourse, in the final rendered html the ember way  above is going to look cleaner because all the other stuff is going to get stripped out. In case of angular, all of this (except the thing in double flower brackets ) is going to stay. But, seriously, who is having a look at your final rendered HTML? Just so you know, that who script tag is going to still exist in your final HTML too , but somewhere else.

DOM Manipulation / the ugly part of client side coding

Whether you agree or not, manipulating the DOM is the most ugly part of writing code in JS. By now most of us are so used to it that it has become part of our life, part of the way of our thinking about DOM. Ofcourse, just to encourage you not to do it, IE / M$ had put in all kind of hurdles in front of you and earlier not many tried crossing that barrier. Finally, one day a great leader ( JQuery ) came forward and showed you a way of crossing the sea ( They even parted that sea for you so you can cross) you are now so accustomed to this life across the sea. What if I told you that you can achieve all the things you have been achieving after getting access to these new tools WITHOUT using them? Ofcourse, you still need those new resource. But, use it only when its absolutely necessary. Not everytime.

Component Architechture.

Ember does not provide you a component architecture. It does not provide you a way of defining new components. It helps you define views , even composite ones but that is not one and the same. Angular’s directive mechanism though agreeably raw currently allows you to provide powerful layouts and widgets. This provides a  mechanism for defining terse html code that makes more sense to the reader. It also makes it easier to partition tasks. You can have a component writer and a layout builder – 2 different people doing different tasks they have expertise with. We could even have an open source project which provides you those controls out of the box which anyone can plug -in (just like jquery plugin’s but this one is used declaratively! ).

Dependancy Injection

This is something new in the world of JS. As far as I can tell, not many ( I dont know of one that has DI ) have implemented DI in JS. I will be bold enough to declare that Angular is probably one of the only mainstream framework to implement DI. I am not even going to talk about DI in Ember because it doesnt seem to exist. Those who are new to this system seem to find this to be totally absurd or magic. How can it work? Do we declare everything as global and then input those global variables into these controller functions for the heck of it? Apparently not.

DI makes it extremely easy to manage dependancies. There is a central system (which perhaps is global) that when asked for an object of a particular type gives you that object. What do you do in a typical scenario of OOP. If A has a dependancy on B, you try to figure out what is the best way of getting B into A. Sometimes you do that communication through events, sometimes through an interface etc etc. In DI , A asks the DI system for B and the DI system gives it back. But now the question is ,how does the DI system know what to give back? This is done when A or B is created. They register themselves with the DI system with a Unique Name. Lets say DI has an internal map which has a mapping between the name and the Object / Class to instantiate. So whenever you do a DI.give(“A”) it instantiates this A and gives you back that A.

Conclusion

In Conclusion, it looks like Angular provides  you whatever Ember is providing and more. It also has a smaller footprint ( 25kb vs 37kb), reduces even more boilerplate than what Ember does ( remove all DOM Manipulations!). Just to note : Ember is not done yet because they havent hit 1.0 yet. So we still need to know how they evolve. But it looks like it was created for large applications and helps manage more things than Backbone does, I would even call it an opinionated Backbone on steroids but I dont see it touching the levels of Angular. I dont say this out of malice towards Ember but because angular is showing itself to be a class apart. It dared to think differently and that paid off.

Contribution to the world

So I was thinking about the contribution we make to this world, statistically / mathematically and this is what I was getting at.

Your Contribution to this world = ( The contribution of your product or service to this world ) / The number of people working on that product or service.
So now the question is , how do we figure out the contribution a product to this world. For my calculations, this is what I will assume.

(From here on, we will assume for practical purposes a service as equivalent to a product )

Independent Contribution of a product (Cp) = Amount of people the product is used by / ( No of users of product 1 + No of users of product 2 +… and so on ..)

Seems like a very tiny quantity doesnt it? :)

Well, but we for sure feel that certain products enable other products.. For example, electricity.. If electricity was not there a lot of current day luxuries would be missing isnt it? Even if we assume that around 5 Billion people make use of electricity in the world ( and also assuming that the population of the world is 6 Billion ) , electricities contribution to this world seems smaller.. But what if we add the numbers of all the products which are derived from electricity? For example, internet, bulbs, Google? Now adding all these to the contribution of electricity..

ω ( Electricity) = Cp ( Electricity) + Σ (ω(Product affected by Electricity) );

Under this new equation, you might already have observed that, if you are working on a product in a large company, the chance of the product making a contribution to this world goes up slightly but your contribution to this world goes down proportional to the number of people who are employed by your company. So the bigger your company, the smaller will be your contribution ( even for the product that is true right? ) .  While, if you are working alone on a product , the chances of the product making a significant contribution to this world goes down but since you are the only contributor to it, the denominator is 1 ( or if you are in a group of 2-3 people , then use the appropriate number ).

Now go ahead, figure out your contribution :D Well, enough ranting huh!

Parallel

So I was thinking about paralllel processing… You know.. Just the kind of thing that I do while riding. Was wondering where and how the processing power will be increased in the future so that the semiconductor industry can adhere to “Moore’s Law“. Seemingly, over time, it is going to get more and more difficult for them to reduce the size of the processor. So i’m guessing they would start increasing the size. Confused?

Well, let me explain. I believe we have already seen the first generation of parallel processors. The current quad core, dual core are the first examples of it. But, lets say tomorrow, we get a 16 core processor ( I am assessing its already there? ). So now, are programs making use of this? Since most of the computer programs that are currently running are built for single processor systems, ofcourse they wont be making use of this feature.

But since we already know that tomorrows processors are going to be multi-processors, shouldn’t the programming languages anticipate that and provide constructs so that if ever such proccessors come up in the future, the programs should be able to make use of them?

We humans, have a dream. Atleast we programmers , I assume, do . Why cant we make the current computer as “smart” as the brain. So can we achieve the levels of a human brains ( or atleast a rats? ) with a huge multiprocessing system?

But then, writing  a system that can work in a multiprocessing environment is not a trivial task. The mental acumen required to do that is not trivial either. But the traits of humans is that we are not born geniuses. We learn. So if even one person in the world deciphers an easy way to do it, the rest of us will follow through. But then again, who will bell the cat , huh!!

Reblog this post [with Zemanta]

Vicky Christina Barcelona

I am a great fan of “thinking out of the box” and I saw a particular concept in this particular movie that seemed like totally out of the box.

I have heard that for us humans, the idea of exclusivity in partners is something that comes genetically. We dont like to share our partners with anyone ? Do we? Ofcourse there are a few individuals who are ok with the idea, but broadly, no one wishes to. We also see a lot of “fall-apart”s because of this. Also because there is this small tiny ingredient that will be missing in most relationships that make it incomplete and this chasm grows wider and wider over the years. Either the individuals involved in this relationship adapt to this “incompleteness” and adjust to it or go separate ways when the chasm becomes so wide that they wont be able to jump to the other side.

Now, in this movie too we see two characters having pretty much the same problem. We always perceive a relationship as constituting two people because that is how it has been through ages. But in this particular case, the three of them made it complete, though when the people increase the higher is the chance of any feud, it still was pretty cool and pretty calm while it lasted. Atleast that is what teh guy said at the end of that relationship. You would see, during the course of the movie, Maria Elena ( Penelope Cruz) telling Christina ( Scarlett Johannson), who is the current lover of Maria Elena’s ex-husband that she was the missing ingredient in their relationship. ??? You might wonder how she got that kind of thought. But that their relationship was not going on rocky waters when Christina was around is what she noticed instead of conspiring to get her ex-husband completely to herself. Amazing thought there and quite unique if you ask me.

I have seen that this kind of movies do not end with any particular conclusion. I noticed that the movie ended rather abruptly and I was swearing when it did, because it was such a nice presentation of thoughts and why they could not give a particular solution for the problem the characters were facing in the movie. But when I was lying on my bed thinking, I noticed that writer had left the movie as life leaves us. In the sense, there are no obvious or proper solutions for most of the human problems. For each solution, there are only choices and there are no absolute solutions.

So, finally there we are with yet another random musing from me, which doesnt lead to anything, which doesnt give you any solutions towards either life or technology. But if it has given you anything to think about, if it has lead you even one step ahead in your quest of understanding the intricacies of life, I would be glad. The movie did lead me further, so I thought of dedicating a blog entry for it. All the best.

Drafts

I see that writing technical stuff takes a bit of forethought and structure more than writing random stuff like this. Its hard to get a proper flow of thoughts in those segments. Another issue being that, when you have the words flowing in your mind, you never have your laptop in front of you to write it down and click “post”.

So a more unstructured version of those same things ( and I hope my readers don’t mind it ;) ) seems like a better option to me or atleast to my style of writing.

I have a few topics that I have started but have not been able to complete because of my inability to complete them in such a way that it is perfectly structured. So .. they still remain as just that.. “drafts”.

Hope I will be able to complete these things and post them here.

Topics Im planning to write about ( or rather complete ) :

Google Wave.

Flex and PhP Sessions.

Lets hope I get the inspiration to finish them off.

How can India make a difference in this world?

India is one of the most sought after destinations when it comes to software outsourcing. We are a very young country, in terms of its population. The Indian population is a population of youngsters , and as Nandan Nelikeni said, “There was an age when we were seeing this explosion in population as a bane, now this same thing has turned into a boon for us.” This being said, where are we in terms of progress? Where are we in terms of a name of our own? Is “Brand India” recognized around the world? These are some of the questions that we need to ask ourselves. Does simple patriotism , like flying the National Flag on the Independence Day and Republic Day suffice ? The map of the world is still agnostic about the existence of a country called India. I know a lot of us will come forward and flame with quotations of what we have done so far. But again, what we need to ask is, is that enough?

What was the last major contribution of India to the world? How many Nobel Physics laurets? Chemistry? Bio? For an ever growing population which is just putting its head above the poverty line , this might seem like a difficult ask. But no path , absolutely no path in this world is easy. So then how do we , collectively as a nation bring about this systemic change? We all celebrated the first Oscar ( a proper one! ) to an Indian when A R Rehman proudly recieved the two oscars for Slumdog Millionaire. But that is just the beginning. We have just entered the race, and the distance we need to cover in order to “truly be there” is huge and every one of us, who calls himself a true Indian, has to contribute , in order to win. We as a nation fought for our Independence and we won. Now we need to fight for a name in this world, and I’m sure we have the mettle to win this one too. So now, what do we have in order to succeed? What do we lack? What stops us from these goals? These are the first few questions that we need to ask ourselves and provide an answer, and in places where necessary, bring in a change.

It is my firm belief that , it is the people of the nation, who are the true assets of the country. Not just assets, they constitute the identity of the country. If you want to put a country on the international map, you need to put sufficient number of people, time and again on the international news. If you need to make it into the international news, you need to put something into peoples homes, into their lives, which becomes an itegral part of their life. When that “some – thing” becomes a part of peoples lives, along with it , its origins will reverberate in the minds of the people. Over the last century, there have been a few things that have made an impact of this magnitude, on the life of the masses. To quote a few, the radio, the telephone, the television , the computer, the internet,  mobile and  google are a few things that come to my mind. These are some of the products that have currently put America in the forefront , and has established it as a super power in this world. If we want to enjoy similiar benefits as the citizens of America, then we need to give something to this world. A product, an asset, which makes the world wonder how it survived so long without it!

What is stopping us:

We celebrated our 62nd year of Independence today. We have made progress, but is the progress enough? When people enjoy sudden richness, I have heard that , they become confused. They dont have an idea as to how to react to it, what to do with it. Is this the syndrome that is affecting most of the youth of India today? The father in an average middle class family was typically a government employee. The money that was earned thus, was sufficient to run a middle class family. For people who have seen this kind of a life, a sudden jump into high paying private sector jobs , might seem as sudden richness and people will tend to get confused. If we need to focus and bring about a change in the system, then we first need to bring about a change in ourselves. A government job, a middle class family to feed was the mantra of the 80′s and 90′s. But if we are planning to step into our fathers’ shoes, we need to think again.We all believe that the contribution of our parents in our life is in, them making us who we are today. But when we are seeing this, the most important aspect that we forget is that , they have provided us a strong platform to step onto and rule the world of tomorrow. Its a perspective shift and probably a lot many of us wont catch it at the first shot. My hope is that , this small snowball that I am throwing at you turns someday into an avalanche.

The Indian Mentality ( The pack rat ) :

Yes. This might sound derisive to some but if we look inside ourselves we will all see a pack rat within us. We are afflicted by a compulsive hoarding syndrome. This particular mentality or behaviour was acceptable earlier, since the time called for it. But what started as a necessity should not end up as an addiction. If you look back at your stash, you might find that what you have is enough to live a respectable life. In the fear of losing the standard of living of the future, we forget to live the present. In fear of losing what we already have, we try to dream less, we accept compromises. We have become a race that strictly , as a dogma , follows ” A bird in hand is better than two in the bush”.

Recognizing the Platform :

If the first step comes in shedding the “pack rat” mentality, then the second is in truly recognizing the platform that we are provided. Isaac Newton said “If I have seen further it is only by standing on the shoulders of giants“. Right now, this platform is one such giant. If we have to go further, it is only by trusting this platform that we can dive into the future. Only when we see this, can we shed our shells of inhibition and fear and sperad our wings.

Build the attitude :

To achieve something different from the rest of the world, we will need one weapon that stands out from the rest ( Ok, I’m not talking about a BFG). It undoubtedly is the attitude. In order to make this change, we need to push this change into our habits, into our attitude . If we look long enough and hard enough, eventually we will find it. But the catch phrase here is “looking long enough and hard enough” which is an adjustment in the attitude that we need to inculcate. It is my belief that it is not sufficient to perspire to succeed, we also need to persevere. Now , you might be wondering, what is it that I am supposed to be looking loong and hard upon? Its ofcourse at the Next Big Thing. What is the next big thing? I really dont know. But surely one of us does. It is towards this goal that we should paddle towards. We spend a lot of our brain power at petty things. Petty fights, petty name callings, blaming the politicians etc. But what we forget is that, a revolution, is possible by every single one of us. All we need to do is look.

Break the vicious circle :

Until you get into a habit of success, you wont take bigger risks and until you take risks the lesser is your chance to succeed. It is in this vicious circle that we all get caught up into. Im not advocating that every one of us quit their jobs and go start looking for this Holy Grail. Sometimes it is not necessary to quit your job , to do something different. As I said earlier, all that is necessary is the right attitude. We can all make this a fun activity and if we really get ourselves involved , it becomes a wonderful dream world that you can get lost in.

Application is more important than accumulation :

We , as a nation  have grown to this extent because we are good at building our careers. Im sure that each one of us is highly qualified, highly intelligent and a knowledgeable being. This is one of the reasons why we are sought after by huge multinational companies. We are called as knowledge workers. Apart from this, the beauty lies in the fact that , India being such a diverse country, we come from different backgrounds. Someone is the son of a potter, weaver, banker, trader etc. We, being part of a family which deals with a particular trade, bring in with us our own experiences from that domain. The industry that we work on today , may be poles apart from the trade our fathers did. But , as I have told time and again, true advancement happens only when you can successfully merge the ideas and knowledge present in one field into another. Since we come from such diverse backgrounds , it should not be difficult for each one of us to apply the knowledge and experience that we bring in from our respective parental trades into the current scenario. This is where innovation happens. This is where lies the crux of the matter. Application of that knowledge in this field will work wonders in the future.

Being an employer :

We become soo accustomed to the idea of being an employee, that we totally forget the concept of being employers. This goes on to such an extent that we even forget to employ our own creativity and innovativeness. Satisfaction takes over. “I have done so much, I have earned soo much ” kind of stuff. Satisfaction comes from the root word “satis” which means “enough” and this satisfaction is a pretty dicey thing. Its a problem if you have too much of it and its going to be a problem if you have too less of it. There is always a balance that we need to strike when it comes to satisfaction. You have to be satisfied and content with what you already have, but that is not where it should stop. Your eyes should always try to see whats next and look and try to find ways to get it. One step at a time.

In conclusion , Im expecting the next big thing from India and I believe that we are well poised to achieve it, if we, as a collective lot put the efforts. A certain few of us have already started the ball rolling and in the future Im sure we will see even more joining the race. So lets just put our thoughts towards this singular goal ” How can I make a difference in this world? “

Email using gmail smtp in PHP

Sending an email from your backend php script is ( as explained in most of the blogs) not a trivial task.

I followed the tutorial in this blog post and everything worked fine for me.

Important things to note are :

1) That the port has to be 465. ( There is another thread going on about 587 or so, which did not work for me) .

2) That the url has to be “ssl://smtp.gmail.com”  ( if your mail is hosted under gmail ).

I am a total noob when it comes to linux , network and stuff like that. So , I had a slight bit of doubt if there was something different when they were asking for the “SMTP username” and “SMTP password”. But no, its plain and simple. Its your email id, like in youremailid@gmail.com and its password.

So feel free to use the gmail smtp servers.

The Beginning

The greatest distance any entrepreneur covers is the distance between an idea to its implementation. That is where we all fail, isnt it?

So now the question is, How do we cover this distance? How do we figure out a way to see if an idea works by practically putting it to test?I have been one of the guys who has been on the look out for fresh and new “world-changing” ideas since the time I can remember. I am not kidding. I have been thinking of “new-ways-to-do-things” since I was in school. Through my own personal experience I have found that , I am pretty good at generating ideas but when it comes to implementing them my score drops down to almost zero.
I agree putting some idea into action is extremely hard. There are various barriers that need to be cleared before doing so and only when we clear all these barriers will we be able to see the idea in action. But there are a few barriers which are easily surmountable.
Here is a list.
1. Knowledge.
This was ( and sometimes still is!! ) one of the primary causitive agents which decides whether an idea goes to fruitition or not. Mostly around 99% of my ideas have gone down the drains because of this sole reason. I want to build a website but I dont know any languages to write them in. I dont know html. I want to write a novel but I dont know how to start.
2. Time.
I believe this to be one of the biggest culprits. As steve graham said in his essay the number one culprit is to procrastinate saying Im doing something really important right now, so I am not able to assign time to this. Your current life is your biggest enemy to a better life. Pay packet is pretty nice working for a big firm, so we tend to settle down there. Since those people pay you that much,  obviously, they will get your moneys worth out of you.
3. Success-o-phobia.
Yeah!! I know!! You wont believe it. But I have found this. A lot of us probably have this. What if I succeed is sometimes more scary than what if I fail! Dive deep into yourself and maybe you will find the answers.
We are, as a species, attuned to a shock-free, stress-free life. Anything otherwise, anything out of normal and we try to run away from it. Even electricity tries to choose the least resistant path. Then how can we spare humans? Impending success is also a greatly stressful situation ( but better than impending failure ) .
4.Motivation.
You always need someone pushing you to do stuff. Your manager pushes you to get your project out before the deadline, his manager pushes him. But in this segment there is no push from anyone outside. Whatever is necessary to keep you on the path should come from within. A better alternative is to find some like minded individual and hop on in the ride. Its something like car-pooling. But when you find someone all our efforts need to be doubled, rather than halved. You need to push a vehicle that has two passengers now instead of one. Its a risky alternative with equal amounts of advantages and disadvantages.
5. Money.
Money is something that I dont need to talk about at all. Everyone knows it. Everyone in the world is not working on an idea because they dont want to lose money.  As simple as that. But there are a few ideas that need more money than you can afford. How do we deal with those issues? How do we deal with those ideas?
I know I have been throwing up a lot of queries. But in this journey I wish to answer them. I started this quest, this challenge to get over them and share the learnings with others. I know a lot of the successful people have done that. But where they stand and speak is something different from where you and I stand. So this is a different perspective to the whole idea called success. We have heard people talking about success from the point where they have already reached it. Lets talk about success from the point where it is a dream.
That is why this page is called the Next Big Thing, because that is exactly what every wannabe-entrepreneur is after.