Closed thanhminhmr closed 5 years ago
I would usually refactor this as:
bool helper_func() {
for (i8 index = 0; index < 10; index++) {
if (!doSomething(index)) return false;
}
return true;
}
...
if (helper_func()) {
finishTheJob();
} else {
screamAndThrowEverything();
}
There are three alternative possibilites that comes to mind if one wanted to solve this in some more uniform manner:
With the third, the code could look like this:
func void do_job() {
load_jobs();
func bool helper() {
for (i8 index = 0; index < 10; index++) {
if (!doSomething(index)) return false;
}
return true;
}
if (helper()) {
finishTheJob();
} else {
screamAndThrowEverything();
}
cleanup_jobs();
}
I think the meaning of a for/while/etc loop should be a loop and not have a conditional meaning at the end. In my opinion this only makes the languages more complex, while it should consist of easy to understand operations.
Currently, things look like this:
With the
then {} else {}
, things should look like this:The similar syntax should be the same with
while
anddo while
loop. This should make the code shorter but easier to understand, remove the necessity for the extrabool
and should speed things up a bit compared to theif then
version (remove the extrabool
and the condition check). Personally I meet a lot of case where I wish this syntax is a thing. What do everyone think about this idea?