Sergio and the sigil

Blocked .js files keep biting me

Posted by Sergio on 2009-04-03

This is a tip for those working with JavaScript (and possibly other types of files) that is freely available for download. For the nth time in the last few months I spent an unnecessary amount of time trying to find a problem in my code that wasn't really there.

Windows keeps track of the files you save to your hard disk which came from the Internet. Maybe you have seen this problem when you try to open a CHM file that you downloaded and see that it opens to a blank page. The same type of thing happens with JavaScript files that you download. Let's see what happened to me today.

First I downloaded the latest version of QUnit and saved to my application's directory. I did this the usual way, as shown in the image below.

Then I proceeded to use the downloaded testrunner.js file in my HTML page, via a <script src="testrunner.js"></script> tag. When I tried to open my HTML page, I was not expecting to see the particular error shown below.

I started to look for common problems: Did I save the file in the right place? Did I spell its name right? Did I mispell the function name? Did I miss a dependency? No, no, no, and no. All looked good.

That's when I remembered that the file might have been flagged by Windows. I opened its properties dialog and, sure enough, there it was, the infuriating Unblock button.

Ah, that's easy. I just clicked it and refreshed my page. Wah-wah! Problem persists. I tried typing the .js url in the address bar and saw this 401 error message from IIS, not 404.

That told me that there was still something messed up with file access. I checked the file's permissions, comparing to an older .js file in the application. The new one did not grant access to local users, so I added Read access to local users.

Now it works. Depending on how your IIS and/or ASP.NET security is configured, you may not need this last step (e.g. if the process identity already has admin rights, in which case I salute you, brave friend.) The system at hand was a Win2008-x64 installation with the default IIS/ASP.NET settings.

This problem seems to happen to me every other week and I still did not develop the reflex to take care of it right when I download the file. On the other hand, I'm getting better at remembering about it when I see stuff like "function not defined" in an error message.

JavaScript: Avoid the Evil eval

Posted by Sergio on 2009-03-31
This post is part of a series called JavaScript Demystified.

I'm pretty sure by now you have heard at least once that eval is evil. Some nuggets from Eric Lippert's post:

if you are considering using eval then there is probably a better way
People, eval starts a compiler.

I'm also pretty sure I don't need to tell you that anytime you have an explicit eval() in your code, there's a good chance it's there because you're not taking JavaScript seriously yet.

//mandatory square brackets notation example
//------------------------------------------
var myObject = new MyObject();
var prop1 = eval( 'myObject.' + somePropertyName ); // BAD!
var prop2 = myObject[ somePropertyName ]; // Better

That's not the end of all eval()

At this point some people might be feeling relieved: "Phew! That was easier than it sounded. Getting rid of eval is a piece of cake." Well, I'm not going to say it's significantly harder than that, but we're not done prunning eval() from our code just yet.

You see, eval() is like that coward opponent that sneaks in the dark to attack you from behind. Let's find some of eval's favorite hiding places.

There's a time when you need a timer

Two very popular JavaScript functions used to create timers are setTimeout() and setInterval(). Here's how you still find code being written to use them.

function doSomething(someValue){
	//...
}

setTimeout("doSomething('3 seconds elapsed. Time is up.');", 3000);

As it turns out, this is just another occurrence of eval() revealing how incompetent we can still be in this programming language. Here's a better way to write that code.

setTimeout( function(){ 
                doSomething('3 seconds elapsed. Time is up.');
            }, 
            3000);

Thank God I didn't know functions had constructors

The other secret place that eval() likes to hang out is in the Function constructor. Fortunately this isn't exactly a popular way of creating functions. I'll say it: I didn't even know about this constructor until less than a couple of years ago.

So, in case you don't know what I'm talking about here, I'll show you how to use the function constructor just to imemdiately tell you to not do it.

var sum = new Function('op1', 'op2', 'return op1 + op2;');
var result = sum(10, 20); // ==> 30

The above code is roughly equivalent to the explicit eval() usage below.

eval("var sum = function (op1, op2) { return op1 + op2; }");
var result = sum(10, 20); // ==> 30

We don't come up with the need to use the function constructor often, but I'll admit that when we do, it's usually hard to replace it with another way that doesn't use eval().

Minor update: I was doing some snooping around with Firebug and Reflector and found that WebUIValidation.js (embedded in System.Web.dll) does use eval() in some ways that I just pointed out to be unnecessary. If that is of any comfort to anyone that has done the same in the past, there you have probably the largest deployment of misused eval() I know of.

Uncle Bob shows FitNesse and Slim

Posted by Sergio on 2009-03-28
UPDATE: Steve has published the video of the presentation.

There are still a few seats left for the next Chicago ALT.NET meeting. The meeting is on April 8th and we will have a demonstration of FitNesse by Robert Martin.

FitNesse is used by many people for acceptance testing and many in our group wanted to understand it better. So come and see what this is all about.

Collaborative Acceptance Testing with FitNesse

6:00 pm
Pizza and networking time

6:30 pm

Robert Martin is back, this time to talk about FitNesse, an Acceptance Testing turned into Wiki framework built on top of Fit.

Who else would you want to see talking about FitNesse? Uncle Bob, was one of its creators and is its maintainer. In this session he will explain what Acceptance Testing, Fit and FitNesse are, why they are useful and how to best use FitNesse in your process.

He'll also show off Slim, the new test-system that supersedes Fit and enables a whole hose of new features and capabilities.

Robert C. Martin has been a software professional since 1970. In the last 35 years, he has worked in various capacities on literally hundreds of software projects. He has authored "landmark" books on Agile Programming, Extreme Programming, UML, Object-Oriented Programming, and C++ Programming. He has published dozens of articles in various trade journals. Today, He is one of the software industry's leading authorities on Agile software development and is a regular speaker at international conferences and trade shows. He is a former editor of the C++ Report and currently writes a monthly Craftsman column for Software Development magazine.

Mr. Martin is the founder, CEO, and president of Object Mentor Incorporated. Object Mentor is a sister company to Object Mentor International. Like OMI, Object Mentor is comprised of highly experienced software professionals who provide process improvement consulting, object-oriented software design consulting , training, and development services to major corporations around the world.

7:45 pm

Time for our monthly open discussion. Aside from any specific topic that anyone wants to bring to the group, we can continue the discussion on Acceptance Testing and report the progress on the effort to produce the Chicago Code Camp.

Talk - JavaScript at Rockford .NET

Posted by Sergio on 2009-03-18

Next Tuesday, March 24th I'll give a JavaScript talk at the Rockford .NET user group.

This talk will be basically the same one I gave for the LCNUG back in December.

The few opportunities I had to give this talk were highly rewarding. It's priceless to see developers realizing how mistakenly they had been using JavaScript, and finally being able to understand why "that bug" was happening in their pages.

So, if you live in the area and think there must be something about JavaScript that you're just not getting, please try to stop by and ask your questions.

JavaScript - Beyond the Curly Braces

One of the greatest problems with JavaScript is its superficial syntax resemblance of C-style languages. We call it the curse of the curly braces.

That is also a very large source of frustration for developers trying to learn JavaScript beyond the basics. Thinking that JavaScript is somehow related to Java or even "It's almost like C# but a little simpler" is an unfortunate and common occurrence that can only lead to trouble.

In this session we will analyze some of the fundamental differences between JavaScript and C#/Java. We will highlight the pitfalls that can trap us and the appropriate workarounds for them.

Time permitting and if there's interest we will take a look at Idiomatic JavaScript, which will help us understand how JavaScript is being written these days. Learning about this will also help you when trying to read the source code or even the documentation and samples for popular JavaScript libraries like jQuery, Prototype, YUI, etc.

Easy project administration with Unfuddle

Posted by Sergio on 2009-03-08

I don't come out often to sing praises for any product, I'd say I even tend to complain more than anything. That said, sometimes I come across a product that strikes all the right chords.

Recently I started using Unfuddle and ...just Wow! What a delightful experience. I can't say I was exactly surprised because I had heard really good things about it from a few people. Still, it's great to try it and confirm that same impression.

Unfuddle is clearly a product that was built with a lot of love and by people that understand the problem that needs to be solved.

Migrating a project - the Repository

I started moving one of my projects to Unfuddle. I started creating a new project and, with help from their support team, I moved my SVN repository (with the complete log history) into the project's repository.

The repository browser offers all you'd expect from a 1st class product. You can easily navigate the repository tree and go back an forth in the revisions trail. You can see what was changed in each commit, see the file by file diffs, and post notifications whenever a commit happens (great way to trigger your CI process.)

Managing the project

Right now I'm in the process of moving the list of bugs and planned features into the ticketing system. In Unfuddle I can plan milestones and version numbers for my project and associated tickets to them, which will let me see how far the project is from each milestone right in the project's dashboard.

Unfuddle reminds me of Trac, with the important difference that I don't need to go through the pain of getting it up an running and that configuring your project (or as many projects as you want) is much simpler.

One important thing for me in any product like this is the ability to create and maintain documents. This is provided through the Notebooks feature, which allows us to create collections of pages with basic formatting. If you need something much richer, like a diagram, or include an existing Word document, you can attach files to a notebook. A real file repository is probably a feature that could make Unfuddle even better.

Other nice things

I love how Unfuddle allows us to use Markdown or Textile to format pretty much everything that is important, like ticket descriptions and notebook text.

Unfuddle also supports git. I haven't been using git yet, but it's good to know that I'll probably not need to go looking for another service if I choose to migrate to git.

Very important disclaimer

Although I could probably get by with just the free plan in Unfuddle, I received a complimentary subscription to their Compact plan. The only important differences for my use cases would be the number of notebook pages and the ability to attach files.