« Alright, it's compiler naming time... Announcing FlashBelt conference »

April 28, 2004

What did you learn today?

I can't believe I didn't know this one... Actually, I think I did know it, but I've been coding with a certain style for so long that it influenced my view of syntax requirements.. For example, did you know that you can write code that looks like this...

x = String.

            fromCharCode(123);

trace(x);  // {

... and it works just fine? The above behaves just like x = String.fromCharCode. The extra spaces, line feeds, tabs, etc, are all ignored around the dot operator.

Similarly,

var a:Array =  new Array("1", "2", "3");

trace(a.   join("."));

... traces "1.2.3" to the output window, just as expected.

I don't recall encountering this before in my years of programming. I might've, but haven't thought about it or just glazed over it. I've always omitted any sort of spaces around the . operator (and the -> operator in C++) as a habit (example: Class.method, not Class    .   method). It somewhat surprised me to find out that whitespace like this is ignored.. and not just by ActionScript. Java, C# and C++ all behave similarly. However, ColdFusion will generate a syntax error - 'The variable StructTest. ends with a "." character. You must supply an additional structure key or delete the "." character.'

I'm not recommending that you space your code like I have above, but it's interesting to note that you can if you want to. It's easy to envision situations where having that type of freedom is useful, but I think it would pollute code readability if used liberally.

Learn something new every day....

Comments

  • It can actually be a nice way to break long lines of chained method calls (although my style is to break *before* the . or -> selector rather than after it).

    I've seen code that uses a style something like this as a matter of course (not in AS but I'll use your AS code as an example):

    trace ( a . join ( "." ) ) ;

    I really don't like that - I think both the function call operator and the selector should be "bound" to something without the space so this is more palatable to me:

    trace( a
    .join( "." )
    );

  • I used to break up long SQL statements into several lines, especially for Stored Procedures.

  • Hi,

    I encountered same thing several times in past. But the very first time, i wondered but after testing i found it works. An thought, since . is like an operator it behaves like other operators.

    Regards,
    Abdul

  • All good 'till MM will publish their next flash player (or next-next flash player) which will not support this kind of practice. same thing as the Case Sensetive issue.
    don't ya think?

  • Neat, just hope people don't start thinking it's PHP :p (wish they would switch to + for concatenation! :/)