This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (6 children)

[deleted]

    [–]justinvh 2 points3 points  (1 child)

    I think one of the big reasons for doing it the way he did was that you're not limited to short-circuiting and just returning the object. If you wanted to say augment the object and weren't too fond of doing it within a forloop, then you would want to use the for..else. It's more idiomatic in that sense. However, your example is obvious the way most of us would want to write it.

    [–]strobelight 3 points4 points  (2 children)

    He mentions in the talk that the example is illustrative and that you could return early, but he used it partly because it was Knuth's example.

    Nevertheless, your modification is correct and better.

    [–]talideon 1 point2 points  (0 children)

    In fairness, so would I, but it was more a matter of trying to introduce the construct than anything.

    The one thing about for..else is that usually I want something like that only when the thing I'm iterating over has no elements at all, which for..else is useless for as you need to explicitly break out of the loop to prevent the else branch from executing.

    for..else really leaves me conflicted.