Reducing cyclomatic complexity of function
Mar 14, 2018 at 5:59am UTC
Hey there, I'm in need of some help here. Any good ideas on how to break this one function into separate functions in order to avoid having so many nested loops? Any input will be appreciated. Thanks!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
Relation* RelationalDatabase::select(string relation, vector<string> values) {
Relation* operand = this ->getRelation(relation);
if (operand == NULL) return NULL;
Relation* result = new Relation("query" , operand->scheme);
// loop over relation rows
for (set<vector<string>>::iterator i = operand->rows.begin(); i != operand->rows.end(); i++) {
bool add_row = true ;
vector<string> row = *i;
for (unsigned int j = 0; j < values.size(); j++) {
if (values[j].find("'" ) != string::npos && values[j] != row[j]) {
add_row = false ;
}
// make sure matching IDs also match value
if (values[j].find("'" ) == string::npos) {
for (unsigned int k = j + 1; k < values.size(); k++) {
if (values[j] == values[k] && row[j] != row[k]) {
add_row = false ;
break ;
}
}
}
if (!add_row) break ;
}
if (add_row) {
result->addRow(row);
}
}
return result;
}.
Mar 14, 2018 at 11:04am UTC
Do you realize that you're copying vectors?
Topic archived. No new replies allowed.