I’ve seen a day I never thought would come: the day I turned into a hipster!
If you’ve been following this blog, you’ll have noticed that throughout 2011 I’ve been workingmoreandmorein Javascript, more specifically withNode.JS.
The last few months have been especially Node.JS-centric. During the day, I’ve been building architectural improvements intoJabbakam. Evenings and weekends, I’ve been hacking about on theLive Unsignedmobile site. All of my recent projects revolve around Node.JS, and I’ve been loving it. Node is incredibly interesting and powerful, and it is giving my brain a workout.
However, Javascript itself has a few niggly bits that become more problematic the larger the project gets. Issues with structure and maintainability of code motivate projects such asJooseand Backbone.js. One of the things that has been bugging me is having to hack in class-based inheritance. After a lot of experimentation, I settled onPrototypal Inheritance, which does the job to an extent, but left me dissatisfied .
So, the other day I finally broke down and triedCoffeeScript.
When CoffeeScript first came out I resolved never to bother with it. It is clearly forRuby peoplewho don’t quite understand Javascript. Since then it’s been getting alotofpress, and it now even hasits own book!
Anyway, I was up the other night, waiting for my daughter to wake up in screaming pain as razor-sharp pieces of enamel slowly bored through her gums. With nothing better to do, I decided to play around with CoffeeScript.
I was up and running in no time, installing theNode.JS Moduleand browsing theLittle Book of CoffeeScript. After about an hour I had effortlessly written some additions to Live Unsigned which integrated seamlessly. To be honest, I feel like a bit of a muppet for not trying it sooner.
After just a couple of days playing around with it, CoffeeScript has now become a major part of my toolkit. The syntax is elegant, nicely lending itself to clean coding, and code can be reused easily, improving project structure. CoffeeScript compiles to pretty clean Javascript, and from what I can make out, the better you understand Javascript, the better CoffeeScript you can write.
So yes, I am now coding in CoffeeScript.
I am now a hipster.
There is nothing left for me but to rush out, buy some skinny jeans and a Macbook Air and learn Vim.
Note: This is the last in a series of posts describing my experiments with constructing a Middle End. In Part 1, we discussed the concept of the middle end and its advantages. In Part 2, we constructed a very simple example.
In this post, we’re going to make that example slightly more complex by introducing dependencies within the modules. Now the code must be able to handle these dependencies for both the client side and the server side.
In the last post, we built a simple module consisting of a single function that returned a greeting:
var Greetings =function(){}
Greetings.prototype.hello=function(who){return"Hello "+who;
}
module.exports= Greetings;
At this point, it would be very useful to sanitize and validate the parameter passed to the “hello” function. We could obviously build this functionality directly into the code, but this is exactly the sort of thing that should be abstracted into its own module(s).
For the purpose of this example, we’ll simply include the functionality to give the parameter passed a “trim”; after some hacking and snippet-borrowing from node-validator by Chris O’Hara, we can build another simple module called Tidy:
We can now improve our Greetings module, including the dependency and functionality:
var Tidy = require('./tidy');
var Greetings =function(){}
Greetings.prototype.hello=function(who){
who = Tidy.trim(who);
returnthis.hello(who);
}
module.exports= Greetings;
To see if this will work as expected, let’s add some spaces to the parameter passed in the NodeJS script:
var Greetings = require('./greetings');
var greetings =new Greetings();
console.log(greetings.hello(' Node ')+"!");
When we run it, this is what we see:
$ node runner.js
Hello Node!
Good, so this works on the server side, but let’s test its behaviour on the client side. Again, we should add some spaces to the parameter passed to ensure that it is working:
require is not defined
[Break On This Error] var greetings = new Greetings();
Our attempt to “require” the dependency in the Greetings module has broken the client side. To fix this, we need to define our own require() function, much like we had to for the module object. We do this by creating a new front end-only script, which we’ll call “middle-end.js”.
Now when we reload the page in the browser, all should work as expected. Huzzah!
This is just a very simple example of what can be done when JavaScript code is shared between the client and server sides—the “Middle End”. This concept becomes really exciting when you start thinking about potential approaches to form validation, session management, and even display logic. This is definitely something worth playing around with.
Note: This is the second in a series of posts describing my experiments with constructing a Middle End. In Part 1, we discussed the concept of the middle end and its advantages.
In my previous post, I talked about Kyle Simpson’s concept of a Middle End—a layer between the front end and back end. Today, I’m going to sketch out my very simple experiments with constructing such a layer.
What I am focusing on here is simply a means of sharing JavaScript code between the client side and the server side.
Let’s start with a very simple NodeJS Module, greetings.js:
var Greetings =function(){}
Greetings.prototype.hello=function(who){return"Hello "+who;
}
module.exports= Greetings;
Next, let’s create a very simple NodeJS script that imports this module, and uses it:
var Greetings = require('./greetings');
var greetings =new Greetings();
console.log(greetings.hello('Node')+"!");
When we run it, this is what we get:
$ node test.js
Hello Node!
All very simple stuff.
Now, suppose we want to use the Greetings module in our front end web app.
Gosh! I just found this in my list of draft posts (originally written in early 2010). I thought I might as well post it as it could be useful to someone out there?
Again, this is one of those posts more for me, but hopefully other people might find it useful.
Recently I updated my Arch Linux box
# pacman -Syu
and during this update a new kernel was added – which meant that my VirtualBox installation didn’t work any more, chucking out the following message
The VirtualBox Linux kernel driver (vboxdrv) is either not loaded or there is a permission problem with /dev/vboxdrv. Re-setup the kernel module by executing
Back in October last year (2010) a bunch of us fled Berlin and made for Warsaw. Not just for the Vodka you understand, but more specifically for the Front-Trends conference.
One of the talks was discussing the concept of the Middle End, by Kyle Simpson. This really made an impression on me, and I think I probably bored Kyle in the bar afterwards with my drunken enthusing. When we arrived back home, my aim was to start playing around with the idea of a Middle End as soon as possible. Unfortunately, as is life, events took over (such as discovering one is going to become a dad!) and these thoughts were consigned to the back of my mind.
Back home now after a busy day at JSConf EU, brain is pretty melted due to a lot of good talks, so will make this a quick one.
Three things really stood out for me today.
Firstly, Cloud9 IDE. This is a browser based JavaScript IDE, and it looks fantastic. With no setup or configuration needed it lets you just start writing NodeJS directly from your browser. The lovely guys even gave me a premium account free for 1 year! I am going to sign up and start hacking away as soon as I get a chance.
Secondly, even though the Arduino talk was a bit, er, ramshackled, they did talk about the Raspberry Pi. This is a tiny (and I mean tiny) ARM box running Linux. It should be available in November some time, which will be perfect timing for a birthday present to myself!
Finally, Emscripten is very exciting. Emscripten is a compiler that takes bitcode and turns it into JavaScript. From what I understood, this is what made repl.it possible, which I saw yesterday in Hacker News and thought was so cool I bookmarked it!
As there were two tracks I did miss a bunch of talks, but I’m sure they will be availble on the website soon.
All in all, good stuff, tiring (especially as some little madam decided to wake up at 5am) but brain is now firing off with a bunch of possibilities!
This is just a quick post as I’m off to bed for an early night’s sleep. JSConf EU is tomorrow and am one of the lucky few who have in their posession a “golden ticket” to attend. It’s a jam-packed event starting at an unaturally early 8am! Do they know this is Berlin?! You can’t even buy breakfast at 8am on a Saturday here!
I have high hopes for the conference. Last year I didn’t even attend the main event, just the pre-party on the Friday night and still came away with a load of ideas! One conversation lead me to completely rewrite major parts of the Jabbakam system. Who knows what new thoughts will spark in my head when I actually go to the real thing?
I’ve been hacking about with NodeJS a lot recently, and lately my attention has turned towards spawning child processes. Luckily NodeJS makes this very easy to do, and also the documentation is pretty good.
What I have been working on is having a single parent process that runs constantly in the background, and this parent is responsible for child processes which actually do the work. One thing I found particularly interesting was how the parent can handle a child crashing, and also what happens to the children when the parent is killed. Below is the culmination of my hackings, it comprises of two simple scripts, a child script, and a parent. I have committed them to my GitHub account, so feel free to clone, fork, hack or whatever.
This is a minor whinge, and I apologise for that. I don’t post for weeks, and when I do: whinge; moan; complain (not that I’d particularly happy about conforming with the whinging pom sterotype).
Anyway. this is about PHP.
Frankly PHP, you’ve served me well. I remember being pried away from Perl when you were only 3. With 4 you had me, and 5 I thought it couldn’t get better. But after 13 years of you being my primary language, I’m sorry to say that I’m going to have to move on.
It’s not you, it’s me.
Yes I know you have some spangly new namespaces and finally ridding yourself of your childish toys, but that’s not enough for me anymore. I’ve been dabbling with other languages. Node, Python, Lisp, Haskell and indeed Lua* and, well, frankly they are more fun. Sure, I’ll visit, especially as Jabbakam, Live Unsigned and Doolali are all mainly written in PHP, but I’m sorry to say, and how do I put this delicately… you’re just not exciting any more.
So, whilst this is not goodbye, it is an auf wiedersehen.