January 2009

Volume 24 Number 01

Toolbox - C# JavaScript, Eric Lippert's Blog, And More

By Scott Mitchell | January 2009

All prices were confirmed November 19, 2008, and are subject to change. The opinions expressed in this column are solely those of the author and do not necessarily reflect the opinions of Microsoft.

Contents

Create JavaScript for AJAX Apps Using C# and Visual Studio
Blogs of Note
Spell-Check Your Comments, Strings, Markup, and Resource Files
The Bookshelf

Create JavaScript for AJAX Apps Using C# and Visual Studio

In the past, JavaScript was considered a toy language that was useful for simple input validation and Document Object Model (DOM) manipulation. But JavaScript is, in fact, a powerful and feature-rich language that powers today's AJAX-enabled Web sites.

And while the prevalence and role of JavaScript in modern Web applications have expanded dramatically over the past few years, there is still a very noticeable difference in the design-time experience when writing JavaScript code compared to writing C# code.

Visual Studio provides IntelliSense and debugging support for JavaScript, but many JavaScript errors and warnings cannot be detected until run time. On the other hand, C# offers compile-time-type checking; more robust object-oriented semantics; and tools for refactoring, creating technical documentation, and class browsing. Furthermore, most ASP.NET developers are more familiar with C# than with JavaScript.

Wouldn't it be nice if you could write C# code instead of JavaScript? This is possible with Script# (version 0.5.1), a free compiler, runtime, and framework created by Microsoft Software Architect Nikhil Kothari. The Script# compiler takes C# code and translates it into equivalent JavaScript code.

Use Script# to create short, one-off JavaScript functions or event handlers as well as larger JavaScript libraries. You can use it to create ASP.NET AJAX components and behaviors or Windows Vista Sidebar gadgets. The Script# compiler can use .resx files to generate localized script files and can be configured to generate release or debug scripts. (Debug scripts include white space and descriptive variable names for readability, whereas release scripts have unnecessary white space removed and variable renaming for code compactness.) In a nutshell, Script# gives you all the benefits of working with C# code—compile-time errors, familiar C# OOP syntax and semantics, and better tool support—while generating the JavaScript code needed for your Web application or Sidebar gadgets.

In addition to a compiler, Script# includes a runtime and a framework. Both the runtime and framework include a server-side implementation that you program against when writing C# code and a client-side implementation that is used by the JavaScript code generated by the Script# compiler.

The runtime defines the type system and it is mandatory. The framework is optional, but offers APIs for performing common tasks such as HTTP requests, JSON serialization, browser detection, and so forth. The framework also includes user interface controls and APIs to work with the Silverlight control in the browser, to access the RSS store in Internet Explorer 7, and to access Microsoft Virtual Earth from within the browser.

Keep in mind that Script# is not designed to convert existing C# applications into AJAX-enabled Web applications. Rather, Script# gives developers the ability to create Web applications or Sidebar gadgets by writing C# code that is then converted to JavaScript code.

However, because there is not a one-to-one correspondence between C# and JavaScript, not all of the C# language features are available when using Script#. For instance, write-only properties are disallowed, as are certain constructs and keywords, including goto, using, lock, and sizeof.

Price: Free.

projects.nikhilk.net/ScriptSharp

Blogs of Note

The most important ingredients for a really great technical blog include the following: frequent, knowledgeable, on-topic posts; in-depth examinations of important technologies or concepts; and a unique voice or style. You'll find these ingredients in just the right proportions on Eric Lippert's blog.

Eric is a Senior Software Design Engineer at Microsoft who has worked with both the scripting languages and C# teams. His blog offers many great posts that focus on security, performance, and code quality, along with entries on language semantics and features such as lambda expressions, immutability, and covariance and contravariance. He explores many of these topics in great depth, often spanning several blog posts.

There's a 5-part series on lambda expression versus anonymous methods, an 11-part series on immutability in C#, and a 5-part series on protected member semantics. You will also find posts with Eric's insights and opinions on ways to use (or not to use) various language features. Read his take on arrays in his blog article "Fabulous Adventures In Coding."

While many of the entries on the blog examine complex topics, Eric has a talent for explaining the concepts and theory in straightforward, down-to-earth terms.

blogs.msdn.com/ericlippert

ericlippert.gif

Eric Lippert Blogs about Code Security, Performance, and Quality (Click the image for a larger view)

Spell-Check Your Comments, Strings, Markup, and Resource Files

Compilers serve as a spell-checker of sorts. If you transpose two letters in a method name or mistype a variable name, the compiler reports a build error. Fortunately, these errors are easy to identify, as Visual Studio underlines the misspelling with a squiggly line.

However, note that the compiler does not serve as a spell-checker for source code comments; strings in code or in resource files; or HTML, XML, or XAML markup. It is all too easy for a misspelled comment to make its way into the technical documentation, or worse, for a misspelled string to slip past QA and into production.

The good news is that there are tools to spell-check the strings, comments, markup, and other content that is opaque to the compiler. One such tool is IntelliSpell 2.0 by ComponentOne. IntelliSpell works much like the spell-check in Microsoft Office Word. IntelliSpell underlines any misspelled word with a squiggly line. Right-click on the misspelled word to see a list of possible corrections. These misspellings are also enumerated in the Task List window.

You can correct a misspelling from the Task List or jump to the misspelled word by double-clicking it in the Task List. You can run the spell-checker at any time by selecting the menu option from the Tools menu or from the IntelliSpell toolbar. IntelliSpell can also be configured to check your spelling automatically as you type and to run the spell-checker after each build.

By default, IntelliSpell checks the spelling in all code files, Web pages, XML files (including configuration files), resource files, and text files. It ignores words in mixed case, words with numbers, and Internet and file addresses, although these rules and what types of files are spell-checked are configurable. And IntelliSpell offers spelling dictionaries in 16 different languages; these dictionaries can be used alone or in tandem when you are spell-checking localized projects.

IntelliSpell comes in two editions: a free Community Edition and a Professional Edition that costs $79.99. The Community Edition has the core features—Visual Studio integration, underlining misspelled words, spelling suggestions via a context menu, and so on—but lacks some of the features found in the Professional Edition.

With the Community Edition, you can only spell-check a single file at a time. The Professional Edition, however, is able to spell-check an entire Project or Solution. Also, features such as spell-checking as you type, spell-checking localized projects, and the ability to create custom dictionaries are not included in the Community Edition.

Price: Community Edition is free. Professional Edition is $79.99.

componentone.com/

intellispell.gif

IntelliSpell Spell-Checks Strings, Comments, and Markup (Click the image for a larger view)

The Bookshelf

In all professions and hobbies there are certain areas that everyone, regardless of their experience, should study. Every golfer, despite her skill level, can certainly improve by working on her short game, for instance.

For software developers, you can never be too knowledgeable about the syntax and features of SQL. And while there are plenty of online articles that include SQL tips and tricks, I have found that the best way for me to hone my SQL skills is by methodically working through a book on the subject.

I recently finished the book Pro T-SQL 2008 Programmer's Guide (Apress) by Michael Coles, which provides a thorough review of the T-SQL syntax and language features. The book begins with a look at the new features in T-SQL 2008, such as the MERGE statement and new data types. The author then moves on to the core T-SQL constructs—user-defined functions; stored procedures; triggers; XML, XQuery, and XPath support. He then continues with coverage on encryption, common table expressions and windowing functions, SQL CLR programming, and error handling, among others. Each of these topics is given its own chapter with in-depth discussion and examples. And each chapter concludes with a series of exercises whose answers are provided in the appendix.

I especially enjoyed the chapter on performance tuning. This chapter starts with an overview of how SQL Server arranges your data on disk and how the arrangement affects performance, along with tips for saving disk space and for storing sparse data. Next, Coles discusses the performance benefits of indexes and compares and contrasts clustered, non-clustered, and filtered indexes. The chapter concludes with a discussion on analyzing query plans and tips for optimizing queries.

Pro T-SQL 2008 Programmer's Guide assumes that the reader is already familiar with T-SQL basics—there are no beginner-level discussions on SELECT, INSERT, UPDATE, or DELETE statements. Moreover, this book concentrates on T-SQL and does not explore database administration, although there is a short chapter that outlines tools such as SQL Server Management Studio, SQL Profiler, SQLCMD, and BCP. There is also a chapter that shows two different ways to interface with SQL Server from a Microsoft .NET Framework client application: using the ADO.NET SqlClient provider and LINQ to SQL.

Time spent learning new SQL skills and practicing the old ones is always time well spent. Pro T-SQL 2008 Programmer's Guide, with its straightforward prose, countless samples, and end-of-chapter exercises, is a great way to learn about the latest features in SQL Server 2008 and to bolster your existing SQL skills.

Price: $52.99.

apress.com/

bookcov.gif

Send your questions and comments for Scott to toolsmm@microsoft.com.

Scott Mitchell, author of numerous books and founder of 4GuysFromRolla.com, is an MVP who has been working with Microsoft Web technologies since 1998. Scott is an independent consultant, trainer, and writer. Reach him at Mitchell@4guysfromrolla.com or via his blog at ScottOnWriting.NET.