TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Reading code from top to bottom (1999)

47 pointsby julietteebabout 3 years ago

11 comments

sailaabout 3 years ago
I prefer code in this style too:<p><pre><code> if something: return something if something else: return something else return default </code></pre> It&#x27;s not applicable everywhere, but where it is, it seems tidier to me than the alternative:<p><pre><code> if something: return something elif something else: return something else else: return default </code></pre> Edit: The article basically says the same thing. I just had a hard time parsing all that C at first.
评论 #30903357 未加载
评论 #30915001 未加载
dangabout 3 years ago
Related:<p><i>Tom Duff: Reading Code From Top to Bottom (1999)</i> - <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=2620872" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=2620872</a> - June 2011 (22 comments)<p>Not sure how the 1999 ended up there but it looks plausible enough so I&#x27;ll copy it.
评论 #30903015 未加载
cehrlichabout 3 years ago
I like the &#x27;guard clauses that return 0 etc at the top, &quot;real&quot; return at the bottom&#x27; style in general, but I find it important to be careful that it doesn&#x27;t degenerate into &#x27;returns in the top, middle, and bottom&#x27;, which isn&#x27;t any better than the thing we were trying to avoid in the first place.<p>Similar thing with avoiding nesting and else clauses - generally a good idea, but occasionally they&#x27;re the right choice, so better not to be dogmatic about it.
frohabout 3 years ago
Tom Duff at the end is referring to a very nuanced kind of response of Knuth to Dijkstra from 1974, like so:<p>&gt; <i>The best reference on programming in a disciplined way using goto is still D. E. Knuth&#x27;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.</i><p><a href="https:&#x2F;&#x2F;scholar.google.com&#x2F;scholar_lookup?title=Structured%20programming%20with%20GOTO%20statements&amp;publication_year=1974&amp;author=D.E.%20Knuth" rel="nofollow">https:&#x2F;&#x2F;scholar.google.com&#x2F;scholar_lookup?title=Structured%2...</a><p>It has received only very little discussion here on HN<p><a href="https:&#x2F;&#x2F;hn.algolia.com&#x2F;?dateRange=all&amp;page=0&amp;prefix=false&amp;query=Structured%20programming%20with%20goto%20statements&amp;sort=byPopularity&amp;type=story" rel="nofollow">https:&#x2F;&#x2F;hn.algolia.com&#x2F;?dateRange=all&amp;page=0&amp;prefix=false&amp;qu...</a>
stitched2gethrabout 3 years ago
This is how the Go community recommends you write Go code. It is pleasant.
potiuperabout 3 years ago
If speed is deciding factor, then cases have to be written in descending order of likelihood.
engmgrmgrabout 3 years ago
WDYT about initializing a limit and having only one return? For&#x2F;else in some languages is kind of interesting, too.<p>I’m indifferent in practice (whatever’s readable to others is usually best), but I like the limit approach in theory.
badrabbitabout 3 years ago
I like bottom to top as well. I just dislike &lt;random location in the middle&gt; and then you scroll around trying to figure out context.
svilen_dobrevabout 3 years ago
i call that get-out-as-fast-as-u-know-the-decision style a &quot;die <i>now</i> without agony&quot;.
unwindabout 3 years ago
My observations, as an opinionated C programmer:<p>1. Missing const; always const all the things, especially pointers passed to functions. Semantically a &quot;find&quot; would be a read-only operation, to me it&#x27;s a huge red flag if it&#x27;s not const-declared.<p>2. Why NULL-check the array but not the name? I guess it doesn&#x27;t affect functionality, or perhaps NULL is a valid name. Confusing without more context&#x2F;commentary.<p>3. I prefer &quot;++i&quot; rather than &quot;i++&quot; in for loops like this because of reasons, but that&#x27;s a rather minor point of course.<p>4. Be daring and use at least C99, move the &quot;int i&quot; to the for-loop&#x27;s initializing part.<p>5. It&#x27;s not obvious that the comparison doesn&#x27;t need strcmp(), but might be clear from context, would expect comment to clarify that.<p>I agree with the top-down rewrite and find the revised code better, it&#x27;s just not ... good enough yet. :)<p>Edit: list formatting.
Mawrabout 3 years ago
1. Missing spaces around operators like &quot;==&quot;. The first code example has them, but the author proceeds to remove them with no mention as to why.<p>2. More than one statement on a line:<p><pre><code> int f(int x){ int v; if(b(x)) v=f1(x); else v=f2(x); return v; } </code></pre> This is so much harder to read than:<p><pre><code> int f(int x){ int v; if(b(x)) { v=f1(x); }else{ v=f2(x); } return v; } </code></pre> 3. Control flow statements placed arbitrarily far down a line:<p><pre><code> int f(int x){ if(b(x)) return f1(x); return f2(x); } </code></pre> Always keep them at the start of lines:<p><pre><code> int f(int x){ if(b(x)) return f1(x); return f2(x); } </code></pre> 4. Nested ternary operators, yikes:<p><pre><code> int a(int i, int j){ return i?a(i-1, j?a(i, j-1):1):j+1; } The language I&#x27;m testing is missing the ?: conditional expression, so I had to reword function a using ordinary if statements. int a(int i, int j){ if(i==0) return j+1; if(j==0) return a(i-1, 1); return a(i-1, a(i, j-1)); } Much better. </code></pre> It&#x27;s scary that the author only rewrote this due to a language limitation. The original is painful to read.