Syntax

Apr 14 2011 Published by under ActionScript, Components, Flash, iPhone

Doing some Android dev these days. Needed to check if a editable text field was empty or not. I had to dig around to find out how to do this. It turns out that an EditText’s getText() method returns an instance of Editable. You need to convert this to a string and then call equals(“”) on it. So you get this:

if(myText.getText().toString().equals("")) {...}

The similar thing in Objective-C would be:

if([[myText text] isEqualToString:@""]) {...}

And in ActionScript:

if(myText.text == "") {...}

I’m going to hold my tongue here, and let you make your own judgments. But as a person who created MinimalComponents, I bet you can guess where my preference lies. 😉

37 responses so far. Comments will be closed after post is one year old.

  • Matt says:

    Wow – Objective-C is nasty

  • Eric E. Dolecki says:

    I know which is quicker to type for sure. I usually use this for Obj-C:

    if([[myText text] length] == 0) {…}

  • Rumori says:

    You have a point here, but maybe we could talk about enums too, just to be fair 🙂

  • Pradeek says:

    The Javascript version is almost the same as the AS3 one, except you need to add another = as in : if(myText.text === “”) {…}

  • But what will happen internally in actionscript?

  • anon says:

    That doesn’t even get into the fact that every single numerical type has its own Class-level toString function to convert prims to string because you can’t just cast.

    Integer.toString(100);
    Float.toString(100.0f);
    Short.toString(100);
    Character.toString(100);

    vs.
    String(100);

  • Zach says:

    That has been my experience too moving to java. there’s a lot more boilerplate. You have to write a lot more code to do the same thing. I miss my display list.

    But the real gotcha is multi-threading. (which I hear we’ll be getting in actionscript soon)

    Still its pretty easy to switch between java and actionscript – they are as alike as they could be in most ways. I’ve been doing Actionscript since AS1 and java for a few months.

    Kieth: what IDE are you using for Java? Eclipse? IntelliJ?

  • keith says:

    Yeah, again, not making any judgment calls, just interesting to note the differences. It’s funny to have chain together three method calls to do something so simple. My overriding principle in making apis / components is to make the things that you do a lot as simple as humanly possible. I get the feeling in Java that everything likes to be encapsulated in several layers of abstraction. Like Flex, the added complexity probably pays off in very complex apps, but tends to make simple apps far more complex than they need to be.

  • Cricket says:

    That’s why I write everything in Flash first. Actionscript is like pseudo-code that actually runs…

  • Certainly being able to do something like:

    if(myText.text == “”) {…}

    would be cool, however by simply looking at documentation for UITextField on iOS, it is easy to realize that “.text” is not simply a buffer of chars like in Flash, but an instance of NSString, so comparing it using the equality operator ‘==’ would be comparing a pointer to a constant string, that’s why it fails.

    I’ll skip why having a real object in place rather than a char buffer has more advantages (think about conversion, escaping etc that all come with NSString), however by using the dot syntax, which has been available for a while in Objective-C, you may use the less “ugly”:

    if ([myText.text isEqualToString:@””]) {….}

    • keith says:

      I wasn’t looking for an explanation. I’ve been working with objective-c for 2.5 years and know what an NSString is. And I’ve already mentioned that I understand there are benefits to having an object. You don’t need to defend what is not being attacked.

    • keith says:

      Sorry if that last one came off as kind of harsh. It’s just that I’m not some noob saying, “gosh that objective-c stuff sure looks funny. Why can’t it be more like flash? ”

      I just found it an interesting comparison.

      • No problem!

        That’s why I started with “Would be cool if it was possible to do the same” and the explanation was for the benefit of the ones that responded by judging a syntax without knowing what’s behind it.

        • keith says:

          Yeah, I overreacted. I guess i’ve gotten touchy about people starting technology wars every time any kind of comparison is made. See my Kindle and ipad post from last August. All out flame war. And when I started comparing JavaScript and flash, some got livid about that. I embrace all technologies, enjoy learning them and take each one on its own basis.

  • 2nd take,

    you can actually even use this:

    if (!myText.text.length) {…..}

    • keith says:

      You could. I’m really against that syntax though. I find it unclear in any language. I would always be explicit and check length == 0.

      there are always tricks to make your code shorter. I was comparing the “standard” syntax.

  • Orion says:

    So if someone is more comfortable in AS, why not develop it in Flash and use AIR to deploy it for the latest Android, Blackberry, and iOS devices?

    As a Flash developer, who is not up on Java or Objective C, I would actually like to know the answer to this question.

    • Eric Dolecki says:

      It’s always better to develop native so you are in control. Going through the magic translation sauce means something else if making it work… You don’t know how and maybe you don’t care, but it will probably never be as good as developing natively. If you don’t want to use the latest and greatest Android or iOS SDK features, do the Flash translation thing. I just don’t prefer to do it that way unless it’s something quite simple or needed ASAP.

      • keith says:

        Crossplatform solutions may be good for simple projects, rapid development, and one-offs where you don’t want to make the investment of learning a new language. But as Eric said, you lose a lot in the process.

        • Orion says:

          Thanks guys. This is more or less the response I expected. My time is always very limited, and my projects fairly simple, so I think I’ll continue with the Flash/cross-platform plan.

          I suppose if I ever did run into noticeable performance issues with a particular deployment and I had a client/employer willing to invest enough in the project, I could do a proper translation to the native language/SDK. At this point I have no pressing need to get my head around Objective-C syntax and the iOS SDK.

          …and if I hold out long enough, I’m sure Adobe will start translating my projects into HTML5/Canvas/JavaScript for me too.

          Man, I sound like a lazy Flash guy don’t I?… 😛

  • JLM says:

    Orion

    Look at nme-2 and Jeash they are haXe libraries aimed at remapping flash native libraries built on a language designed to compile to many targets.

    Keith

    By the way Keith your SWFSheets seems very useful, I tried it out with Jeash, and I have to wonder about your statement about Crossplatform solutions, I think stuff designed to be cross platform from the start and built on sound theories is not in the same category as strap-on technologies.

    The haXe code for your examples would maybe be the same for all three targets and look the same is the actionscript. Although have to wait until the haXe meet in May to see if haXe java performs on a par with native Java, early signs look promising.

    • Orion says:

      Thanks JLM, I will look into haXe when I have a bit more time.

      I can certainly appreciate the philosophy behind it. I love to create interactive experiences, but don’t have the luxury of time or resources to put into learning every language for every platform.

      It’s the same argument that Flash haters have been levelling at us for the last dozen years or so: Why should I have to learn an idiosyncratic language for a proprietary platform subject to the whims of a giant corporation? (This is why I have no interest in learning Objective-C.)

      Web standards are theoretically the solution in the web world, but it’s no coincidence that every major player tends to back their own browser or pluggin. The browser wars are alive and well today, and Flash, despite the haters, looks like a safe, stable platform again — that just so happens to have all the best in interactive, rich media features with a growing list of cross-platform deployment options.

      • Of course the fact that this post was only about differences in syntax to check strings and not about loving/hating or which platform is the best to use, is completely irrelevant to you.

        You don’t need to learn every tool for every platform, but ignoring the leading platforms that clients ask application for is not wise move.

        Certainly if the only thing you know is Flash you’re not going to go too far, unless this is just a hobby for you.

        If it is business and you can’t afford the “luxury” of learning new things you’re either doing the wrong job and most likely in the wrong way.

        • Orion says:

          You might find this hard to believe Emanuelle, but many of us are enjoying fairly lucrative and rewarding careers doing primarily Flash design and development.

          The fact that there are new and exciting platforms emerging has me wondering if I should learn other languages. So far no one is demanding that I develop anything for iOS. So for now I’m going to keep working on what I’m being paid for and wait and see what technologies become more important and what opportunities arrise from them. This is why I follow such excellent blogs as bit-101 — kudos to Keith for his informative posts on these topics.

          I’m just being honest in expressing my interest in cross-platform deployment. My life is very busy but I love creating interactive, rich media experiences, and the more platforms and devices I can reach, in the most consistent ways, with the fewest development tools the better. Sorry if that somehow offended your sensibilities…

  • Happy to know you and others have a rewarding career with Flash.

    Have you reflected on the fact that nobody asks you anything to be developed on iOS because you only know Flash?

    • Orion says:

      I’m certainly not marketing myself as an iOS developer, but I think you’ve missed my point… and your tone is pretty insulting.

      • No, I believe I got your point. You were asking for reasons to learn new things and I gave my point, if you feel insulted because I basically said that people that know more than Flash (or just one single technology) have more opportunities, that’s just your problem.

        Since you seems to be interested in this blog, try to understand why the writer, who has been a Flash developer for long time, is always interested in learning new things and don’t consider it a luxury.

        Being open to new things open more opportunities, it’s a fact.

  • Yuku says:

    Hi, I’m an Android developer. In fact, doing a check whether an EditText is empty, can be done very easily:

    if(myText.length() == 0) {…}

  • sawebdesigns says:

    I tried and works fine thanks

  • Shawn says:

    Learning new languages is awesome, but it’s just not a good ivestment of my time personally.

    Either I can spend months learning something new, and build a cobbled together, gross ugly project. Or I can focus on a Flash project, top notch quality targetting iOS & Android together.

    I don’t think you lose that much really. You lose some performance, you loose the native component set (big deal), you also lose the ability for low level API’s.

    At the same time you gain the richness of flash, rapid development, smooth/easy animation, scalable vector art, timeline animations etc.

    Most importantly you gain the ability to have a single project. One project to debug, one project to maintain, one project to upgrade. This brings your efficiency level through the roof compared to maintaining 3 projects, in 3 languages, with 3 different bug sets.

    I have a number of cross platform apps out right now, they’re making quite a bit of money, and using Flash I can just bang these projects out in a few weeks, targeting 3 platforms at once, and move on to the next project. It really can’t be beat.

    I love learning new languages too. But there’s something to be said for profitability, and for working in a space where you are a master of the language, and can quickly create whatever you want to create, without the minutia/frustration/tediousness of learning platform specific syntax…

    • Eric E. Dolecki says:

      If that works for you, that’s great. I am of a differing opinion, I like to expand my toolbox, but that’s just me.

      • Shawn says:

        I like to expand my toolbox too, when it makes sense. I’ve written some apps in Java, I’ve recently done a massive game in HTML5 Canvas, I’m excited for a chance to do some XNA/C# work. I have nothing against “expanding my toolset”;

        But I’m also a pragmatist, with a family, a life, and ends to meet.

        So what I don’t want to do is spend 500 hrs on something, that I can do in 75.

        I want to focus on building what I want to build, not learning new languages, and maintaining 3 different code bases.

        I also don’t have much interest at all in learning something like Objective C, which is basically proprietary language only used by Apple, that I will never use anywhere else, ever again. To me, that has no appeal whatsoever.

        • Last time I checked, ActionScript is also used only by Adobe Flash, so the argument of “proprietary language” is moot in your case.

          Most of the cross platform application that you see native are written in C/C++ with only the interface to the system written in native language (Objective-C for iOS and Java for Android); this native code accounts for a tiny percentage compared over the rest of cross-platform code. That’s how games are many other kind of applications are written, but probably you are not comfortable learning even languages like C and C++, but unless you’re 75, basking in the spotlight of Flash only is kind of dangerous, technologies come and go, all the time.

          Whenever I tried to use Flash for mobile development, I always spent more time trying to achieve, at least in part, the performance of native code, with a lot more efforts spent in replicating the UI the native thing developers enjoy for free, hence, what’s points of using Flash on mobile? This does apply to both iOS and Android by the way.

          Even yourself list missing things like “You lose some performance, you loose the native component set (big deal), you also lose the ability for low level API’s.”, but apparently a bad performing, alien looking application and having to jump thru hoops for anything not included in the Flash box, seems to be not a problem for you. You forgot to mention the extra luggage of several megabytes of Flash runtime attached to every application, which is a huge disadvantage.

          One question: basically you say “I do things with the right tool only when it makes sense”, so why you choose to develop in HTML5 Canvas a “massive online game” when it would certainly made sense to use Flash, as every browser has it share of problems with HTML5, instead you choose Flash for mobile, a choice that really makes the least sense according to the vast majority of native developers.

          Because that’s the real argument. If Flash was really such a magical box that could shrink development time from 500 hours to only 75 why so little number of mobile apps are written with it?

          Probably I’m not pragmatic like you, but developing is not a costly hobby for me, I make living out of it, and Flash on mobile is really not that working solution you think it is, irrespective of the mobile platform we are talking about.

  • Chockyegg says:

    Every language is just a binding right, I mean the only real difference between PCs is the CPU – x86, Z80 or whatever, and these were long ago abstracted by C, upon which the other languages sit. So whatever you write, it ends up interpreted as C. It’s interesting how projects like Unity offer 3 different languages/bindings to the same kernel. Maybe in the future everything will be ported & portable by design, so you can use whatever language is currently fashionable – just look at Coffeescript, for people bored with Javascript!