- 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
comment.frist = frist ? comments.Length == 0 : frist;
Admin
Surely
DocumentStatusConsts.New
is TRWTF here.DocumentStatusConsts
??? That speaks of many ills in the codebase, and a ternary in place of an else-less if is neither here nor there compared to such a thing.Admin
Seriously though, this isn't that WTFy. It's presumably disabling things that shouldn't be allowed except on a brand new document - there might be a design WTF in that concept, but that's not a code problem. Yes, you could write it with &= or a nested if instead, but is that actually any clearer?
Admin
That said, the worst ternary I ever saw was also a replacement for a else-less if, but far scarier, and can be paraphrased as:
That was the entirety of a statement. It was not an element in a larger expression.
Admin
Yes, this is much clearer:
It expresses what is going on: we want to update
item.Enabled
if it is true.Admin
I wrote something along those lines exactly once, while learning C++ in my first week of lessons. I was immediately told never to do something so silly again
Admin
I'd put the condition in the loop
foreach (var item in documentMenuItem.DropDownItems.Where(i => i.Enabled)) item.Enabled = Document.Status == DocumentStatusConsts.New;
Admin
All of the ternary/if logic should be removed and the line simplified to the following:
item.Enabled = !(!item.Enabled || Document.Status != DocumentStatusConsts.New)
Admin
OK. Explain that one a bit more please.
I'm hoping that you aren't having problems with using constants in general. That is much preferred over scattering literals around.
So something wrong with tracking a document's status?
Admin
Not necessarily - it could be a clumsily-named enumeration.
DocumentStatus.New
doesn't sound so WTF-y, does it?Admin
On the other hand, it could also be a pile of unrelated constants, or not even an enum at all, which is what I think Steve's getting at. That would be a WTF.
Admin
Names for interesting values ("constants") are a good thing, if they are done correctly(1). What's suggestive of bad things in the codebase, though, is the "DocumentStatusConsts" name. Why is it not an enum? What else is in there that isn't an actual status value, meaning that it wouldn't be in the enum? (Example: substatus codes where a particular status has two subcases that are close enough that we consider them identical, but their representations - in another field - are defined in DocumentStatusConsts)
(1) See the endless vituperation surrounding e.g.
or, worse:
Admin
Grossssss!
Admin
This could "make sense" if item.Enabled is a property with attached setter, having side-effects. Still wtf, though.
Admin
Another possibility comes to mind--this used to make sense, then something changed.
Admin
One thing I notice is that developers have been told they should never use a control structure without {}. This is ostensibly because of an SSL bug which apparently was caused by this code:
I think after the game of universal best practices telephone, this has resulted in the idea that ternaries are always safer than single line if expressions. I catch myself doing it sometimes. But if you work in a modern codebase with a code formatter it would be a lot harder to sneak that mistake through code review. Even without a code formatter that duplicated line error couldn't have easily occured if the entire expression was a single line.
Admin
I don't see the problem with #define ZERO 1. That's something you do because you're the kind of bastard who enjoys dividing by zero, blowing raspberries at mathematicians.
Admin
If you're going to be snarky you should at least provide a solution that work to the specs.
Admin
Rewriting it with &= changes the behaviour, as that doesn't do short-circuit evaluation so that Document.Status will be evaluated even if !item.Enabled.
But since presumably Document.Status == DocumentStatusConsts.New is loop-invariant, why not hoist it out of the loop anyway?
Admin
All I remember is that in Algol 60 you actually had to write "if", "then", and "else". and you could do it in an assignment statement. With longer keywords (not ':' and '?') it became tedious and you didn't do it (even though you "could")!!
Admin
Ahem.
if (Document.Status != DocumentStatusConsts.New) { item.enabled = false; }
Admin
item.enabled &= Document.Status != DocumentStatusConsts.New
Admin
I think once again someone's been thinking that a one-liner ternary has better performance than an if-statement written over a couple of lines (4 if you include new lines for braces).
Admin
Not sure if this is a WTF, even though it is bad code.
But your interpretation of what it does isn't right: If the thing is enabled, set it to disabled (or enabled) depending on this other feature. Otherwise leave it disabled.
Admin
That's a bitwise
&
and also tests for the inverse of what you want.Admin
Or apply De Morgan's law to get
item.enabled = !!(!!item.enabled && !(Document.Status != DocumentStatusConsts.New)
Admin
Or apply De Morgan's law to get
item.enabled = !!(!!item.enabled && !(Document.Status != DocumentStatusConsts.New)
Addendum 2020-06-11 07:20: )