0 What ASP.NET Developers Should Know About JavaScript - Part 1

Part - 1Part - 2Part - 3

JavaScript – It's beat up, put down, shrugged off and kicked around. Cursed by the web browser's inconsistency yet blessed by a pervasive ubiquity -it's a technology many try to disregard even when its potential is something few can ignore. If you want to write an interactive application for the web today, then you'll need some JavaScript code on your side.

This article approaches JavaScript from the perspective of an ASP.NET developer who is comfortable with the paradigms and patterns of either C# or Visual Basic. The article doesn't look at how to use JavaScript from ASP.NET exactly, but it does look at why JavaScript is so different from the two languages we commonly use with the .NET CLR. The article assumes you already know that JavaScript is a loosely-typed language (because you don't have to declare the type of data you store in a variable), and that the syntax is similar to the C family of languages (with charming curly braces and stunningly beautiful semi-colons).

What Is Wrong With JavaScript?

The introduction didn't paint a flattering picture of the JavaScript language, but the truth is JavaScript is a good language. The biggest sources of pain when programming with JavaScript aren't because of the language. The biggest pains come from:
  • The tools
  • The implementations
  • The bad practices

JavaScript Tools
Most of the tools we use in a Visual Studio environment are geared to languages targeting the CLR. If you program in C# or Visual Basic, you'll be assisted by Intellisense, class browsers, class diagrams, code snippets, code analysis, and a world class debugger. The menus will list refactoring commands, and unit testing is only a few keystrokes away.

Contrast the above with the experience of programming with JavaScript. There is no Intellisense (although the feature is coming in the next version of Visual Studio), the debugger is finicky, and most of the other tools listed above are missing entirely. Of course, the majority of the code we write in Visual Studio is not JavaScript, but as the demands of the web have required more scripting, we've started to need better tools. The lack of tools makes JavaScript more difficult to work with.

Bad Implementations
JavaScript is hosted by many different types of web browsers, and is generally our primary means to manipulate a browser's DOM. While ostensibly governed by W3C standards, we all know each browser contains variations and idiosyncrasies. Script code that works on one version of a browser might not work on a different version of the same browser. These scenarios cause a lot of pain in testing and re-writing of JavaScript code. This pain isn't the language's fault – we will never see the day when all browsers implement web standards with 100% accuracy.

Bad Practices
JavaScript is an accessible language. We don't need special tools or compilers. We can view the source code of any page on the Internet and copy the script code for our own purposes – and many people do. Of course, not everyone who uses JavaScript is a software developer with an eye for good code. All sorts of people use JavaScript, and all sorts of ugly JavaScript code perpetuates itself on the Internet.

Even software developers (the author included) have taken a quick and dirty approach to writing JavaScript. It's only script code, after all, and just slapping the code into a text editor to get the desired result is all we need. It's not until we have to untangle a mess that we realize a more disciplined approach would have ultimately saved us time.

With all of these problems in the JavaScript environment – why do we still want to torture ourselves by writing JavaScript code?

What is Right with JavaScript?
Over the last few years, the amount of JavaScript code you'll find in the typical web application has surged. There are a couple good reasons for the surge:
  • JavaScript is ubiquitous
  • JavaScript is mature
Ubiquity
If you want to write an application that will reach as many users as possible, then you'll be writing a web application. You can reach users on Windows, Macintosh, Linux, and hundreds of other platforms on devices both large and small.

How will you make a web application interactive? You'll use JavaScript. Your users won't have to install a runtime, or an ActiveX control, or download some interpreting engine. They'll install a web browser that includes JavaScript support, as so many do, and they'll happily use your application. JavaScript is the most ubiquitous programming language on the planet.

Maturity
As the demand for JavaScript code has increased, the frameworks and libraries of well-tested and robust JavaScript code have begun to emerge. Many of these frameworks abstract away the browser idiosyncrasies we discussed earlier, and can greatly reduce the amount of time we invest in writing and debugging cross-platform JavaScript code. Here are some of the most popular frameworks:
  • ASP.NET AJAX
  • Prototype
  • Script.aculo.us
  • The Dojo Toolkit
  • Yahoo! UI Library
We've also begun to see the emergence of proven practices and design patterns. The practices put the object oriented features of JavaScript to good use. What's that? You didn't know JavaScript was object oriented? We might not have applied OOP practices to JavaScript code over the last few years, but the capability does exist.
Part - 1Part - 2Part - 3

0 comments:

 

Dot Net - Pros & Cons Copyright © 2011 - |- Template created by O Pregador - |- Powered by Blogger Templates