Friday 19 October 2012

Tinkering With TypeScript



In the spirit of David Wagner's try { harder } talk on 'the value of tinkering' I decided to do a little tinkering with a new language from Microsoft called TypeScript.

TypeScript attracted me for three reasons. Its the new project from Anders Hejlsberg the creator of C#, it brings type safety to JavaScript and im a sucker for new technology.

TypeScript is basically a superset of Javascript much in the same way C++ is to C. So every single JS project is a valid TS project. This is good if you are familiar with JS already, you should be able to pick up TS fast and then get to grips with the other cool bits it bring.

TS compiles down to JS much in the same way that that JS target of Haxe compiles down to JS. Unlike Haxe however the generated code is much more readable and so although there is no integrated debugger (yet) you can just use Chrome's developer console to debug with without too much pain.

Before I get too much more into the specifics of the language I want to mention the project I am tinkering with TS for. A while back I wrote an extension for Chrome called Chrome Crawler. It is a rather simplistic web crawler written in JS and released as a Chrome extension (because of the cross-domain scripting limitations with normal JS).

Over the intervening couple of years I have returned to the project on occasion with an idea to do a second version, however I never actually completed one. So I thought it may be nice if I gave it a go again but this time using TypeScript and at the same time see how it compares to the Haxe JS target.

Chrome Crawler v2 isnt ready for release but checkout the quick video I put together below to see how its coming along:



The original idea was to lay out the crawling in a left to right fashion. So each crawl depth is put in its own column, but as you can probably tell from the video above, things start getting a little unmanageable when you get to just level 3 of crawler depth. So I think im going to have to rethink how I display the nodes in the crawl graph. I have some ideas, but that's going to have to wait until I return from my trip to NY next week.

More on that in later posts, lets take a look at a few key TypeScript features:

Typing



One of the key features of TypeScript is that it allows you to structure your code in a type-safe way. You can declare Classes, interfaces (inequivalent to typedef in Haxe) and modules as types then use them later.

TS also has structural typing so you can define a structure as part of a function or variable definition without first declaring a Class or Interface for it. For example:



This is great for many reasons but the big one for me is the way it assists tooling particularly when coupled with the excellent VisualStudio 2012 you get all the things you would expect such as intellisense, go to definition, find references and refactoring. For me it takes much of the pain out of the dynamic nature of JS.

As with AS3 and Haxe typing is optional. You can if you wish declare everything as "any" (dynamic in Haxe or * in AS3). Doing so however would forfeit many of the strengths of TS.

Like Haxe you can 'define' things as being available at runtime, this means you can reuse existing JS libraries without having to totally rewrite them in TS. By declaring an "interface" you can just tell the compiler that this type will exist at runtime (we did this with extern's in Haxe) and thus you can use the existing library in a type-safe way. For example here is a definition for the Facebook SDK:

https://github.com/mientjan/typescript-facebook-definition/blob/master/facebook.d.ts

Alot of the type-safe TS stuff is familiar to me because of Haxe. I think some things in TS are nicer fit because its only designed to compile to JS unlike Haxe which can compile to many different languages. For example TS has function overriding baked into the language also strongly typed functions in my option are a little nicer:

[codesyntax lang="text" lines="normal"]

(index: any, domElement: Element) => any

[/codesyntax]

is a function type that takes in two params and returns anything (including void), its Haxe inequivalent:

[codesyntax lang="text" lines="normal"]

Dynamic -> Element -> Dynamic

[/codesyntax]

These things however are just my opinion, but it does tie nicely into one of my favourite features of TypeScript...

Lambda Functions



Lambda functions are basically syntactical sugar to anonymous function definitions so instead of:

[codesyntax lang="javascript" lines="normal"]

var fn = function(a, b) { return a+b; }

[/codesyntax]

You could write this as:

[codesyntax lang="javascript" lines="normal"]

var fn = (a, b) => a+b;

[/codesyntax]

Which is really nice when looping over arrays:

[codesyntax lang="javascript" lines="normal"]

var a = [4,2,1,6,5];
a.sort((a,b)=>a-b).forEach(i=>console.log(i));

[/codesyntax]

Because TS uses type-inference all the variables in the above are type safe. I really love how terse this syntax is and have had quite a few discussions about introducing it into Haxe.

There is one big difference between function() and lambda definitions however and that is the way they handle scoping of "this".

For example take this example from the javascriptplayground.com blog post:

[codesyntax lang="javascript" lines="normal"]

$("myLink").on("click", function() {
console.log(this); //points to myLink (as expected)
$.ajax({
//ajax set up
success: function() {
console.log(this); //points to the global object. Huh?
}
});
});

[/codesyntax]

This is a common gotcha in JS coding and the usual solution is to do something like the following:

[codesyntax lang="javascript" lines="normal"]

$("myLink").on("click", function() {
console.log(this); //points to myLink (as expected)
var _this = this; //store reference
$.ajax({
//ajax set up
success: function() {
console.log(this); //points to the global object. Huh?
console.log(_this); //better!
}
});
});

[/codesyntax]

In TypeScript lambda functions "this" is scoped to the block in which the lambda was defined so the above can be written more simply as:

[codesyntax lang="javascript" lines="normal"]

$("myLink").on("click", function() {
console.log(this); // points to myLink (as expected)
$.ajax({
success: () => console.log(this); // points to myLink
});
});

[/codesyntax]

this.



One minor thing that does annoy be about TS is the necessity to put "this." in front of every single member variable access. For example:

[codesyntax lang="javascript" lines="normal"]

class Crawler {
constructor(private url:string){}

crawl(){
console.log(url); // error
console.log(this.url) // okay
}
}

[/codesyntax]

Some may consider it rather minor but I find it annoying coming from a Haxe / AS3 / C# perspective where member variable access is implied. I guess the reasoning is because in JS if you dont supply a var before a variable declaration / usage you are referring to global scope, thus in that above example "console.log(url);" would be trying to log global.url which is undefined.

Tooling



The tooling support for TS is pretty good so far. Visual Studio has a plugin for the language and has reasonably good support. There is still quite a bit of room for improvement here however. Things such as integrated debugging, code generation for functions event handlers etc and source formatting would be nice.

Because the TS compiler is written in TS they are able to create really cool things such as the TypeScript Playground:



With it you can mess around with TS and see its generated JS as you type. It even includes a version of intellisese for that type-hinting goodness.

Conclusion



I must admit I really like TypeScript. It provides a light-weight layer over the top of Javascript giving just enough of the type-safe goodness and language enhancements without making it totally alien to a JS coder.

The generated JS looks very similar to the source TS (and will look even more so once ES6 is mainstream) which is important when you are trying to debug a problem. I discovered this to be an important point when developing the Haxe version of my Post To Tumblr chrome extension. Haxe although awesome tends to generate alot of alien looking JS making it tricky to work out where to put a breakpoint.

More important than any particular nuance of the language however is who is backing it. Microsoft is a huge company with a massive following of developers. It pays many people to work on the language and evangelise it. What this results in is a much bigger community. A bigger community means you have more questions asked on Stack Overflow, more API definitions written and more job positions specialising in the language. Also having Anders Hejlsberg, the father of C#, behind it you can be confident that the language will continue to develop in a manner that (if you are a C# fan) makes sense.

I have been having a whole lot of fun in TypeScript, and have high hopes for its future.

Try Harder 2012 Slides



Because Monocle is now available to everyone via the FB 4.7 Beta2 I guess its okay for me to now post these slides.

Wednesday 17 October 2012

Try Harder 2012



The above is what it looks like from a speakers position at the try { harder } conference. (click to embiggen).

I had indented to write this post a couple of weeks ago, immediately after the conference but STRATO denied that. But oh well, here it is anyways.

This year was the second annual try { harder } conference / coder retreat / brain explosion. I have spoken about try harder in the past but it really is a uniquely awesome event. Its a rather intimate affair  where 10-15 people getting together and sharing ideas and knowledge in the tranquil forests of Center Parks Nottingham.

The format was largely the same as last year. Everyone gave a talk on a topic of their choosing, some were technical talks such as David Arno's on C# (NCrunch, MVC & Mono) or Mattes Groeger's on developing iOS social games. Quite a few talks were on non-technical things such as David Wagner's talk on 'the value of tinkering' or Stray's talk on 'Heuristics, collaboration special sauce'. Each talk was fascinating and the wide range of topics covered adds to the special feel of this event.

When deciding what to give my talk on I couldn't make my mind up. I have been through a lot this year from launching my first business then having it fail to exploring a great many new tools and technologies to creating and launching my first mobile game. Not to mention all the cool things I have had the pleasure to work on at Playdemic. In the end it was a tweet from David Wagner who suggested that I talk about an exciting tool I had been experimenting around with lately; Adobe Monocle.

Adobe Monocle is the new profiler for flash and mobile content from Adobe and really is very nice. It currently still in pre-release and hence under NDA so I had to ask for permission before giving the talk on it. I haven't had permission to blog about it get so if you want to find out more about it then check out this video that Thibault Imbert made a few months back:



I personally think it is a game changer for flash game makers. It makes development so much easier by giving better insights as to what is going on inside your game.

Try Harder is more about simply giving talks however. Each afternoon was set aside for pair programming sessions. Like last year we were given a deceptively simply problem then split off in pairs to try to solve that problem in a test-driven way. The kicker was that at the end of your hour with your parter you then must delete all your work and start again with the next person.

The problem we were given this year was "to find and score the palindromes with a length greater than 3 chars in a given string". Seems simple, but when you get into it, it is deceptively complex. Because of the varied programming backgrounds of the attendees there were many different tools and technologies used.

Of the 6 different partners I had I solved the problem using: Javascript, Actionscript3, Haxe, C#, Java and Ruby using TextMate, Flash Builder, FlashDevelop, Visual Studio and IntelliJ. It was fascinating to see the multitude of ways to solving the same problem

Try Harder is held in the heart of the forests of Center Parks Nottingham. This adds to a sense of "getting away from it all" and I found it to be a really helped with concentrating on what we were doing. In many of the evenings I enjoyed walking around the forests or swimming in the pool or sampling David Wagner's rather excellent scotch whisky that he brought with his all the way from Scotland (cheers Dave!)

   

So if you cant tell I had a great time at try { harder } this year. I think because I knew what to expect this time I could relax and enjoy the whole thing a little more. I also approached the event with a much more open mind which meant I could relax more and enjoy the format.

Bring on try { harder } 2013!

Tuesday 16 October 2012

STRATO and the Customer-Support-Circle-of-Doom



Why is it so hard to find a good web hosting company?

About one year ago I announced that I moved hosting from WebFusion over to STRATO in the hopes for higher reliability. This was indeed the case for almost a year. I had no problems, until...

Three weeks ago my blog started to suffer problems, as is clear from my analytics data on one of the domains:



The traffic drops off from normal levels down to nothing in the space of about a week. I was away the start of the month (more on this in a later blog post) and thus wasn't checking the domain. Its only when I received an email from someone informing me about the fact my blog was down that I sat up and took notice.

I immediately tried to access the domains in Chrome but I got timeout for each one. I then tried to remote into my VM. Timeout there too. I then tried hard-resetting the machine from the Strato console, no luck there.

With no other options I decided to call my hosting provider STRATO. It turns out however that their call centres aren't open at the weekend *sigh* so I sent them a mail:
Hi

A visitor reported that my site was down: http://www.mikecann.co.uk/. I cant access it either. I have tried to remote into my VM but it wont connect. I have logged into the Strato backend and tried to reboot my machine. It says it has rebooted, but I still cant login or access my site.

Can you help please.

Mike

Two days later I get the following reply:
Dear Mr. Cann,

Thank you very much for your enquiry to which I am glad to respond.

I can see that the server is up and running , If you are using Plesk please check the domain setting in plesk. If you cannot reach plesk please  work through the following FAQ Article ID: 1360.

Your opinion about our Customer Service is very important to us! Please take some time for our questionnaire - so we can respond even better to your needs in the future. The completion of the form takes only about 3 minutes.

Link to the rating system: https://www.strato-mailcenter.com/quality?ident=bd391e7c-3c076f-13f18d-47938a-aaafba

With kind regards from Berlin,

Craig Laishley
STRATO Customer Care

Well clearly they hadn't actually read my mail and checked that the domain isn't up (this is the main domain associated with my account) they also haven't tried to login to the box else they would have encountered the same issue.

So after some email jousting back and forth and some experimentation with STRATO's non-functioning recovery console I get the following reply:



Dear Mr. Cann,

Thank you very much for your enquiry to which I am glad to respond.


I have taken your server out of the recovery mode for you .You server could also be slow as it is affected my some major maintenance
that is also going on at the moment on our windows servers, which could be causing your server to run slow at the moment  , I cannot give you a time frame when this will be completed as i do not have one myself.
Your opinion about our Customer Service is very important to us! Please take some time for our questionnaire - so we can respond even better to your needs in the future. The completion of the form takes only about 3 minutes.

Link to the rating system: https://www.strato-mailcenter.com/quality?ident=bd391e7c-39a316-13f846-44189a-8050db
With kind regards from Berlin,Craig Laishley
STRATO Customer Care

(Bold added by me)

Okay... I thought to myself... I have just been led round the houses for two days emailing back and forth when only just now they tell me they are having some major maintenance issues, why not tell me that from the start? In fact, more to the point,  why are you performing maintenance, without telling me, that has taken my box down for a week?

I decide at this point, its a good idea to give them a call and find out what is going on. Eventually I get put through to Lee. Lee at first tells me that he cannot give me the reason for the maintenance nor how long it will last. I get pretty insistent at this point that I should be told why the service that im paying for is not working. Eventually he relents and tells me it is to do with their licensing for windows.

Lee explains to me that when Microsoft license the Windows OS to hosting companies they create a child license from their master license. Strato as one of these hosting companies then creates further child licenses for its customers. Apparently something has gone wrong with the master license at Microsoft which has caused every single other license that has been created from it to become invalid.

Lee explained to me that every single Windows VM that STRATO hosts is down. Not only that, every single Windows VM in the world is down because of this master license issue. I was understandably dubious at this as I would have heard something in the tech-news or blogosphere. I decided however to give him the benefit of the doubt as he sounded honest and it took some persuading to get this information out of him that it must be true.

Before hanging up I pointed out to Lee that STRATO should have sent an email to its customers to inform them of the problem. At the very least there should have been a press release or something on their homepage to inform people of the outage. Lee agreed whole heartedly with this and suggested that I send in a complaint detailing all of what had happened thus far, so I do just that.

I get the following reply a day later:
Dear Mr. Cann,

Thank you very much for your enquiry to which I am glad to respond.

We're aware of the issue where users are unable to connect to their STRATO Windows Servers. Our technical teams has been notified and we expect this resolved by the end of the week. We have raise a credit amount of £10, and it will be paid to your credit card in the next few days.

I will contact you around noon to answer any questions you may have regarding this issue

We appreciate your patience, and apologize for the inconvenience caused.

Your opinion about our Customer Service is very important to us! Please take some time for our questionnaire - so we can respond even better to your needs in the future. The completion of the form takes only about 3 minutes.

Link to the rating system: https://www.strato-mailcenter.com/quality?ident=bd391e7c-a2fc8d-13fff2-44cee9-a53fb3

With kind regards from Berlin,

Seth Prince Dodoo
STRATO Customer Care

Okay so it will be ready by the end of the week. I decided to hang on and see what happened.

What happened? Exactly nothing, I was obviously a little angry at this point:
Hello,
So as its now the end of the week. You promised that my VM will be back up again but low and behold its still not up! Also have you decided not to reply to me any more?


I need a snapshot of my data so I can move my hosting from strato. I also would like the email address of your supervisor so I can speak to them about this.


Mike

Two days later I get the following:
Good Morning Mr. Cann,
Thank you very much for your enquiry to which I am glad to respond.

After looking at your contract, I have seen that your sever has been up and running since Saturday 13.10.2012.

If you would still like me to cancel the Server, this is not a problem. I would therefore need to you to confirm this for me in a reply to this e-mail.

If you do wish to keep your server, you will be able to start ordering your domains within the customer service area.

Once again, I would like to apologise in the setup delay that you experienced.

If there is anything else that I can do for you Mr Cann, please feel free to let me know!

I would therefore like to take this opportunity to wish you a pleasant and successful working day!
Your opinion about our Customer Service is very important to us! Please take some time for our questionnaire - so we can respond even better to your needs in the future. The completion of the form takes only about 3 minutes.

Link to the rating system: https://www.strato-mailcenter.com/quality?ident=bd391e7c-c8411a-140d93-40f3ee-9f18a5
With kind regards from Berlin,

Lee Anthony Welsh

WHAT?!??! Setup delay? Im not setting anything up! I have been with STRATO over a year! Its been up since Saturday, no it bloody hasnt! Its clearly not up now! WTF?

I realised that I was trapped in a customer-support-circle-of-doom.

Well that's about the point I decided this company really doesn't care about its customers so im going to move to another hosting provider.

So short of the long, my sites are now being hosted on 1&1 and everything seems to be fine and dandy. Sure it costs twice as much but hopefully that money will ensure a better level of service.

Oh one more thing, apparently STRATO have won awards for their customer service:



*sigh*

</rant>