The Benefits of Static Typing

Writing reliable software is hard. It is not difficult to write programs of several dozen or even hundreds of lines of code without bugs . But writing large applications is a difficult job even for the most experienced developer. Although many large applications are written in scripting languages such as Javascript and Perl, programmers debug such applications almost entirely at run-time, because scripting languages provide few compile-time checks. Most modern compiled languages (Java, C++, C#, ML, Haskell, etc.) provide static typing to help build large, reliable applications. Static typing can help by detecting common type errors in a program during static analysis so that they can be handled instead of being discovered only when the program is run.

Static typing helps programmers have confidence in the correctness of their programs. A type-correct program is guaranteed never to raise a type error in any evaluation. A type error occurs when the type of an expression is not compatible with the type required by the context in which the expression is used. For example, an expression whose type is a date typically cannot be used in an expression that requires a floating-point number. Because static analysis examines a program's expressions, not the values that those expressions produce, it must be conservative; that is, a type error may be raised during static analysis, even though running the program may raise no type errors. For example, the type system may determine that an expression has type element(surgeon) element(plumber) , meaning that it will return either a surgeon or plumber element. It will raise a static error if that expression is used in a context requiring a plumber element, even though every actual evaluation of the expression may yield a plumber element and so would not raise a dynamic error. Further, although static typing allows us to detect all potential type errors, it cannot detect all potential errors of other kinds. For example, it cannot detect errors that depend on the values of expressions at run-time, such as divide-by-zero or array-bounds-overflow errors.

To use a statically typed language, the developer must have a basic understanding of the language's type system and what it can express. A developer using Java or C++ must understand the fundamentals of object-oriented programming. The developer also must have a basic understanding of how the type system works, so that she can understand what type errors mean. She must understand how to work around the type system when it is too restrictive . For example, Java developers know how to apply downcasting when the type system cannot infer a precise enough type.

Static typing is most effective for applications that use authoritative and stable schemas to describe their input and output data and that access XML data sources that are valid with respect to those schemas. Most serious applications of XML (electronic commerce, large-scale web publishing, relational data integration, etc.) define and use authoritative schemas and require a high level of reliability that static typing can help to achieve. Not all XML applications, however, consume or produce schema- validated data, so static typing is an optional conformance feature in XQuery.

This chapter explains how to use the XQuery static type system to write type-safe queries, how to understand and resolve type errors, and how to work around the type system when necessary. We start with one example of an application that can benefit from static typing. Then we explain the fundamentals of the XQuery type system and describe the kinds of errors it can detect. Along the way, we show how to work (safely!) around the cases where the type system may be too conservative.



XQuery from the Experts(c) A Guide to the W3C XML Query Language
Beginning ASP.NET Databases Using VB.NET
ISBN: N/A
EAN: 2147483647
Year: 2005
Pages: 102

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net