- 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
That is the correct use of the word clever
Admin
I would pay good money if this was even close to a WTF in the codebase I inherited. It works and it's intuitive. Now, excuse me while I return to excising the direct calls to the database from the front-end controller . . .
Admin
So this would be a "Well, That's Fine" in your codebase, eh ;-) Also, as the saying goes: "i am truley sorry for your lots"
Admin
It's more smartass than clever...
Admin
I wouldn't say "fine" . . . more like a "Well, that's not worth my time".
Admin
Oh it could be way worse. In Python, for example, you can do something like
print(["no", "yes"][some_boolean_variable])
. ;)Admin
Intuitive?
Reading thru that code without any familiarity, you don't even pick up on the fact show, hide are functions, you just see a conditional on some string literals. It does not even save any keystrokes from a code golf perspective because it adds the square brackets and the quotes which would not be needed if you just put the () on show and hide inside the ternary statement! Its literally more syntax to interpret for the reader and as bonus requires one to be thinking abstractly about how JavaScript does symbol lookup rather than just considering the control flow itself.
Honestly this some of the dumbest clever I have seen in a while, because at least most 'clever' code at least save labor for the person who initial authored it. This was just pointless.
Admin
I wouldn't write it that way myself, but this is hardly a WTF. It's perfectly clear what it's doing.
Admin
So TRWTF is not extracting that nugget of functionality into a separate method (or function, or subroutine, or whatever the term is in that language)?
Admin
Looks perfectly fine to me. If you are proficient in JavaScript then (1) I pity you and (2) this is about as clear a solution as you could hope for.
Not a WTF, unless somehow the platform changes the keys "show" and "hide" to "arglefargle" and "filenotfound." In which case the stringification comes back to bite you. But hardly a likely eventuality.
Admin
I agree, with no familiarity the use of "on" for the name of the boolean in a language that I'm used to seeing handle events would likely have me initially thinking "...on show or hide of what, do what?!"
...That wouldn't be much less of an issue with the ternary, though. But then, my intermittent relationship with Javascript may be unusually strongly associated with this kind of thing, since I consider it an important strength of the language. Not the ability to do this, which I agree is kind of silly, but the characteristics that make it possible to do this.
Admin
Avoiding duplicate code at it's best :-)
Admin
it's the equivalent of this monstrosity in C:
or this really ugly monstrosity in C++
Admin
Instead of all of this, change 'd' and the 'show' method:
d.show(on) -- where show handles the true (show) and false (hide) case.
Or, if you CAN'T do that:
(on ? d.show : d.hide)()
Admin
That's why std::function exist… And I implemented similar fun in C++ for dynamic dispatch and accessing variables. (I need to just use std::any for final version ;) )
Admin
Not sure how I feel about it. It is compact. It does capture that the called object is independent from the condition, while the called function is dependent. It's an uncommon syntax, but it's a legitimate syntax, not like they're eval-ling random stuff or cycling through json.
Admin
I am also made about the stringiness because then my IDE can't navigate to the function definition.
Don't laugh at me! There are dozens of us!
Admin
JS quiz: which of these lines is actually dangerous?
The correct answer is 2, which is safe only if the function is not an unbound method. That's because javascript uses a syntactically bound this: it binds whatever was before a . or () to this.
Admin
or make a setter, which i'm pretty sure you can do in javascript, so it'd look more like
d.visibility = on