Fabulous Adventures In Coding

Eric Lippert's Blog

Attention passengers: Flight 0703 is also known as Flight 451

I hate octal.  Octal causes bugs.  I hate bugs, particularly stupid "gotcha" bugs.

  Foolish C programmers do things like

 

int arr_flight = 0703;

 

not realizing that this does not assign the number 703, but rather 7 x 64 + 3 = 451.

 

Even worse, foolish JScript programmers do things like

 

var arr_flight = 0708;

var dep_flight = 0707;

 

not realizing that the former is a decimal literal but the latter is an octal literal

 

Yes, in JScript it really is the case that if a literal begins with 0, consists of only digits and contains an 8 or a 9 then it is decimal but if it contains no 8 or 9 then it is octal!  The first version of the JScript lexer did not implement those rules, but eventually we changed it to be compatible with Netscape's implementation. 

 

This is in keeping with the design principle that I mentioned earlier, namely "Got a problem? Muddle on through!"  However, since this problem can be caught at compile time, I think that the decision to make illegal octal literals into decimals was a poor one. 

 

It's just a mess. Octal literals and escape sequences have been removed from the ECMAScript specification, though of course they live on in actual implementations for backwards compatibility.

 

This is why I added code to JScript .NET so that any use of an integer decimal or octal literal that begins with zero yields a compiler warning, with one exception.  Obviously x = 0; does not produce a warning!

Published Thursday, October 23, 2003 12:13 PM by Eric Lippert

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Jack Shainsky said:

Actually, not always the problem can be caught at compile time. If server generates pages with JS dynamically, the number containing 8 or 9 can appear in actual script once in a while. So the decision to correct the problem on the fly is not so bad as it seems.
October 23, 2003 4:47 PM
 

Eric Lippert said:

Well, sure, but an ASP page could also generate a JScript block full of unterminated strings, and we catch those at compile time.
October 23, 2003 5:10 PM
 

Kim Gräsman said:

"This is why I added code to JScript .NET so that any use of an integer decimal or octal literal that begins with zero yields a compiler warning" Out of curiosity: How does the warning manifest itself? A callback to the script host? Or is JScript.NET not part of the whole Active Scripting family?
October 24, 2003 9:27 AM
 

Eric Lippert said:

Correct -- JScript .NET does not participate in the old, 20th century unmanaged Windows Script interfaces. Rather, it uses the IVSA, aka "Script for the .NET Framework" interfaces. One of the shortcomings of the old Windows Script interfaces was that there was no way to surface warnings, only errors. IVSA provides a more fully-fledged compiler hosting mechanism. JScript .NET has some super-cool error recovery mechanisms for IVSA scenarios -- I might blog about them later, or convince Peter to.
October 24, 2003 6:14 PM
 

Fabulous Adventures In Coding said:

July 29, 2004 2:47 PM

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required
Submit

About Eric Lippert

Eric Lippert is a senior developer on the Microsoft C# compiler team. Before that he worked on the framework of Visual Studio Tools For Office. Before that, he worked on the compilers, runtimes and tools for VBScript, JScript, Windows Script Host and other Microsoft Scripting technologies. He lives in Seattle and spends his free time editing books about programming languages, playing the piano, and trying to keep his tiny sailboat upright in Puget Sound.

This Blog

Syndication


© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker