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'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.
Related:<p><i>Tom Duff: Reading Code From Top to Bottom (1999)</i> - <a href="https://news.ycombinator.com/item?id=2620872" rel="nofollow">https://news.ycombinator.com/item?id=2620872</a> - June 2011 (22 comments)<p>Not sure how the 1999 ended up there but it looks plausible enough so I'll copy it.
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.<p>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.
Tom Duff at the end is referring to a very nuanced kind of response of Knuth to Dijkstra from 1974, like so:<p>> <i>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.</i><p><a href="https://scholar.google.com/scholar_lookup?title=Structured%20programming%20with%20GOTO%20statements&publication_year=1974&author=D.E.%20Knuth" rel="nofollow">https://scholar.google.com/scholar_lookup?title=Structured%2...</a><p>It has received only very little discussion here on HN<p><a href="https://hn.algolia.com/?dateRange=all&page=0&prefix=false&query=Structured%20programming%20with%20goto%20statements&sort=byPopularity&type=story" rel="nofollow">https://hn.algolia.com/?dateRange=all&page=0&prefix=false&qu...</a>
WDYT about initializing a limit and having only one return? For/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.
My observations, as an opinionated C programmer:<p>1. Missing const; always const all the things, especially pointers passed to functions. Semantically a "find" would be a read-only operation, to me it's a huge red flag if it's not const-declared.<p>2. Why NULL-check the array but not the name? I guess it doesn't affect functionality, or perhaps NULL is a valid name. Confusing without more context/commentary.<p>3. I prefer "++i" rather than "i++" in for loops like this because of reasons, but that's a rather minor point of course.<p>4. Be daring and use at least C99, move the "int i" to the for-loop's initializing part.<p>5. It's not obvious that the comparison doesn'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's just not ... good enough yet. :)<p>Edit: list formatting.
1. Missing spaces around operators like "==". 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'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's scary that the author only rewrote this due to a language limitation. The original is painful to read.