Tuesday, January 29, 2013

Oh yeah, Coffeescript

Oh yeah,

One thing I forgot to mention picking up over the last year was Coffeescript.

When I first heard about Coffeescript I was interested.  After checking it out initally I came away rather unimpressed.  It added a more traditional looking class structure for OOP, had some structural changes to loops and conditions but in the end since it compiled down to Javascript and you would be debugging that code, I didn't see the point.

Some time later I decided to give it a shot on a new project and once I started using Coffeescript, I was sold.  I now use Coffeescript on pretty much all projects.  Why?  Once I began to do some real development in Coffeescript I noticed that I was able to work much faster in it than Javascript alone.  The class structure I dismissed early on does come with some features that make it easier in many cases to develop faster.  Simple inheritance with extends and the fat arrow for context binding allows you to code faster without having to dedicate as much time to boilerplate code.

Classes

Javascript doesn't have classes.  It uses a prototypical system for inheritance and everythis is declared using functions.


function A()


}

var a = new A(); 
console.log(a instanceof A); // true

The above code says that instance a has the constructor of A in it's prototype chain and will contain it's properties and methods.

Too add properties and methods we

A.prototype.info = "On the prototype";

A.prototype.hotMethod = function(){ 
 return "this method is hot"; 
};

console.log(a.info); // On the prototype
console.log(a.hotMethod()); // this method is hot

When we get to inheritance that's where it get's hairy for many people, myself included

function B(){

}

B.prototype = new A(); 

B.prototype.newHotMethod = function(){ 
 return "this method is the new hotness" 
};

var b = new B(); 
console.log(b instanceof B); // true
console.log(b.hotMethod()); // this method is hot
console.log(b.newHotMethod()); // this method is the new hotness

There are better more thorough ways of doing inheritance in javascript but that will work.

Here is the same basic class setup in Coffeescript

class A 

 constructor: () -> 
 info: "this is on A" 
 hotMethod: () -> "this method is hot" 

a = new A();
console.log(a instanceof A) // true 
console.log(a.info) // this is on A 
console.log(a.hotMethod()) // this method is hot

And basic inheritance

class B extends A 
 constructor: () -> 
 newHotMethod: () -> "this method is the new hotness" 
 b = new B();

console.log(b instanceof B) // true
console.log(b.info) // this is on A 
console.log(b.hotMethod()) //this method is hot 
console.log(b.newHotMethod()) //this method is the new hotness

As you can see the Coffeescript is slightly less verbose and more easily readable, which can make it both faster to code and easier to maintain.

The Fat Arrow

Not simply a gluttonous arrow, fat arrow notation has actually worked it's way into a future version of Javascript.  Using this keyword is javascript can lead to some gotchas, such as the following:

function App(){ 
 this.something = "something!"

 var theClick = function(evt){ 
   console.log(this); // the button that emited the event
   console.log(this.something); //undefined 
 }; 

 this.notTheClick = function(){ 
   console.log(this.something); // something! 
 }; 

 $("#clickit").on("click", theClick); 
 this.notTheClick(); //something


run = new App();

That is a totally contrived, non-sense example of scope issues in javascript (featuring jQuery) but it does prove the point. The context for the event handler is different from the enclosing App function but on visual inspection that is difficult to tell. In Coffeescript we have the fat and skinny arrows for context resolution

class App constructor: () -> 
   $("#clickit").on("click", @fatArrowHotness) 
 fatArrowHotness: () => ##binds it to the instance of App
   console.log(@) // context is App instance 

 runApp = new App()

One thing you'll notice here is my use of the @ symbol which is an alias for this in Coffeescript. As promised, using the fat arrow has bound the context of fatArrowHotness to the instance of App, if you use a skinny arrow the context would be the DOM element that emitted the click event.

class App 
 constructor: () -> 
   $("#clickit").on("click", @thinArrowHotness) 
 thinArrowHotness: () -> 
   console.log(@) // the DOM element that was clicked 

 runApp = new App()

The fat arrow makes scope resolution far easier to see at a glance which again can lead to faster development and easier maintanence.

These are just a couple of reasons I have been using Coffeescript so heavily over the last little bit, I hope to share more of why I enjoy Coffeescript in the next little bit.

Monday, January 28, 2013

Back to Bring the Info

Hey all


I'm back to continue my blogging adventures after quite awhile off.  I haven't had time to keep up with the blog as work/life was keeping my quite busy and although I'm still quite as buy, I've decided it's time to get back to doing some blogging.

First off I want to review some of the topics I was discussing in earlier posts.  I didn't get my last effort with go and backbone.js off the ground.  I've been keeping up with both projects, especially backbone as I was doing some extensive front end work over the last year or so.  I've fallen off using go as a primary development language but that doesn't mean that I didn't like it, I actually think it is quite good for rapid development.  I was working primarily in .Net with MVC in a windows environment so it didn't give much time for developing with go.

More on the .Net stuff in a bit but first I want to review what has been going on with go and talk a bit about where I would like to go with it (I don't think I intended a pun but there it is anyway).  Since my last writing go has gone version 1.  Even though I haven't been using a lot of go I have been trying to keep up with it and the go ecosystem seems quite healthy.  Sometime in the next few weeks I'd like to tackle a new go project on app engine, using the web.goRevel framework or possibly just use the Gorilla tools with some home baked http stuff.  Web.go used to be the only way to go but I'm not sure how the project support is going these days.  Revel is pretty new and interesting.  I'm also hearing lots of good things about using Gorilla with the go library http package so we'll see, maybe end up doing something for all three.

The backbone.js community has really taken off since my last posts about it.  There are many great tutorials out though as well a large list of plugins and just a great on-line presence in general.  I'm not going to go over any specifics here as I will be doing some backbone stuff for sure here in the next few weeks.

Now a little on some projects I have been working on.  Over the holidays (actually sometime before) I started developing a project in node.js.  I had left one job and was waiting for another to start so I had some time on my hands and wanted to see what all the fuss was about.  The project used node.js with a mongodb database and had facebook and twitter integration.  The app was a simple gift list which basically allowed you to add  wish list items your facebook and twitter friends could see.  There is more to it but it's incomplete (of course) and I'm still working on getting it out for sometime this year.  I did enjoy developing in node.js.  I have been doing front end development with javascript for years so I was quite familiar with it going into this.  One major problem I had earlier on with the development of the project was "call back hell", I'm hoping to share some techniques to overcome this soon.  Working with the noSQL mongodb was interesting and I enjoyed that as well.  The noSQL db's are something I also hope to share some thoughts on soon.

As I mentioned earlier I have been primarily developing in .Net MVC for the last year and a half or so.  One project I have been working on and is in "early alpha" state is messages and reminders.  Feel free to follow the link and sign up to see what it is and what it does.  Again it's a very early version and has many features that will be added in the next release (early next month, fingers crossed) but I have been using the current version for simple reminders and messaging between a few friends.  It also uses twitter bootstrap as it's base CSS. As primarily a developer I'm a huge fan of bootstrap.  I'm hoping to go over bootstrap soon as well as customizing it with LESS.  The long term goal is to have the web app as well as a native iOS, Android, Win8 and maybe BB10 apps as well.  I`m looking at using the base HTML5 and compiling to the other platforms using a tool such as appMobi or Phonegap, so look forward to that.

Lastly, as stated earlier, I left one job for another recently and the direction of development I`ll be doing has changed extensively as I`ve gone from primarily app development in HTML and Flash to a more Business Intelligence path so I`ll be sharing my thoughts on that as well.

Anywho, that was quite a rant, so TLDR; I`m back and hoping there are still some people out there reading.

Jason