3 ms·
I HaveThisPattern. :) It is good to see that more people than me search for such... lets call them control flow patterns, because they reduce how much you have
by tetha 16y ago
I HaveThisPattern. :) It is good to see that more people than me search for such... lets call them control flow patterns, because they reduce how much you have to think about mundane things during programming in a language which does not provide such primitives. You can just think "ok, iterate this array", and while your fingers note down this pattern, you can already figure what to put in there.
Of course, one needs to consider the loop constructs the language has. I would be annoyed if someone wrote the first loop in java, instead of using a for-each loop.
I just have some patterns differntly. For example, by now, I use a method to implement the guarded linear search, as it requires less branches and allows to hide the handling of the not-found-case:
for (i = 0; i < students.size(); i++)
if (student[i].hasGoodGrade()) return student[i];
return noStudentFound;
For the polling loop, I'd just extract a conditional.
do { readValue(); } while(!valueValid());
But yes, I recognize that these are minor stylistic quibbles :)