It seems the previous developers of the current project I'm working with decided to create some working yet unmanageable code.
Throughout the code I'm finding multi-conditional ternary expressions. It's getting to be a headache to translate and re-write/refactor them.
Does anyone know of a free tool, standalone or as an add-in for VS 2008, that can decompose ternary expressions? There's no budget for CodeRush on this project. I'll continue re-coding if needed, but I'm trying to have a little hope here.
Here's an example of the issue:
sNoteType = objSelection.Items[1].Selected ?
objSelection.Items[0].Selected ?
objSelection.Items[3].Selected ?
objSelection.Items[4].Selected ?
objSelection.Items[5].Selected ?
objSelection.Items[2].Selected ?
string.Empty + "OV " :
string.Empty + "EV " :
objSelection.Items[2].Selected ?
string.Empty + "OV " :
string.Empty + "LT " :
objSelection.Items[5].Selected ?
objSelection.Items[2].Selected ?
string.Empty + "OV " :
string.Empty + "EV " :
objSelection.Items[2].Selected ?
string.Empty + "OV " :
string.Empty + "BA " :
objSelection.Items[4].Selected ?
objSelection.Items[5].Selected ?
objSelection.Items[2].Selected ?
string.Empty + "OV " :
string.Empty + "EV " :
objSelection.Items[2].Selected ?
string.Empty + "OV " :
string.Empty + "LT " :
objSelection.Items[5].Selected ?
objSelection.Items[2].Selected ?
string.Empty + "OV " :
string.Empty + "EV " :
objSelection.Items[2].Selected ?
string.Empty + "OV " :
string.Empty + "LS " :
objSelection.Items[3].Selected ?
objSelection.Items[4].Selected ?
objSelection.Items[5].Selected ?
objSelection.Items[2].Selected ?
string.Empty + "OV " :
string.Empty + "EV " :
objSelection.Items[2].Selected ?
string.Empty + "OV " :
string.Empty + "LT " :
objSelection.Items[5].Selected ?
objSelection.Items[2].Selected ?
string.Empty + "OV " :
string.Empty + "EV " :
objSelection.Items[2].Selected ?
string.Empty + "OV " :
string.Empty + "BA " :
objSelection.Items[4].Selected ?
objSelection.Items[5].Selected ?
objSelection.Items[2].Selected ?
string.Empty + "OV " :
string.Empty + "EV " :
objSelection.Items[2].Selected ?
string.Empty + "OV " :
string.Empty + "LT " :
objSelection.Items[5].Selected ?
objSelection.Items[2].Selected ?
string.Empty + "OV " :
string.Empty + "EV " :
objSelection.Items[2].Selected ?
string.Empty + "OV " :
string.Empty + "FD " :
objSelection.Items[0].Selected ?
objSelection.Items[3].Selected ?
objSelection.Items[4].Selected ?
objSelection.Items[5].Selected ?
objSelection.Items[2].Selected ?
string.Empty + "OV " :
string.Empty + "EV " :
objSelection.Items[2].Selected ?
string.Empty + "OV " :
string.Empty + "LT " :
objSelection.Items[5].Selected ?
objSelection.Items[2].Selected ?
string.Empty + "OV " :
string.Empty + "EV " :
objSelection.Items[2].Selected ?
string.Empty + "OV " :
string.Empty + "BA " :
objSelection.Items[4].Selected ?
objSelection.Items[5].Selected ?
objSelection.Items[2].Selected ?
string.Empty + "OV " :
string.Empty + "EV " :
objSelection.Items[2].Selected ?
string.Empty + "OV " :
string.Empty + "LT " :
objSelection.Items[5].Selected ?
objSelection.Items[2].Selected ?
string.Empty + "OV " :
string.Empty + "EV " :
objSelection.Items[2].Selected ?
string.Empty + "OV " :
string.Empty + "LS " :
objSelection.Items[3].Selected ?
objSelection.Items[4].Selected ?
objSelection.Items[5].Selected ?
objSelection.Items[2].Selected ?
string.Empty + "OV " :
string.Empty + "EV " :
objSelection.Items[2].Selected ?
string.Empty + "OV " :
string.Empty + "LT " :
objSelection.Items[5].Selected ?
objSelection.Items[2].Selected ?
string.Empty + "OV " :
string.Empty + "EV " :
objSelection.Items[2].Selected ?
string.Empty + "OV " :
string.Empty + "BA " :
objSelection.Items[4].Selected ?
objSelection.Items[5].Selected ?
objSelection.Items[2].Selected ?
string.Empty + "OV " :
string.Empty + "EV " :
objSelection.Items[2].Selected ?
string.Empty + "OV " :
string.Empty + "LT " :
objSelection.Items[5].Selected ?
objSelection.Items[2].Selected ?
string.Empty + "OV " :
string.Empty + "EV " :
objSelection.Items[2].Selected ?
string.Empty + "OV " :
string.Empty;
The practice of decomposing numbers allows young students to understand the patterns and relationships between digits within a larger number and between numbers within an equation. You can decompose numbers into their hundreds, tens, and ones places, or you can decompose by separating numbers into their various addends. [1]
The ternary operator requires one line of code, making it more compact than a full if-else expression. You can regulate the flow of your program using conditional statements, such as if statements. The code inside conditional expressions runs when a specific condition (or a collection of events) is met.
The expressions may be nested, task is convert the given ternary expression to a binary Tree. Recommended: Please solve it on “ PRACTICE ” first, before moving on to the solution.
You can decompose decimal numbers, but every number placed past the decimal point must be decomposed into a position piece that is also written with a decimal point. [6] The "tenths" position is used for a single digit that comes after (to the right of) the decimal point.
The meaning of the code is really much simpler than it seems. Continuing with ReSharper found me the following:
string sNoteType;
var items = objSelection.Items;
var item0Selected = items[0].Selected;
string item3NotSelectedValue;
if (items[1].Selected)
{
item3NotSelectedValue = item0Selected ? "LS " : "FD ";
}
else
{
item3NotSelectedValue = item0Selected ? "LS " : string.Empty;
}
if (items[2].Selected)
{
sNoteType = "OV ";
}
else
{
if (items[5].Selected)
{
sNoteType = "EV ";
}
else
{
if (items[4].Selected)
{
sNoteType = "LT ";
}
else
{
if (items[3].Selected)
{
sNoteType = "BA ";
}
else
{
sNoteType = item3NotSelectedValue;
}
}
}
}
Since Jon did all the hard work, here's an edit that I think boils it down to the essence. Obviously, you'd want to put tests around this code ASAP - as I can't imagine not making some mistake in decoding this monstrosity, and automated refactoring is only going to get you so far (not very, from the looks of the samples placed here):
var items = objSelection.Items;
string sNoteType = string.Empty;
if (items[0].Selected && items[1].Selected) {
sNoteType = "LS ";
} else if (items[1].Selected) {
sNoteType = "FD ";
} else if (items[2].Selected) {
sNoteType = "OV ";
} else if (items[3].Selected) {
sNoteType = "BA ";
} else if (items[4].Selected) {
sNoteType = "LT ";
} else if (items[5].Selected) {
sNoteType = "EV ";
}
ReSharper can convert ternary to if/else.
I actually ran it through ReSharper, and the output is equally terrifying. I wish you the best of luck in refactoring.
if (objSelection.Items[1].Selected)
if (objSelection.Items[0].Selected)
if (objSelection.Items[3].Selected)
if (objSelection.Items[4].Selected)
if (objSelection.Items[5].Selected)
if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
else sNoteType = string.Empty + "EV ";
else
{
if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
else sNoteType = string.Empty + "LT ";
}
else
{
if (objSelection.Items[5].Selected)
if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
else sNoteType = string.Empty + "EV ";
else
{
if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
else sNoteType = string.Empty + "BA ";
}
}
else
{
if (objSelection.Items[4].Selected)
if (objSelection.Items[5].Selected)
if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
else sNoteType = string.Empty + "EV ";
else
{
if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
else sNoteType = string.Empty + "LT ";
}
else
{
if (objSelection.Items[5].Selected)
if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
else sNoteType = string.Empty + "EV ";
else
{
if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
else sNoteType = string.Empty + "LS ";
}
}
}
else
{
if (objSelection.Items[3].Selected)
if (objSelection.Items[4].Selected)
if (objSelection.Items[5].Selected)
if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
else sNoteType = string.Empty + "EV ";
else
{
if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
else sNoteType = string.Empty + "LT ";
}
else
{
if (objSelection.Items[5].Selected)
if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
else sNoteType = string.Empty + "EV ";
else
{
if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
else sNoteType = string.Empty + "BA ";
}
}
else
{
if (objSelection.Items[4].Selected)
if (objSelection.Items[5].Selected)
if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
else sNoteType = string.Empty + "EV ";
else
{
if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
else sNoteType = string.Empty + "LT ";
}
else
{
if (objSelection.Items[5].Selected)
if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
else sNoteType = string.Empty + "EV ";
else
{
if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
else sNoteType = string.Empty + "FD ";
}
}
}
}
else
{
if (objSelection.Items[0].Selected)
if (objSelection.Items[3].Selected)
if (objSelection.Items[4].Selected)
if (objSelection.Items[5].Selected)
if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
else sNoteType = string.Empty + "EV ";
else
{
if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
else sNoteType = string.Empty + "LT ";
}
else
{
if (objSelection.Items[5].Selected)
if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
else sNoteType = string.Empty + "EV ";
else
{
if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
else sNoteType = string.Empty + "BA ";
}
}
else
{
if (objSelection.Items[4].Selected)
if (objSelection.Items[5].Selected)
if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
else sNoteType = string.Empty + "EV ";
else
{
if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
else sNoteType = string.Empty + "LT ";
}
else
{
if (objSelection.Items[5].Selected)
if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
else sNoteType = string.Empty + "EV ";
else
{
if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
else sNoteType = string.Empty + "LS ";
}
}
}
else
{
if (objSelection.Items[3].Selected)
if (objSelection.Items[4].Selected)
if (objSelection.Items[5].Selected)
if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
else sNoteType = string.Empty + "EV ";
else
{
if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
else sNoteType = string.Empty + "LT ";
}
else
{
if (objSelection.Items[5].Selected)
if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
else sNoteType = string.Empty + "EV ";
else
{
if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
else sNoteType = string.Empty + "BA ";
}
}
else
{
if (objSelection.Items[4].Selected)
if (objSelection.Items[5].Selected)
if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
else sNoteType = string.Empty + "EV ";
else
{
if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
else sNoteType = string.Empty + "LT ";
}
else
{
if (objSelection.Items[5].Selected)
if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
else sNoteType = string.Empty + "EV ";
else
{
if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
else sNoteType = string.Empty;
}
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With