Untodesu sends us this submission, with this comment:
Literally no idea what kind of drugs the guy was taking but nonetheless we've rewritten it to be just a two-liner
Well, that doesn't tell us a lot about what to expect from the code, but let's take a look.
QStringList TableViewAssembly::parametersFilter(ProbePart::Type type, int pos, QList<ProbePart> probeDesign) {
QString to, from;
if(pos == -1) {
if(probeDesign.length() == 0) {
to = "*";
from = "AutoJoint";
} else {
to = probeDesign.at(0).fromMounting();;
from = "AutoJoint";
}
} else if(pos == 0) {
if(probeDesign.length() == 1) {
if(probeDesign.at(pos).type() == ProbePart::Type::Stylus) {
to = probeDesign.at(pos).fromMounting();
from = "*";
} else {
to = "*";
from = probeDesign.at(pos).toMounting();
}
} else {
to = probeDesign.at(pos + 1).fromMounting();
from = probeDesign.at(pos).toMounting();
}
} else if(pos == probeDesign.length() - 1) {
if(probeDesign.at(pos).type() == ProbePart::Type::Stylus) {
if(probeDesign.length() <= 1) {
from = "*";
to = probeDesign.at(pos).fromMounting();
} else {
from = probeDesign.at(pos - 1).toMounting();
to = probeDesign.at(pos).fromMounting();
}
} else {
from = probeDesign.at(pos).toMounting();
to = "*";
}
} else {
from = probeDesign.at(pos).toMounting();
to = probeDesign.at(pos + 1).fromMounting();
}
return { to, from };
}
QStringList andQList tell me that this is a Qt-based application. The goal of this function seems to be to take some inputs about a "probe part" and construct a pair of strings. Let's trace through it.
Let's just walk through the conditions, quickly, without worrying too much about the inside. We look at pos, and check for three cases: either pos is -1, 0, or probeDesign.length() - 1.
Inside each of those branches, we also check the length of the list, testing if it contains no elements, exactly one elemnet, or more than one element. We also check if the part in question is a stylus.
With that in mind, let's see if we can summarize the conditions here. If pos == -1, we do some automatic stuff, using the first element in the list if there is one. If pos == 0 and there's exactly one element in the list, we grab the first element and link it to * (the to/from order depends on the stylus question). If there's more that one element in the list, we pair the current pos with pos+1; notably, in this branch, pos is definitely zero. If pos is the last element in the list, we follow the same logic, but pair with pos-1, with a side branch for checking against the length of the list.
It's all bounds checking. That's all this code is. Bounds checking that's gotten out of hand. The main branch here is actually the final else: that's where most of the code is going to pass through. All the other branches are just handling edge cases. Literal edge cases, as in "the edge of the list".
Untodesu didn't supply the two line version, but based on the fact such a version exists, I also suspect that many of these branches weren't actually used. Or, at least, based on the actual business rules, could be combined.