« Flash Reserved Word Listing PHP Easy Templating System »

April 16, 2004

Do you ";"?

In creating an ActionScript compiler, the ECMAScript Language Specific has been the single most valuable source of information for me.

Section 7.9 of the ECMA-262 Edition 3 document outlines how automatic semicolon insertion works in ECMAScript. Because ActionScript is based on this standard, not all of your statements require semicolons to terminate them. I'm sure you'll all aware of this fact by now... Check this out though:

b = 1;
c = 5;
x = {test:function() {
     trace("x.test() called");
   }};

a = b + c
(x).test();

trace("a = " + a); // a = NaN

Alright, so look at the code where we set a... b + c is 6, which looks like it should be the value of a. However, we trace out NaN. Why? The reason is because, according to the standard, a semi colon is not automatically inserted after c. In fact, the statement is actually parsed as..

a = b + c(x).test();

...which doesn't seem quite right based on the white space we specified. To the parser though, the parenthesis on the next line are interpreted as a function call. This explains why a was set to NaN - the number b plus undefined (there is no function c), returns NaN.

Why am I telling you this? Simple... use semicolons! Personally, I think it's a lazy habit to not use them. Using them will increase code clarity as purpose is made clear. Additionally, not using them can get you in trouble. There are times when they are not inserted automatically for you when you might think they should be (as in the example above). Rather than have the compiler think for you, best practice says you should explicitly tell it what you want it to do...

Food for thought... enjoy your weekend everyone!

Comments

  • I second that for the same reason.
    And on another note, always use {}, even if you only have one statement in a block. Just lately i wanted to comment out some debug statements in about 200 files by using a global multifile search&replace. Now imagine what happens, if your code looks like this:

    if( condition) myDebug("condition is true");
    nextStatement();

    And now you replace myDebug by //myDebug

    if( condition) //myDebug("condition is true");
    nextStatement();

    Ups, now "nextStatement()" becomes the block of the if.

    bokel

  • Yes I do ;)
    Not shure what you guys are talking about tough...
    Does it work without {} ?
    I know that you can leave out the ";"
    In AS that iz...

    cosmin

  • another nasty habit of mine ;(

    whenever im writing AS, the semicolon part of my brain just turns off, even if i was writing php half an hour earlier. to be honest, i dont think it reduces clarity, not like the auto-format tool reduces clarity, man is that thing bad.

  • Coming for Python, I have always found ; and {} absolutely useless in code. They simply provide the opportunity for sloppy coding. Python uses Tabs to denote code blocks {} and new lines to denote statements.

    An amazing side effect of this is that Python code is very readable and much easier to maintain. If you take a look at any pyhton code you can tell what it does very quickly compared to other languages.

    Although this a very subjective topic and I am biased.

    TIP: To reformated AS with ;, Do a "SaveAs MX" in MX2004. The IDE will reformat your AS with ; acording to the compiler rules. This makes the above error obvious.

    Cheers, Ted ;)

  • Ted,

    In a language like Python where code structure is important, the ; and {} aren't necessary because they're defined by the structure. In a language like ActionScript, because you can write code any way you want (as far as indentations and such go), semicolons are very important as they denote when the statement ends.

    I come from a C background originally, so I'm used to getting syntax errors when a ";" is omitted.

    How does Python handle block scope? If I have { int a = 7; } in C, a is only available inside of the block, between the braces. If I want the same kind of functionatlity in Python, is it as simple as just indenting one more time? ActionScript doesn't have block scope, but it does have local scope. I'm curious as to how Python handles this.

    In regards to Python readability, it's great that it is enforced on the language level. For the "other" languages, the source code format is one of those unwritten rules. I would argue that any developer who formats code in that manner can write readable code in any language. Python just forces you to... which isn't a bad thing, but my point here is that other languages can have code that is just as readable without it being a requirement.

    On the other hand, sometimes there ARE times when you want to write unreadable code. For insntace, some JavaScript code can be written as one massive line (with ;'s!), which makes it harder for people to look at it. Of course you can run an auto-format on it.. But you might be able to turn the "novice code stealer" away. It's nice to have the option, in my opinion.

    Thanks for the tip, too. Although, I always use ;'s and have never had a problem. ;-)

  • yeah, i remeber this thread [http://chattyfig.figleaf.com/cgi-bin/ezmlm-cgi?1:mss:102218:200401:ndakoailgoiebanmdkeg] where i suggested a semicolon (which solves the problem easily)... of course there's always somebody who (thinks he) knows better... LOL

  • Like Darron, I also came to Actionscript from a C/C++ background. Since I began coding in browser-based Javascript back in the late 90's, I've been accustomed to including ; and {} in all the right places even if the ECMAScript spec declares that they're optional.

    I've also brought over a number of "good coding practices" from previous work in other languages over to my Actionscript code, e.g. explicit variable declaration, typed variables, maintaining case, inline documentation and explicit casts.

    When AS2 came out, everything came together and I took to it like a duck to water and was back up to speed in less than a day and porting my old AS1 code over is a breeze. I do feel rather sorry for those who got "bitten" when trying to migrate to AS2 with poor coding practices, but you do reap what you sow: Darron's got the moral of the story right on.

    I do, however want to learn Python in-depth at some point, just to see how well the enforced syntactical code formatting works with me. So far from what I've heard and small snippets of Python hackery, it works wonderfully though I still feel a bit discombobulated by a lack of C-like syntax.

  • Every seasoned coder has come across sourcefiles with corrupted codeformating. I wonder how python handles this. To me it is a serious design flaw of a language to make invisible characters syntactical relevant.

  • I can't believe any language would have syntactically relevant hidden characters ~ that's appalling if changing the indentation or whitespace blocks could fundamentally change the execution. Is this really true of Python?

  • In Python code blocks are tab indented. This is true for if, for, while, functions, classes and several other features.

    http://www.python.org/doc/current/ref/indentation.html

    The interpreter forces you indent thus making code parsing dramatically faster while improving readability. It is a very alien concept coming from other languages and catches most people off guard when they start using python.

    Before you judge the language, download python and run some code. Here is an HTTP server I wrote in Python as a sample and some helpful links to get started.

    http://www.powersdk.com/download/FCHS.py
    http://www.python.org/
    http://www.python.org/topics/learn/
    http://www.python.org/topics/learn/overview.html
    http://www.vex.net/parnassus/
    http://www.stackless.org/ (tasklets in python - mini-threads)

    BTW, Python is named for Monty Python not the snake.

    cheers,
    ted ;)

  • Error http://www.stackless.com
    Bonus http://www.twistedmatrix.com/products/twisted

  • I once made a bunch of scripts that had mix-match ";" and then went to obsfucate that code..

    Lets just say, OUCH! i paid for it.

    So yeah, I agree with the above and have gotten into the practice of using them not only with declaring variables etc, but when closing of a function

    ie
    function myMonke() {
    // insertCode here..;
    };

  • There is one other reason in Flash to use ;

    If u include files and dont end them with ; but for example with } things can get messy. Ppl suggest to put a white line between and include and the next line, but its the missing ; that can give u headaches.

    Greetz Erik

    ps. After a day of coding, when im typing a mail with () in it (like this) i automaticly put a ; behind the last )

  • Tell us more about this compiler, eh!

  • Interesting on Python - I guess copying and pasting code from the web is a hassle though..? Does it take 4 spaces to equal a tab too? Does this mean you can't wrap long lines? Anyway, interesting idea.

    And yeah, what compiler! : ).

  • Robin,

    Not really. The indention just needs to be even on a block. If the indention is uneven the interpreter will throw an error.

    You can wrap long line or break out of indention altogether with """ (triple string). This allows you to asign a block of text to a string. Take a look at http://www.powersdk.com/download/FCHS.py the "template" variable is defined using triple string.

    Indentation is only strange for 10 minutes, then other languages seem lacking. ;)

    Ted

  • Sounds cool, thanks Ted... I personally find braces annoying, it seems to be the most common thing to argue about when multiple coders work on one project. Kind of silly when you consider they aren't even really needed if you follow any form of code structuring - nobody argues about the indent after all.

    I've always thought programming has to get more visual, and block markers via punctuation should be the first against the wall. It really needs a box around it. And statements can have their own containers as well, as could expressions and classes etc. This cryptic use of punctuation had its reasons when it was introduced, but let's move on people :).