- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Frist! Ah-ah ... saviour of the WTF!
Admin
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 }
Admin
Actually, flags & flagMask == flagMask can be true for flags != flagMask if flags is a superset of flagMask.
Admin
Came to write the same, beat me to it.
Admin
Anyone else .... about that the most common comment on this site is
'Comment held for moderation.'
???
Admin
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.
Admin
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.
Admin
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.
Admin
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 (update
flags
by setting or clearing the bits specified byflagMask
), and the implementation seems correct.Admin
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.
Admin
" 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.
Admin
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.
Admin
Interesting thing that no one picked up on.
The flow of the function:
There's a return statement that will never be used!
Admin
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.
Admin
None of the if/else blocks has a guaranteed return statement
Admin
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.
Admin
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.
Admin
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 offlags
to true ifvalue
is true, else to false." I'm not sure why that's really useful, but whatever.It should be just:
(1) Naming things well is one of the hardest things in programming.
Admin
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; }
Admin
The problem is that the function is unnecessary to begin with, why not just do "value & mask" and "value & ~mask", as needed?
Admin
@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, eitherx |= bits;
orx &= ~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...Admin
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.
Admin
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.
Admin
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. :)
Admin
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.
Admin
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.
Admin
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.
Admin
"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!).
Admin
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?
Admin
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.
Admin
The git repo is at https://github.com/apache/flex-sdk
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.
I should have bothered, because of point 1.
I installed command-line git for Windows from git-scm.org, and cloned the repo.
git grep "\.update(" | wc -l
shows 235 calls to this function. Looking at the code inframeworks/projects/framework/src/mx/utils/BitFlagUtil.as
, we see that the function is, in fact, well documented, but the superfluous "optimisations" are still there.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 withvalue != null
. The constant is neverfalse
.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.
Admin
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.
Admin
thank you for giving this information https://shantinursingservices.com/gallery.php
Admin
Thank you Steve-With-Too-Much-Time-On-His-Hands.