Coding the Web
By Tim Anderson
Languages like JavaScript specifically target the World Wide Web, as does Adobe’s Flex platform. Tim Anderson explains.
HardCopy Issue: 39 | Found In: Development | Published: 01/02/2008 | Last Revision: 06/07/2010
JavaScript began in 1995 when Brendan Eich, an engineer at Netscape, designed a lightweight scripting language for Web browsers that would be familiar to C and to Java developers. It was initially called LiveScript but Netscape agreed with Sun on a change of name to JavaScript on the grounds that it could also be used to script Java objects. The name was good marketing but technically confusing since JavaScript has no dependence on Java and is a completely different language. Microsoft implemented a JavaScript clone called JScript, and the language became standardised as ECMAScript. It quickly became essential for authoring Web pages.
ECMAScript
JavaScript 1.x and ECMAScript up to and including version 3.0 are case-sensitive and use braces for statement blocks and semi-colons for line endings. The language uses dynamic typing, which means that the type of a variable is determined by its value, and it is object-based although it does not support classes. Instead you can freely add properties and methods to custom objects at runtime. It is also possible to simulate classes using a technique called prototypes.
Another unusual feature of JavaScript is that functions are first-class objects and can be passed as arguments to other functions. This gives JavaScript good credentials as a dynamic language. Although interpreted and therefore a little slow, JavaScript is a powerful language with few inherent limitations. However security considerations and the browser environment restrict its capability when running within a Web browser.
JavaScript 2.0
Two recent developments have given JavaScript a boost. One is the increasing use of extensive JavaScript libraries to improve the user experience for Web applications. The idea is to avoid full page refresh by making calls to the server in the background using JavaScript and updating only the portions of the page that have changed. This technique is called AJAX and is heavily used by Google and others in their online applications.
The second trigger is Adobe’s efforts to establish Flash player as a platform for client applications. Flash has long included an implementation of JavaScript called ActionScript but was marred by poor performance. This changed with the release of ActionScript 3.0 in Flash Player 9 which represents a huge leap forward for two reasons. Firstly it implements most of ECMAScript 4.0, a major revision to the language which adds features including classes and interfaces, strong typing, packages and namespaces. In other words, with ECMAScript 4.0 the language grows up to be more like Java or C#.
Second, Adobe has implemented a new ActionScript Virtual Machine that includes a Just-in-Time (JiT) compiler. Again, this is something which both Java and .NET have included for some years. Anyone who thinks that ActionScript is just a toy scripting language should re-examine this latest incarnation.
Another twist is that Adobe has handed over much of its ActionScript Virtual Machine to open source where it forms the basis of the Tamarin project at Mozilla. This means that a fast ECMAScript 4.0 implementation is coming to FireFox, though it will be called JavaScript 2.0. This is mostly backwards-compatible with earlier versions.
Microsoft is resisting pressure to upgrade its JScript engine in Internet Explorer to ECMAScript 4.0, perhaps to promote Silverlight for client-side code in the browser, but Mozilla is countering with plans to enable Tamarin within Internet Explorer.
ActionScript and Flex
Adobe's Thermo is a forthcoming tool that converts Photoshop artwork into Flex objects.
Adobe’s Flash is popular with designers but the Flash IDE is alien and difficult to most developers. In order to establish the Flash runtime as a developer platform, Adobe came up with Flex. This is based on MXML, an XML language that gets compiled into a Flash SWF binary. MXML is essentially a language for defining a graphical user interface. ActionScript code can be embedded into MXML within a Script element, designated as CDATA because the script itself is not valid XML. Alternatively you can write script in separate files, similar in some ways to the way code-behind works in ASP.NET.
Flex applications run in the browser. Adobe’s AIR (Adobe Integrated Runtime) project enables the same technology to be used for desktop applications. AIR applications run in their own window, like any other desktop application, and have full access to the file system. They can also use a local database engine, based on the open source SQLite. Cross-platform and security considerations limit other access to operating system resources. AIR applications install and update easily from the Internet. They are best thought of as hybrid applications, with the appearance of desktop programs but many of the characteristic of Web applications. AIR is particularly strong for multimedia and can play H264 video with hardware acceleration.
AIR actually supports two development models. The first is simply HTML and JavaScript running outside the browser through an embedded version of WebKit, as used by Apple for Safari. The second model is a Flash SWF running in a local desktop window.
In both cases the logic of the application is written in JavaScript, though confusingly the different models use different virtual machines. HTML applications use the Safari JavaScript engine, which is JavaScript 1.x and interpreted, while Flash application use ActionScript 3.0, which is JIT-compiled. The performance of the native Flash option is strikingly better. The situation is further confused by the ability to include Flash content within an HTML application.
Coding Flex
The design tool for Flex is FlexBuilder which is based on the open source Eclipse tools framework. The latest version is 3.0, in beta at the time of writing. Although not as comprehensive as Microsoft’s Visual Studio, FlexBuilder is considerably improved in version 3.0. There are many new components including an advanced DataGrid which supports hierarchical data and pivot tables. New tools include refactoring and performance profiling. There is full support for Adobe AIR and a component kit for the Creative Suite 3 design tools that makes it easier to build Flex components. In preparation is a more sophisticated design tool for Flex, codenamed Thermo, which ties in with the designer’s favourite tool, Adobe Photoshop.
FlexBuilder includes a visual designer for MXML which lets you drag-and-drop components from a palette onto the design surface, just as you would expect if you are familiar with Visual Studio or Delphi. You can tab between source and visual editors, and changes in one editor show up immediately in the other. There is a property editor but developers coming from Visual Basic or the like will find that they need to do a little bit more work hooking up controls and events than they are used to. Another difference is that typically (and rightly) Flex applications are designed to be resolution-independent, so you need to think carefully about layout management.
This is Flash so it is easy to include impressive visual effects. It is also relatively easy to call on Web services, either using plain XML, SOAP or Adobe’s LiveCycle server which lets you call methods on remote Java objects. A benefit of LiveCycle is that you can use AMF (Action Message Format), a binary protocol optimised for performance.
How to shoot yourself in the foot in JavaScript:
// You perfect a robust, rich user experience for shooting yourself in the foot but you then find that bullets are disabled on your gun.
_________________________________________________________________
Delphi
Microsoft’s Visual Studio now has only one language capable of compiling directly to native Win32 code, which is Visual C++. Unfortunately, developing in C++ is substantially more demanding than C# or Visual Basic, leaving an awkward gap for developers who want to target native code without giving up rapid application development. The solution is to use CodeGear’s Delphi.
Delphi offers a visual development environment, a language based on Pascal, and a rich component library that greatly speeds productivity. Although it comes from the same era as the original Visual Basic, Delphi does not suffer from the same limitations. One of its architects, Anders Hejlsberg, went on to design C#, so it is not surprising that it shares some characteristics. Its language is both object-oriented and component-oriented. If you need to call an advanced Windows API, or work at a low level with pointer arithmetic or callbacks, Delphi can easily meet the need. The resulting executable is relatively small and fast, while avoiding dependency on the .NET Framework simplifies deployment and improves compatibility with older versions of Windows.
Delphi’s language is case-insensitive. Lines of code are terminated with a semi-colon, as in C or C++. Blocks of code are marked by Begin..End statements rather than braces. Delphi is strongly typed and its performance is excellent. It has strong exception handling. Memory management is mostly transparent, but memory functions are there when you need them.
Borland made major changes to the IDE after Delphi 7, unfortunately introducing some instability which took several versions to resolve, but the latest Delphi 2007 is a solid product. A strong feature of all Delphi IDEs is that you can flip seamlessly between visual designers and code, making modifications in either editor, and everything stays synchronised. The IDE is now called RAD Studio, and is also used by a .NET version of Delphi, and by C++ Builder which uses the same Visual Component Library as Delphi but with the C++ language. Delphi 2007 is the first version with proper support for Windows Vista. It also now uses MSBuild, the same build system as Visual Studio 2005 and higher.
There are several options for database access, which can be confusing. It is worth highlighting dbExpress, a fast, lightweight API which works in both Win32 and .NET versions of Delphi and which is substantially improved in Delphi 2007.
Most Delphi developers favour native code development because that is the key advantage of Delphi over Visual Studio. The .NET version of Delphi is useful for migrating projects to the .NET Framework, perhaps to take advantage of the Framework libraries, to integrate with other .NET applications, or to run behind an ASP.NET Web application. Delphi tends to lag behind Visual Studio in supporting the latest .NET features, and examples currently include LINQ and Windows Presentation Foundation, neither of which are yet supported. Future plans for Delphi include native 64-bit code, full Unicode support, and parameterised types (in other words, generics).
How to shoot yourself in the foot in Delphi:
// The compiler won’t let you shoot yourself in the foot.