• Bittle Tobby Lables (unregistered)

    Frist! Ah-ah ... saviour of the WTF!

  • (nodebb)

    Better: public static function u(f:uint, m:uint, v:Boolean):uint { if (v) { if ((f & m) == m) return f; f|= m; } else { if ((f & m) == 0) return f; f&= ~m; } return f }

  • anon (unregistered)

    Actually, flags & flagMask == flagMask can be true for flags != flagMask if flags is a superset of flagMask.

  • (nodebb) in reply to anon

    Came to write the same, beat me to it.

  • NotAThingThatHappens (unregistered)

    Anyone else .... about that the most common comment on this site is

    'Comment held for moderation.'

    ???

  • Chatogaster (unregistered)

    The optimizations don't make sense (the code was written well after the time when writing to memory/a register was still something to be avoided), the comments are utterly senile (but correct), and the function should not exist in the first place (for the same reasons that "function addOrSubtract(int a,int b,bool c)" should never see the light of day).

    But in the end, it's just a function that takes some flags, a mask and a boolean, and then returns some flags. Its naming is the least of its issues. It could have been named modify, upd, setOrUnset and it'd still be clear from the signature what it's extremely likely doing.

  • AnotherNonSignedIn (unregistered)

    Firstly, rather than have multiple returns, invert the tests and simply fall out of the if-then-else.

    Secondly, the tests are likely to be almost as expensive as the update so the updates should probably be done unconditionally. If profiling suggests that it's worth the effort to improve that, a temporary can be generated with the updated value and only store the result if it's different to the original value. That would be subject to performance testing with the expected tool chain and target.

  • (nodebb)

    TBH in the mid-2000s (and even early 2010s) I was actually hoping that Flash could become a web standard for business apps (I wasn't aware of Flex back then). The reason, of course, was simple - the browser landscape was still a very wild west, where much of the things Flash did in a standardized way were done differently in every browser, if they could be done at all. Advanced GUIs were a PITA to make. It's a great relief that the browsers have now gotten their act together and have obsoleted Flash.

  • (nodebb)

    Not much of a WTF. Sure, the guard conditions are kinda useless (they're probably as expensive as the mutation they're avoiding), and the naming isn't great, but it's easy to understand what the code does (updateflags by setting or clearing the bits specified by flagMask), and the implementation seems correct.

  • (nodebb) in reply to Vilx

    The reason, of course, was simple - the browser landscape was still a very wild west, where much of the things Flash did in a standardized way were done differently in every browser, if they could be done at all.

    Hmmm... seems like you were looking for a "single runtime that had behaved consistently across platforms".... like maybe Java. The problem with Flash was that it was a client-side application running in a client-side runtime, but it was marketed as a web technology. If you wanted all the the things that Flash (or Java) offer, simply write traditional applications instead of web applications.

    Flash got you none of what the web was supposed to get. Writing for the web got you the ability for your product to be used by anyone with a web browser. The ubiquity of Flash made it seem like it was the web, but Apple's refusal showed how thin the argument really was. Additionally, all of the runtimes were made by the same organization and they did a really horrible job maintaining and securing the runtime.

  • Duston (unregistered)

    " things Flash did in a standardized way were done differently in every browser" That's true, each browser had its own ways of being compromised.

  • (nodebb) in reply to Jaime

    All true. But at one point, Flash really was ubiquitous across all desktop browsers, and mobile browsing was still in its infancy so nobody cared much for it. The arguments against it were clear (single vendor, crappy support), but for a while it seemed to be the only real chance for advanced web apps. At least to me. :) I'm happy to say I was wrong.

  • (nodebb)

    Interesting thing that no one picked up on.

    The flow of the function:

    if (boolValue) {
       blah; return value;
    } else {
       blah; return value;
    }
    return value;
    

    There's a return statement that will never be used!

  • (nodebb) in reply to Vilx

    Even when it seemed like it was the only game for "advanced apps", it completely broke how web browsing was designed. You can't bookmark into a Flash app; the back button is unusable; the browser history can't record your progress inside the Flash app. I'm reiterating what Jaime said, but a Flash app is a desktop app running inside a browser, not a browser app.

  • Blr (unregistered) in reply to miquelfire

    None of the if/else blocks has a guaranteed return statement

  • AnotherNonSignedIn (unregistered) in reply to miquelfire

    The code isn't the same as your shortened version. The first two returns are used if the flag(s) don't need to be changed (for each state of value); the third is used if the flag(s) are changed. All three returns are used.

    I suggested above that it could, and perhaps should, be reduced to just one return.

  • (nodebb) in reply to Ross_Presser

    I think I should have clarified better. The use case that I had in mind actually was "desktop apps in a browser". These are your intranet apps that are used to run your business by your employees, but not open for the public. Even back then a web solution was preferred for new apps because you could deploy them more easily than desktop apps. And people used them like desktop apps too - web specific features (history, bookmarks, etc) were rarely if ever used by anyone.

  • (nodebb)

    The function's name is awful(1), as are the names of its parameters, but its functionality is, in fact, fairly clear once you remove the fluff.

    "For each bit of flagMask that is set, set the corresponding bit of flags to true if value is true, else to false." I'm not sure why that's really useful, but whatever.

    It should be just:

    public static function update(flags:uint, flagMask:uint, value:Boolean):uint
    {
        if (value)
        {
            flags |= flagMask;
        }
        else
        {
            flags &= ~flagMask;
        }
        return flags;
    }
    

    (1) Naming things well is one of the hardest things in programming.

  • Argle (unregistered) in reply to Steve_The_Cynic

    Once you clear away the fluff, maybe it's time to realize it was never necessary in the first place. What if it was this (sorry for the language shift):

    public static int update( int number, bool flag ) { if ( flag ) number++; else number--; return number; }

  • (nodebb) in reply to Steve_The_Cynic

    The problem is that the function is unnecessary to begin with, why not just do "value & mask" and "value & ~mask", as needed?

  • (nodebb)

    @Argle and @Mr. TA Agreed, but the article is written as if it's also incomprehensible what it even does.

    As noted, any call where value is a constant-valued expression should be replaced by the appropriate equivalent (notably, either x |= bits; or x &= ~bits;), but there's a somewhat vague and pretty weak case for a method to do the conditional set rather than copy-pasting, or, worse, rewriting, the same if() everywhere, and hoping it really is the same everywhere...

  • (nodebb) in reply to Vilx

    The use case that I had in mind actually was "desktop apps in a browser". These are your intranet apps that are used to run your business by your employees, but not open for the public. Even back then a web solution was preferred for new apps because you could deploy them more easily than desktop apps.

    ClickOnce. Or one of a thousand other "click here and it will install (or update) and run a desktop app even if you aren't an administrator" solutions. I also wrote an auto-updater that would update and launch a desktop application. It worked just fine for non-administrators.

    Flash was simply an embrace-extend-extinguish attempt by Adobe, just like ActiveX and Silverlight were from Microsoft. You, and many others, fell for it and paid the price when the carpet was pulled.

  • Giulio (unregistered) in reply to Argle

    If this is a common pattern in the application, and value is an actual variable (meaning you really don't know its value at compile-time), then I could see the sense of such a function.

  • (nodebb) in reply to Jaime

    Ha, I just thought it would be neat if Flash got to business apps, but I never actually got to touch the thing. I never paid anything. :)

  • Chris (unregistered)

    I can see the value in a method like this. Would be useful for things like checkboxes the set and unset options, which are stored and passed around as a single value representing a bitmask of flags. Instead of hoping the developer is using the correct logic each time, they simply call this method (which should have been named better - maybe setFlags?). Even Mr. TA above got the logic wrong - it should be "value | mask", not "value & mask", showing it's an easy mistake to make when writing something out quickly. This way you get one point of failure that is easily tested, and code that is probably a bit easier to read and maintain (again, with a better name).

    Very doubtful that those checks saved anything. Smells like pre-optimisation to me.

  • Ann on a Mouse (unregistered) in reply to Jaime

    ClickOnce is a Windows-only thing, so it wouldn’t actually get you the same write-once-run-anywhere state as Flash. (Mind you, I’m not advocating for Flash. Just saying that if your goal was “this business app will run on your machine, period” then ClickOnce isn’t a solution at all.)

    And no, Flash, was NOT “an embrace-extend-extinguish attempt by Adobe”. It was a multimedia system (originally basically for cross-platform educational CD-ROMs!) from a company called Macromedia. When the web took off in the late 90s, making a browser plugin so that the multimedia data could come from an Internet source instead of a local file was an obvious step to take. Adobe eventually bought Macromedia not because of Flash but because of Dreamweaver and ColdFusion — Adobe wanted to have a lock on the DESIGN market, not the web — and it took a long, long time for them to even consider Flash seriously and start allocating programmers to work on it, which is why it developed so many security holes.

  • (nodebb) in reply to Mr. TA

    Syntactic sugar, possibly, and/or making it harder to screw up one of the instances by typoing the punctuation. Of course, proper syntactic sugar should also come with a less generic function name.

  • xtal256 (unregistered) in reply to Ross_Presser

    "You can't bookmark into a Flash app; the back button is unusable"

    Gee, just like many of the more modern web apps. Ok maybe it was worse 5-10 years ago, but even The Daily WTF forums don't handle it very well and spam my history with an entry for each post as I scroll down the page (though I think not all browsers do that, but again so much for consistency!).

  • JustSomePersonWhoStoppedBy (unregistered)

    Sorry, what are we actually complaining about here? The logic is pretty clear, and the code is not hard to read, understand and mentally validate. (As Steve_The_Cynic correctly points out you can skip the secondary IFs and return the direct result of bit twiddling).

    So that just leaves a badly named function (fully agree to that, though by far-and-away not the worst I have seen). Is that really worth writing a WTF for?

  • JustSomePersonWhoStoppedBy (unregistered)

    It is a pity the git repo now returns a 404, otherwise I would love to have checked if Steve put in a PR to correct the naming to be more descriptive ... because to my mind, if he hadn't, THAT would be the REAL WTF.

  • (nodebb) in reply to JustSomePersonWhoStoppedBy
    1. The git repo is at https://github.com/apache/flex-sdk

    2. The wording "And it's from this era that today's sample from Steven comes." suggested strongly that it may well have changed since then, which is why I didn't bother with it.

    3. I should have bothered, because of point 1.

    4. I installed command-line git for Windows from git-scm.org, and cloned the repo.

    5. git grep "\.update(" | wc -l shows 235 calls to this function. Looking at the code in frameworks/projects/framework/src/mx/utils/BitFlagUtil.as, we see that the function is, in fact, well documented, but the superfluous "optimisations" are still there.

    6. It is always called with only one flag in flagMask, and in nine cases out of ten, maybe more, it is called with a constant, and in the others, it is called with value != null. The constant is never false.

    7. The 235 calls thing makes me suspect the maintainters would ... resist, let's say, a PR to change it, not least because the last commit in the repo was on 31 December 2017.

  • code archeologist (unregistered) in reply to Steve_The_Cynic
    Comment held for moderation.
  • markm (unregistered)

    It would be clearer if the function was named "updateFlags". It really needs a comment documenting what the function does, e.g., "For each 1-bit in flagMask, set the bit = value." But poor naming and lack of documentation are too common to be much of a WTF. The superfluous secondary if's are worthy of notice, but it seems like more programmers than not are incapable of minimizing a logic tree. (This is assuming that the language isn't odd enough to surprise me.)

    I'd expect that 99% of calls to this function are with constant flagMask and value, so flags |= flagMask or flags &= flagMask are clearer and much more efficient than a function call. But according to Steve, there are a few calls with variable parameters, and if you're going to write a function to handle these cases, it's simpler to call it for all cases.

  • KUSHAL (unregistered)

    thank you for giving this information https://shantinursingservices.com/gallery.php

  • ankit (unregistered)
    Comment held for moderation.
  • (nodebb) in reply to Steve_The_Cynic

    Thank you Steve-With-Too-Much-Time-On-His-Hands.

  • ankit (unregistered)
    Comment held for moderation.
  • Jan (unregistered) in reply to yaytay
    Comment held for moderation.

Leave a comment on “Flexing Bits”

Log In or post as a guest

Replying to comment #:

« Return to Article