Wow, I actually couldn’t write JavaScript, what I wrote smelled…

Alright, I’m not really a JavaScript developer, but do they even exists? I think there probably are a few JavaScript developers in this world, but most of the developers that I know of are developing for the Web. Developing for the Web in it’s widest sense. Most of the time there’s a server technology like ASP.NET, ASP.NET MVC, PHP or something else. A lot of times people actually feel like they are a WHATEVER-SERVER-TECHNOLOGY developer. I felt the same.

But lately the client-side technologies become more and more important. Yes most web developers know a little bit of HTML, and if we’re lucky there’s some knowledge on CSS as well. Yeah we also write JavaScript. uh actually we write jQuery script. Yes we are lucky to have a library like jQuery, but in the end most of don’t know much about JavaScript at all. Yeah we write functions, sometimes. But that’s about it, we don’t write, maintainable, easy to read JavaScript code. Our JavaScript smells.

And if we want to make use of the full HTML5 stack either for web or for example in a Windows 8 Metro application, our code needs to be maintainable. I can assure you, it’s not going to be 10 lines of JavaScript for a JavaScript Metro application.

To == or to ===?

I know C# so I’m using the double equals to check if left and right are equal. WRONG! I really need to admit I didn’t know I was wrong until a few months ago. So chances are some of you, my readers, don’t know it either. To quote Douglas Crockford’s book on JavaScript:

JavaScript has two sets of equality operators: === and !==, and their evil twins == and !=. The good ones work the way you would expect. If the two operands are of the same type and have the same value, then === produces true and !== produces false. The evil twins do the right thing when the operands are of the same type, but if they are of different types, they attempt to coerce the values. the rules by which they do that are complicated and unmemorable.

So from now on I will be using triple-equals instead of double-equals.

Of course there’s a lot more in The Good Parts book, I can recommend you buying it.

Object Oriented Programming uh Closures

Most of the JavaScript code I write are just functions, and functions might call other functions, some anonymous some named. The end result is either a large amount of functions, are just a couple of functions with more than 100 lines of code.

Is this maintainable? Hell no!

So why don’t I write code in JavaScript like I do in C#? Uh is that possible at all? I recall everybody telling me JavaScript is not an object oriented language. I still have to agree JavaScript doesn’t have all of the object oriented language characteristics to call  it a fully object oriented language. But still.

JavaScript objects can be created in different ways, but you’ll have to know a little bit more about some of the JavaScript language features to be able to get things working like you expect.

Some things were completely new to me like Closures: JavaScript Closures for Dummies

Take some time to get to know the above JavaScript features.

With the above concepts you can learn a little bit about JavaScript patterns, which help you when you want to write your own JavaScript library.

Really interesting articles that can help, but if you need more, you should definitely take a look at the pluralsight course for Structuring JavaScript Code.

PS. Don’t feel hurt, there are exceptions to what I write here. Actually if there weren’t good JavaScript devs out there the rest of the world, like me, couldn’t learn from their expertise.

Improve the YSlow score - remove the ETags

There are a lot of resources on the web which tell you how to create a fast website. You can measure the speed with for example YSlow. Do you want reasons? Maybe it’s good to read for example Scott Hanselman’s blog or his recent podcast in which he interviews Steve Souders the creator of YSlow.

I’m not here to discuss the reasons, but one of the things that can improve the speed of your website is to get rid of the ETags. ETags are essentially a kind of checksum that can be used to determine if a file on the server has changed. I wanted to improve my YSlow score, and part of the way to improve it was removing the ETag. But how?

Manipulate the HTTP headers through configuration?

1. Remove the header

<httpProtocol>
    <customHeaders>
        <remove name="ETag" />
    </customHeaders>
</httpProtocol>

Doesn’t work. The header is still sent.

2. Set the header explicitly to empty string

<httpProtocol>
    <customHeaders>
        <add name="ETag" value="" />
    </customHeaders>
</httpProtocol>

Doesn’t work either. The header is still sent.

What does work? A HTTP Module?

Alright, let’s try it, write a small HTTP Module and manipulate the headers before they are sent to the client. I just tried to remove the tag.

public class RemoveETagModule : IHttpModule
{
    public void Dispose() { }

    public void Init(HttpApplication context)
    {
        context.PreSendRequestHeaders += OnPreSendRequestHeaders;
    }

    void OnPreSendRequestHeaders(object sender, EventArgs e)
    {
        HttpContext.Current.Response.Headers.Remove("ETag");
    }
}

Of course we need to configure it. So add the below code to your web.config file.

<system.webServer>
  ...
  <modules runAllManagedModulesForAllRequests="true">
      <add name="RemoveETag" type="MM.Website.Web.Modules.RemoveETagModule, MM.Website.Web" />
  </modules>
  ...
</system.webServer>

Did it work? So let’s run Fiddler.

image

Yes, it’s gone.

Book review: ASP.NET MVC in Action now in print

image Jeffrey Palermo, Ben Scheirman and Jimmy Bogard, real community leaders, have written the ASP.NET MVC in Action book. I did have the honour to review the MS of the book in very early stages. They were already writing this book when ASP.NET MVC was still in a preview stadium.

Although I followed the development of ASP.NET MVC, I didn’t have time to try every feature. After the final release of ASP.NET MVC this book helped me better understand ASP.NET MVC. Reading this book will give you control over ASP.NET MVC. If you want to learn ASP.NET MVC this book will really help you. It covers best practices and recipes. Besides all the basics, you will really get at speed level if you combine reading sessions with development sessions.

Advertisement?

Yes this is kind of an advertisement, but I might end up with a set of licenses. That's why. Unit Testing ASP.NET? ASP.NET unit testing has never been this easy. Typemock is launching a new product for ASP.NET developers – the ASP.NET Bundle - and for the launch will be giving out FREE licenses to bloggers and their readers. The ASP.NET Bundle is the ultimate ASP.NET unit testing solution, and offers both Typemock Isolator, a unit test tool and Ivonna, the Isolator add-on for ASP.NET unit testing, for a bargain price. Typemock Isolator is a leading .NET unit testing tool (C# and VB.NET) for many ‘hard to test’ technologies such as SharePoint, ASP.NET, MVC, WCF, WPF, Silverlight and more. Note that for unit testing Silverlight there is an open source Isolator add-on called SilverUnit. The first 60 bloggers who will blog this text in their blog and tell us about it, will get a Free Isolator ASP.NET Bundle license (Typemock Isolator + Ivonna). If you post this in an ASP.NET dedicated blog, you'll get a license automatically (even if more than 60 submit) during the first week of this announcement. Also 8 bloggers will get an additional 2 licenses (each) to give away to their readers / friends. Go ahead, click the following link for more information on how to get your free license.

Microsoft DevDays 2009 – I’m attending!

DevDays 2009 This year will be another year I’m attending the Microsoft DevDays. DevDays 2009 will be held in The Hague, The Netherlands. This year my focus will be largely to attend Silverlight sessions, but also Windows Azure and ASP.NET MVC are part of the sessions I’m interested in.

Sessions I’m attending on day 1 – Thursday 28th of May:

My Sessions for DevDays 2009 Day 1

Sessions I’m attending on day 2 – Friday 29th of May:

My Sessions for DevDays 2009 Day 2

Are you also attending the Microsoft DevDays? And what sessions are you going to attend?