dcsimg
A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: 10 questions

  1. #1
    Member
    Join Date
    Sep 2009
    Posts
    88

    10 questions

    I'm trying to relearn to program well (reading coding conventions/standards etc) but I have a few questions to which I couldn't find answers.

    1- Is it possible to check for conditions with switchcase? I don't like the way using if/elseif looks for code clarity but couldn't find information about using anything more complex than a simple number or string for a case of switchcase.

    2- For a function inside a class that uses the class properties, would it be a good or bad practice to still send the properties needed to that function via parameters?

    3- When you have mildly complicated mathematics formulas that takes a lot of space on 1 line (100+ char space), should you cut it down to more than 1 line? On the same subject, is there such thing as too many parenthesis? in my case there is this )))); at the end of that line with the formula.

    4- I've read I should definitely avoid using flash IDE (cs3-4-5 etc) for coding. Has someone actually played with both one of those IDE and flex or flashdevelop and could elaborate on this?

    5- I don't remember having to be connected to internet to use a TFL textfield (an error is generated and makes me unable to compile if not connected). What's up with that?

    6- If I use function2 function3 and function4 inside function1, that are only used for function1, should I declare them inside function1 or inside the class right below function1?

    7- I want to change the text color of different text lines inside the same textfield (a bit same way as items textfields have in games like diablo when you mouse over an item), is the best way to do this via a bunch of different textformats?

    8- Through my reading, I seen a few things speaking about ways to improve performance, but none of them gives any real number on how it affects that performance. For example using Vector instead of Array. By how much simple changes like that can help performance and memory use?

    9- When I use the underscore to name my private properties, some names turn blue, why does that happens? As far as I know there is no reserved keywords starting with an underscore and adobe as3 reference livedocs won't reveal any either.

    10- Another question about good coding practice, if I have a multiple variable with the same name (and add numbers to the end of the names), should the first one always use the number 1 at the end? Like: name1, name2, name3, etc vs name, name2, name3?

    Thanks to anyone who can take the time to answer a few or all.

  2. #2
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Good questions! Here's my two cents.

    1 - Sort of. You can get away with logic in your switch statements but in my experience it's a pretty bad idea. The code is checking that what's in the switch is equal to what's in the case - whenever those equate to true it's going to fire - it can work for the case below but doing comparisons between different parameters or complex logic is a fast ticket to bug town.

    PHP Code:
    var i:int 11;

    switch(
    true){
        case 
    i==10:
            
    trace('10');
            break;
        case 
    i==11:
            
    trace('11');
            break;
        case 
    i==12:
            
    trace('12');
            break;

    2 - Depends on if it's public or private. If it's a private function, it can use other resources inside the class. If it's public - you should pass in params (unless they are always going to be inside the class - in which case, don't pass anything, just reference the private members).

    3 - Good rule of thumb is to save variables as you go so you can avoid that - it makes it easier to see what's going on later. When you get to optimization, comment out the extra vars and make one uber-line that computes fast but leave the readable version for debugging later.

    PHP Code:
    //var slope:Number = 5;
    //var xIntercept:Number = 0;
    //var x:Number = 11;
    //var y:Number = slope * x + xIntercept;
    var y:Number = (11) + 0;    //  faster but also very hard to tell what the intent is 
    4 - Both have their place. The debugger and code sense in FlashBuilder makes it indispensible when writing large amounts of code or debugging complex projects. On the other hand, Flash lets you write code into a lot of obscure parts of a swf (movieclip frames for example) that is hard to do in FlashBuilder. It's also extremely fast and easy to open a new FLA and try out a little block of code to make sure it does what you want - setting up a test harness in FlashBuilder is a new class with full supporting project structure - if you're just testing something small (see question 1) it's a lot faster to do it in Flash.

    5 - TLF is a Runtime Shared Library; it's a precompiled block of code that you can download and cache and use in many swfs (same as the Flex Component Framework). Here's a lot of detail on the subject.

    6 - Either will work technically. It's important to remember that weird things happen with scope when you do that - the inner functions will have private access to variables outside themselves but inside the function that declared them. Consider this:

    PHP Code:
    function load(resource:URLRequest):void{
        var 
    l:Loader = new Loader();

        
    l.addEventListener(Event.COMPLETE, function(e:Event):void{
            
    //  this inner function knows about the resource and l vars...
            
    trace('loaded ' resource.url);
            
            
    //  if you don't remove the event handler - this function is
            //  stuck in memory forever - as well as all the junk outside
            //  in the parent function!!
            
    l.removeEventListener(Event.COMPLETEarguments.callee);
        });
        
        
    l.load(resource);

    7 - In a dynamic TextField you are limited to a default color and one area of custom color. You can get past that using htmlText and defining custom styles in css for the TextField. I think you can do this all natively now with a TLF textfield but I haven't played with that enough to say for sure.

    8 - Performance falls into two categories - architectural and micro-optimization. Micro optimizations are things that get you milliseconds of benefit or less (ex. ++i is a little bit faster than i++). Those types of changes are only valuable when you're running huge loops or render code where you can stack that benefit tens of thousands of times into a noticeable increase.

    Architectural optimization is building your code to avoid slowdowns in the firstplace. Things like using cacheAsBitmap intelligently to avoid re-rendering assets every frame; or saving records into a Dictionary so you can access things in one lookup instead of crawling an array looking for a match; or using an object pool to hold things in memory so you don't incur the cost of making and destroying new objects all the time. On a game I worked on an engineer figured out that unpacking .gif files was pretty slow so we switched over to png-8 which Flash can handle much more quickly. We saw our average load time shrink by about 5 seconds even though the overall download went up.

    9 - You may be getting crossover with AS2 syntax - most properties started with underscores to differentiate them from functions. Also - code coloring is just weird sometimes.

    10 - This is personal preference; I'd just stuff those variables into a vector or array. If you're going to loop over these, start with 0 so your iterator doesn't have to be offset.
    Please use [php] or [code] tags, and mark your threads resolved 8)

  3. #3
    Total Universe Mod jAQUAN's Avatar
    Join Date
    Jul 2000
    Location
    Honolulu
    Posts
    2,429
    I'd say neznein captured my sentiment as well.
    On mac, I prefer FDT, it's just smarter and gets updates (android publishing etc) faster, on PC, flashdevelop. Flash IDE is still the best for quick tests.
    As far as nested functions, on top of what Neznein says, a project is likely to change requirements on you and you'll likely need to reuse some of those inner functions elsewhere. Its less of a headache to just separate them and if there's time and a need at the end of the project, you can consider condensing, though you probably wont see a performance increase.

    A few things (as in the date class) start at 1, but for the most part, start at 0 and change later if the need arises. If you have a list of similarly named objects, I can almost guarantee you'll be looping over them eventually.

  4. #4
    Member
    Join Date
    Sep 2009
    Posts
    88
    Thanks for the answers, I'd like to go deeper into understanding some of them.

    1- I went ahead to read more about it after your answer, found some tests that shown it makes the code a bit slower (~20%) when using switch instead of if/else but why do you say it is a bad idea and how does it brings more bugs than if/else with complex logic?

    2- ok

    3- I don't usually use much numbers into math formulas, hence the reason for sometimes getting a long line of code (cause of variable names). Just to make sure if I read correctly, you suggest I insert the value of the variable instead of variable itself to make it smaller (and adding in comment each variable above)?

    4- Where do people usually draw the line for a complex project and use something else than CS4-5? I ask because I find myself often giving up on my game projects (usually because simple stuff won't work and I have to spend 4-5h researching on each little issue I come across, gives huge headaches)

    5- Ya I've read about it a bit, I try to keep stuff simple, and wanting to use a TF forcing me to be online is not simple. Any alternatives?

    6- A bit confused, never knew you could declare the function called by an even listener at the same line you actually add the event listener, my head hurt haha. So anyway rule of thumb, should I declare functions outside another function is they are used only by that function calling them or inside?

    7- Never tried HTML texts yet, would you say it's worth it to learn and use them or should I just use a bunch of different text formats applied alternatively to change each line's color in the same TF?

    8- ok

    9- Does it do that no matter where you code? (IDE, flashdev, flex)

    10- ok

    Thanks again for your time.

  5. #5
    Total Universe Mod jAQUAN's Avatar
    Join Date
    Jul 2000
    Location
    Honolulu
    Posts
    2,429
    1. switch statements only check strict equality, if statements allow you to check over any number of any type of condition. You have to be very careful to include break statements and default conditions in switch statements. Switch statements have their place when dealing with the same type in similar contexts like strings for days of the week. If you find yourself dealing with an unruly stack of if() statements dealing with all kinds of types and conditions, its time to consider a refactor.

    3. It's a judgement call depending on how confusing the equation would be on its own. While you may not be worried about other developers reading it, you will probably have to 6 months later and if you didn't leave a clear comment, you'll be re-working the logic. If a number is not known at author time but will stay the same at runtime, its better to calculate it once and record it to a variable rather than the needless extra math every time its needed.

    4. At the beginning. Personally, there's no logical reason to produce anything in the Flash IDE. Its the best way to create a .swc full of graphics and fonts you'll need to code with but after that I won't touch it. Robust text editors make life easier. period.

    5. Good old TextField and TextFormats are still acceptable (I use that word loosely) for most cases. Don't squash grapes with an anvil.

    6. In rare cases such as Array.forEach() that's ok. As long as nothing outside of the parent function references the local function such as listener lists or callbacks.

    7. For simple textfields like headings, button labels, tool tips etc. its not worth the trouble of styling html. For body text with mixed formatting, flashes html/css is quite worth the effort though frustratingly limited.

    9. No, only the Flash IDE confuses legacy code hints.

  6. #6
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    I drafted a quick example of what scares me about using switches with conditionals:

    PHP Code:
    var a:String 'foo';
    var 
    b:String 'bar';

    switch(
    true){
        case (
    == 'foo'):
            
    trace("FOO");
            break;
        case (
    == 'bar'):
            
    trace("BAR");
            break;
    }

    //  traces FOO - but not BAR 
    This first example works but it only takes the first match and then breaks.

    PHP Code:
    var a:String 'foo';
    var 
    b:String 'blahblahblah';

    switch(
    true){
        case (
    == 'foo'):
            
    trace("FOO");
    //        break;
        
    case (== 'bar'):
            
    trace("BAR");
            break;
    }

    //  traces FOO BAR - should not trace BAR!! 
    This one is worse - if true, the first case just falls through into the next case without checking the second condition.

    Neither of these are that surprising but I am firmly against anything with the potential to cause bugs in unrelated code ("B is true - why isn't it firing?!" or "B is false - how is this code still running??").
    Please use [php] or [code] tags, and mark your threads resolved 8)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center