At work I recently wrote a less than operator for a class implemented from a published specification and which has many properties, six of which are used to uniquely identify an instance of the class. (For the sake of this question, we'll call these properties a-f.) Also, these six properties are of six different types. I defined the operator as such:
bool operator<(const Class& lhs, const Class& rhs)
{
bool retval = (&lhs != &rhs);
if (retval == true)
{
if (lhs.a == rhs.a)
{
if (lhs.b == rhs.b)
{
if (lhs.c == rhs.c)
{
if (lhs.d == rhs.d)
{
if (lhs.e == rhs.e)
{
retval = (lhs.f < rhs.f);
} else {
retval = (lhs.e < rhs.e);
}
} else {
retval = (lhs.d < rhs.d);
}
} else {
retval = (lhs.c < rhs.c);
}
} else {
retval = (lhs.b < rhs.b);
}
} else {
retval = (lhs.a < rhs.a);
}
}
return retval;
}
This, of course, breaks the Linux kernel coding philosophy of, "If you need more than 3 levels of indentation, you're screwed anyway, and should fix your program." So my question is, is there a better way to define this operator to not have so many levels of indentation?