Live data from Hacker News

Reading code from top to bottom (1999)

iq0.com

1–10 of 15 posts

Re: Reading code from top to bottom (1999)

#3
I prefer code in this style too:

    if something:
        return something
    if something else:
        return something else
    return default
It's not applicable everywhere, but where it is, it seems tidier to me than the alternative:

    if something:
        return something
    elif something else:
        return something else
    else:
        return default
Edit: The article basically says the same thing. I just had a hard time parsing all that C at first.

Re: Reading code from top to bottom (1999)

#4
post #2

Related: Tom Duff: Reading Code From Top to Bottom (1999) - https://news.ycombinator.com/item?id=2620872 - June 2011 (22 comments) Not sure how the 1999 ended up there but it looks plausible enough so I'll copy it.

There are several references to the time of writing in the piece, each with a date and the last one is

Yesterday (Feb 21, 1999) I rewrote [...]

Re: Reading code from top to bottom (1999)

#8
post #3

I prefer code in this style too: if something: return something if something else: return something else return default It's not applicable everywhere, but where it is, it seems tidier to me than the alternative: if something: return something elif something else: return something else else: return default Edit: The article basically says the same thing. I just had a hard time parsing all that C at first.

To me early returns and top level exceptions are nice to convey base cases, invariants, short cuts etc. nested branching to convey choice that is more closely related.

The former is often not possible (or idiomatic) in expression based languages however.

Re: Reading code from top to bottom (1999)

#9
Tom Duff at the end is referring to a very nuanced kind of response of Knuth to Dijkstra from 1974, like so:

> The best reference on programming in a disciplined way using goto is still D. E. Knuth's paper Structured Programming with go to Statements, ACM Computing Surveys, Vol. 6, No. 4, December 1974, pp. 261-302. I think that everything I have to say about goto is in his paper.

https://scholar.google.com/scholar_lookup?title=Structured%2...

It has received only very little discussion here on HN

https://hn.algolia.com/?dateRange=all&page=0&prefix=false&qu...

Re: Reading code from top to bottom (1999)

#10
I like the 'guard clauses that return 0 etc at the top, "real" return at the bottom' style in general, but I find it important to be careful that it doesn't degenerate into 'returns in the top, middle, and bottom', which isn't any better than the thing we were trying to avoid in the first place.

Similar thing with avoiding nesting and else clauses - generally a good idea, but occasionally they're the right choice, so better not to be dogmatic about it.

Post reply on HN