CodeSOD: A Tern Off

This post was originally published on this site

The Daily WTF

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:

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.

[Advertisement] ProGet can centralize your organization’s software applications and components to provide uniform access

To read the full article click on the 'post' link at the top.