Pretending I have a code like this:
(Yes, it is my code. But let just never mind its function)
// Triple nest!!!
class DB
{
static fetch(db, getRecord)
{
for (var i = 0; i < db.length; i++)
{
getRecord(db[i]);
}
}
}
Is it readable to you guy? Should I avoid nest of code? Does it take long time to compile and cost a lot computer (CPU) resource?
Which language are you using? A lot of languages benefit from Collections nowadays.
But as others said, you need another level to make it nested.
class DB
{
static fetch(db, getRecord)
{
db.forEach(item => { getRecord(db); });
}
}
There is a metric called cyclomatic complexity that measures the amount of branching logic that a program has. The greater the cyclomatic complexity, the more difficult a program is to comprehend. So yes, nesting and branching should be minimized as much as feasible.
Ahmed Mahmoud
Front-end development
I don't see nesting here. I see Class (Model), Method, and iteration 🤷♂️.
The nesting would be to have multiple iterations inside each other, or multiple callbacks, that would be arguably harder to read. But this example is perfectly fine.