6 ms·
You could image code like this: for (pos = 0; !found; pos++) { row = get_next_row(); if (strstr(row, "user") { // this is obviously a header ro
by thriqon 10y ago
You could image code like this:
for (pos = 0; !found; pos++) {
row = get_next_row();
if (strstr(row, "user") {
// this is obviously a header row
continue
}
if (strstr(row, user_name)) {
found = 1;
}
}
- wingerlang 10y agofor (pos = 0; !found; pos++) This is so cool, I have never thought about this in like a decade of programming. I mean using a bool variable within a for loop.
- lmm 10y agoDon't do it in production code - indeed try to avoid languages where "for" allows such arbitrary constructs.
- TeMPOraL 10y ago> Don't do it in production code I'd say fair, but... > indeed try to avoid languages where "for" allows such arbitrary constructs. ... come on. I'd say - go for such languages, they give you if a tiny bit more expressive power than ones who don't let you write such a loop.
- lmm 10y agoAllowing the same statement to do any kind of arbitrary nonsense is not the good kind of expressiveness. Far better to separate your concerns, and keep the orthogonal parts orthogonal. C-style for mixes too many different things.
- TeMPOraL 10y agoSemantics of the C-style for loop are pretty well defined - it's: for(initialize form ; condition form ; step form) { loop body forms }. Ascribing any different meaning to it, like e.g. that all three forms in the loop header must be present or must refer to the same set of variables, is a mistake. That said, if one has an alternative that can express their intention better (like e.g. mapcar, foreach loop, Lisp's dotimes, etc.), one should use it instead.
- seanmcdirmid 10y agoWell, you also have to consider greater debugging costs of you use a loop comprehension. Expression of intention isn't the only issue to consider.
- TeMPOraL 10y agoI disagree - debugging costs usually come directly from choosing a construct that does not express programmer's intentions (like using a naked for when foreach would be more adequate - or, conversely, by messing with conditionals inside loop body when putting the condition in the loop header would be more adequate).
- seanmcdirmid 10y agoDebugging a higher order function application is harder than stepping through a loop, at least in most debuggers I've used. I use them only when the logic called indirectly is not complicated to require much debugging (and I still wind up converting comprehensions into loops because they are too much trouble).
- TeMPOraL 10y agoOh ok, I can imagine more complex expressions being harder to debug from machine code level. That said, I rarely if ever have to resort to low-level debugging in such cases. I'm yet to encounter a problem that couldn't be solved by looking at the code and (very occasionally) stepping it through in the debugger. Here, using higher-order constructs in code is actually very helpful - the closer your code gets to expressing your intent, the smaller the set of possible bugs is. For instance, you can't make an off-by-one error in a foreach or a mapcar.
- seanmcdirmid 10y agoThere are many other things to debug beyond the basics. I admire those people who can do all the computer simulation in their head, but many of us want good debugging support, ever since the mid 90s (well, it really started with Microsoft's CodeView in the late 80s). PL abstractions should consider that in their designs, not just elegant conciseness. Code isn't just read.
- return0 10y agoWhat you call "Arbitrary nonsense" is for others good practice. For loops allow you to check all the conditions of the loop in one single line, without having to read the body of the loop, which i consider good practice.
- pmontra 10y agoIt doesn't apply to that sample code but maybe this is generally safer: for (pos = 0; !found || pos < limit; pos++) but there is nothing wrong in using an iterator and break out of the loop, especially if the language or the library gives you iterators.
- jjawssd 10y agoThat should be logical AND, not logical OR, because if you go over the limit and the right side is False, the left side will always be True if the item is not found, and True || False is still True, and the loop continues infinitely. When you are over the iteration limit you want to trigger True && False which is False to break out of the loop where the right side False is the condition when pos > limit Btw this is covered in Code Complete Second Edition by Microsoft Press on page 378-380
- pmontra 10y agoYou are right. I believed I didn't need a test for that. See what happens? :-)
- deleted 10y ago[deleted]
- fallingfrog 10y agoSpeak the truth, brother. Sadly I've seen production code (I do not work at Microsoft) that contains assumptions that are really that bad..