Pages

September 20, 2009

You open, You see :: Curly Braces

Scroll down for the English translation...

מה המצב? איך עובר עליכם החג? בטוב? יופי.
אז ככה, אתם יודעים שמקום העבודה הוא אחת הזירות הטובות ביותר להעלות תהיות ומחלוקות. וכך קורה ששאלות תמימות (או לעיתים לא ממש) הופכות לדיונים סוערים, שאפשר להפיק מהם בעיקר תובנות ותשובות.
הנה משהו שקולגה שלי העלה וחשבתי שיהיה נכון (כמובן, ברשותו האדיבה) לשתף אתכם בו:
בזמנו, כל ביטוי action script בקובץ MXML היה צריך לחיות בתוך סוגריים מסולסלים, אבל מאז עברו מעט מים בנהר והסוגריים המסולסלים הם לא ממש הכרח. מתי לא? מתי שהצפי הוא עבור אובייקט, או מתודה, או אם להיות יותר מדויק כל מה אמור לייצג מחרוזת. סוגריים מסולסלים קיבלו מקום בעיקר עבור Binding, וכולנו יודעים שהן בעצם מורות לקומפיילר לייצר אקסטרה קוד שאמור לנהל את ה- Binding במקןם הנתון.
ובכל זאת, אנחנו עדיין רואים מפתחים משתמשים בסוגריים מסולסלים, כאשר הם למשל מגדירים event handlers, הו-אז נשאלת השאלה הבלתי נמנעת: האם כאשר אנחנו מגדירים event handler בתוך סוגריים מסולסלים אנחנו למעשה כופים על הקומפיילר ליצור אקסטרה קוד עבור Binding לא נחוץ בעליל?
בפשטות? לא.
למטה תוכלו למצא את קטעי הקוד המג'ונרטים (ברוך המוציא generated מן הקומפלייר), פעם אחת עם סוגריים מסולסלים ופעם בלי. כפי שאתם רואים, ההבדל הוא, באופן מפתיע, סוגריים מסולסלים. אז מעתה אתם יכולים להיות רגועים ולהרגיע את הסובבים אתכם – זה בסדר.
שנה טובה.



How’s its going?
So, you know that the working place is one of the best places to raise thoughts, wonderings and debates. And so it happen that innocent questions (or some time not that innocent) become a great argument, which sometimes you can benefit from.
Here is question that my colleague raised and I thought it would be great (with his generous permission) to share with you:
At the time, any ActionSctipt expression on an MXML file would have to be inside curly braces, but since then time has passed and the curly braces are not mandatory. You ask “when?” – Well, whenever the expression is not meant to define a String. Curly braces got the honorable rule of defining Binding (in general), and they instruct the compiler to generate extra code to handle the binding on the spot.
Still, we see developers using the curly braces when they define Event handlers, for instance. Then, one might ask, when you define an event handler inside curly braces, do you still instruct the complier to generate extra code, to no need?
Simply answered? Nop.
If you look, below, you would see the code and the generated code snippets for both using curly braces and not using them. As you can see, the only difference is, well, the curly braces. So from now on you can relax… it’s ok… it’s ok :).

Cheers.


Without curly braces


/**
* @private
**/
public function ___CurlyBraces_Application1_creationComplete(event:mx.events.FlexEvent):void
{
onCreationComplete(event)
}


With curly braces


/**
* @private
**/
public function ___CurlyBraces_Application1_creationComplete(event:mx.events.FlexEvent):void
{
{onCreationComplete(event)}
}

No comments: