the blog
Back to -Blog

Semantic error detection in Amethyst 2

Locate problems before they happen
by Dermot Hogan
Wednesday 2 January 2013.

Typically, there are two classes of errors in code. The first are ’syntax’ errors, such as leaving a ’{’ out. These are easily detected and fixed. But the second type – ’semantic’ errors’ – where the syntax is correct but the meaning is nonsensical or incorrect can be a good bit harder to spot. We’ve added a number of semantic error checks in the forthcoming version 2 of our Amethyst IDE.

The classic example in C like languages of a semantic errors is the statement

if (x = 2) y = 5;

What is usually meant is

if (x == 2) y = 5;

This peculiar use of ‘=’ and ‘==’ goes right back to the earliest days of C and I think this must be responsible for more accidental errors than any other language construct in existence. Apparently, Ken Thompson (one of the authors of C) was a firm believer in minimising any typing. C was derived from a previous language, BCPL, via an intermediate language, B (and you can already see Thompson’s desire to eliminate any ’excess’ characters in the name of the language!). Thompson reckoned that = being one character shorter than the BCPL assignment operator := was much superior and used that (see Anyway, semantic error checking detects errors in meaning such as this example and flags them using, in Amethyst’s case, green ‘squigglies’.

You can turn on semantic error checking via an editor option:

We’ve included six semantic error checks:

1) Not all code paths return a value

2) A variable having the same name as a parameter

3) A variable being declared but not used

4) Use of an unassigned local variable

5) Assignment in test (that is, the example above)

6) Unreachable code

We’ve also implemented ’coercion’ checks – assigning a Boolean to an integer, for example. However, using Booleans and integers seems to be relatively common in the ActionScript code that I’ve come across. It’s pretty poor programming practise in my view. If you can to do something like that, use a cast to make it explicit. You can see this in the ’assignment in test’ example above where the expression

a =1

yields an integer, but strictly speaking, an ‘if’ expression requires a Boolean value.

This is a preview of the forthcoming version of Amethyst 2 – our ActionScript/Flash IDE for Visual Studio 2012 and 2010. If you buy Amethyst 1.0 for Visual Studio 2010 now you will be entitled to a free update to Amethyst 2 when it is released. See the Amethyst 2012 announcement for more information.

Bookmark and Share   Keywords:  Amethyst 2  development
© SapphireSteel Software 2014