Jim Lynch Codes
  • Blog
  • About Jim

Writings about one coder's stories & experiences.

Beware of the If-Semicolon Bugs!

12/23/2015

0 Comments

 
This has happened to me enough times before for me to be embarrassed by it, and for that reason I'm writing this blog post about it. If you have an if statement that is always returning true (it feels something like, "GAAAHH, why does my if statement always return true!) then this could be your problem (and solution!). I noticed this mistake again today working in Actionscript 3 while fixing a bug in Head Exploder. Here's a simplified version of the as3 code:
var smallNumber:Number = 1;
var bigNumber:Number = 100;

if (smallNumber > bigNumber)
{
     trace("Uh oh!");
}
The code above is fine, and when run it will not print out the message "Uh Oh!" to the console. However, what if we put a semicolon after the closing parenthesis of the if comparison?
var smallNumber:Number = 1;
var bigNumber:Number = 100;

if (smallNumber > bigNumber);
{
     trace("Uh oh!");
}
Now this code is going to be trouble for the developers writing it! For some reason, the semicolon at the end of the statement forces it be true, and therefore everything in the if statement brackets is run. Yikes! This mistake is especially tricky to debug since it doesn't cause any errors, just unexpected behavior in the application, and nobody likes discovering unexpected behavior! I've tried this in Flash Builder and Intellij. In both IDE's this syntax error is not detected, and I'd be so bold as to say that this will slip by the compiler in any As3 code editor. Also note, this isn't really specific to As3. It happens in Java as well. For example, type this into your favorite Java IDE and watch what happens:
int smallNum = 1;
int bigNum = 100;
                
if (smallNum > bigNum);
{
     System.out.println("Uh Oh!");
}
Sadly, this code prints out "Uh oh!" as well. I'm almost too afraid to think about how many other languages are also vulnerable to this subtle mistake.

Some Help From an Else

It's interesting that (in both languages) if you try to follow up the if statement with an else then the IDE complains (as it should) with a error that prevents you from even running the program. Even an empty else {} will cause the author-time error to appear, but then deleting the else will make the code good-to-go again (even though you'll get the unwanted behavior).

What's Really Going On Here?

At first glance you might think that this is some unfixed issue with programming languages, but of course it's not. Remember, compilers allow for single line body if/for statements without braces! Let's take a look at the problem code again. The compiler thinks that the semicolon after the closing parenthesis is ending the block that occurs if the if conditional returns true. the opening and closing braces are no longer connected to the if statement since it has the single line bracket-less body. The compiler basically ignores these brackets since they are not connected to any if or for statement (or anything else) so the code inside always runs.  

The If-Semicolon bug is not a bug in the language itself. It is a type of bug you may find in your applications that is caused by this programmer mistake of putting a semicolon after an if statement.
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    ​Author

    Picture
    The posts on this site are written and maintained by Jim Lynch. About Jim...
    Picture
    Follow @JimLynchCodes
    Follow @JimLynchCodes

    Categories

    All
    Actionscript 3
    Angular
    AngularJS
    Automated Testing
    AWS Lambda
    Behavior Driven Development
    Blockchain
    Blogging
    Business Building
    C#
    C / C++
    ClojureScript / Clojure
    Coding
    Community Service
    CS Philosophy
    Css / Scss
    Dev Ops
    Firebase
    Fitness
    Flash
    Front End
    Functional Programming
    Git
    Go Lang
    Haskell
    Illustrations
    Investing
    Java
    Javascript
    Lean
    Life
    Linux
    Logic Pro
    Music
    Node.js
    Planning
    Productivity
    Professionalism
    Python
    React
    Redux / Ngrx
    Refactoring
    Reusable Components
    Rust
    Security
    Serverless
    Shell Scripting
    Swift
    Test Driven Development
    Things
    TypeScript
    Useful Sites
    Useful Tools
    Video
    Website Development
    WebStorm
    Writing

    Archives

    March 2023
    August 2021
    February 2021
    January 2021
    October 2020
    September 2020
    May 2020
    April 2020
    February 2020
    January 2020
    December 2019
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015

    RSS Feed

  • Blog
  • About Jim
JimLynchCodes © 2023