Jump to content

Why no syntax error?


jekillen

Recommended Posts

Perhaps this will help someone who is having maddening problems with

managing simple true/false conditionals.

 

I have had a problem with the following code:

argument 'e' is event being being handled. The user has clicked on an item in the web page.

 

// etc...

case 'either':
if(heading == 'false')
{
heading = setFile(e); // if a file is detected, returns 'true', else returns 'false'
}
if(heading == 'false')
{
heading = setDir(e); // if a directory is detected, returns 'true', else returns 'false'
}
break;

// etc....

 

The explanation is:

The snippet is from a function that detects user generated event on visible item in the page.

The items to select are either directories or files from a file system directory listing.

case 'either' means that the user can select either files or directories from the list.

If setFile() returns 'true', the user has selected a file listing. So setDir() will not be run

 

The problem I was having is that setFile() would set heading to 'true' but setDir() was still

being run. The cause turned out to be in the code below. I had a semicolon after the conditional.

 

I was NOT getting a syntax error.

 

The browser I am using for developement is Firefox v34.0.5 on MacOSX v10.6.8

The presence of the semicolon appears to have had the effect of resetting heading to 'false'

when setFile() set it to 'true'.

 

The problem was this:

if(heading == 'false'); // <- Notice the semicolon?

{

heading = setDir(e)

}

Link to comment
Share on other sites

You didn't get a syntax error because there was no syntax error. The syntax is perfectly valid (although useless). The effect of putting that semicolon there was the creation of an empty if statement. What you had is the equivalent to this:

 

 

if(heading == 'false')
{
}
heading = setDir(e);
Link to comment
Share on other sites

I would think that the interpreter could figure out that this was not productive syntax, as it

occures where there is absolutely no sense for it. The interpreter should be able to recognize

a conditional statement, branching statement such as switch, and a looping

statement; and distinguish it from a function call; and find a semicolon after the closing

parenthesis to be adverse to the process of the code.

 

The use of string true or false instead of the boolean is because sometimes it comes from a

form field value or similar string representation, such as using php to initiate a javascript variable

via a php print statement.

Link to comment
Share on other sites

That is a job better suited to a static analyzer or code quality tool, of which there are a few if you are using build tools like Grunt or Gulp.

Link to comment
Share on other sites

I would think that the interpreter could figure out that this was not productive syntax, as it

occures where there is absolutely no sense for it. The interpreter should be able to recognize

a conditional statement, branching statement such as switch, and a looping

statement; and distinguish it from a function call; and find a semicolon after the closing

parenthesis to be adverse to the process of the code.

While all of that is technically true, it is not the job of the interpreter to figure out what you mean, the interpreter's job is to run the code. More specifically, the interpreter is not the thing with intelligence, the programmer is supposed to have that. If the programmer writes code that doesn't do what they meant it to do, the interpreter isn't going to question that, it's just going to run the code. The programmer is the one who is supposed to know what they're doing.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...