CodeSOD: A Tern Off
by Remy Porter from The Daily WTF on (#52Q6Z)
Jim J's co-worker showed him this little snippet in the codebase.
foreach (ToolStripMenuItem item in documentMenuItem.DropDownItems) { item.Enabled = item.Enabled ? Document.Status == DocumentStatusConsts.New : item.Enabled; }
Tracing through the ternary, if the menu item is currently enabled, set it enabled if the document in question is new, otherwise set it to itself (that is to say, disabled).
Or, to put it differently, if it's not enabled, make sure it's not enabled.
My suspicion is that the original developer just really wanted to use a ternary, even if it didn't make much sense.
Jim writes:
[Advertisement] ProGet can centralize your organization's software applications and components to provide uniform access to developers and servers. Check it out!When one of my colleagues showed me his find I suggested him to add this line into the loop: if (!item.Enabled) item.Enabled = false || item.Enabled;
Just to be absolutely sure the item will be disabled.