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.

Ask HN: Writing code with diff-readability in mind?

15 pointsby Syrup-tanover 9 years ago
While this isn&#x27;t a very dire or pressing matter, I think it would be interesting to discuss.<p>It&#x27;s often considered good practice to use trailing commas in multi-line arrays (where language syntax rules permit); e.g.<p><pre><code> [ &#x27;a&#x27;, &#x27;b&#x27;, &#x27;c&#x27;, ] </code></pre> going further, would it also make sense to start an else-if check train with ``if false; &#x27; &#x27; ? e.g.<p><pre><code> if false; else if check 1; etc else if check 2; etc else if check 3; etc </code></pre> for a real-ish world example:<p><pre><code> https:&#x2F;&#x2F;gist.github.com&#x2F;Syrup-tan&#x2F;c78f781c9b500b802fec#file-w-sh-L33-L56 https:&#x2F;&#x2F;gist.github.com&#x2F;Syrup-tan&#x2F;c78f781c9b500b802fec#file-w2-sh-L33-L56 </code></pre> the second way grants a refactor-er the ability to re-order the elif checks without modifying any of the statements, reducing the chance of basic errors. It also makes diffs simpler to read.<p>However, in exchange, it sort of violates ``Principle of Least Surprise &#x27; &#x27;, as someone reading the code may not understand what the ``if false; then :; &#x27; &#x27; is doing there. ( ``: &#x27; &#x27; is the null-operation in sh)<p>I&#x27;d love to hear more thoughts on this practice.<p>edits: readability &amp; highlighted section in gist ( no wording changes )

7 comments

wellpastover 9 years ago
&gt; It also makes diffs simpler to read.<p>I find that there&#x27;s a pretty direct conflict between trying to keep diffs &#x27;pretty&#x27; and the quality of the ever-growing code.<p>In my experience, code quality is only achieved and sustained by continual refactoring. When this continual refactoring attempts to satisfy easy-to-read diffs the objective stops being to simplify the code itself and rather to simplify the delta -- which limits&#x2F;checks the simplicity the code can achieve.
评论 #10403921 未加载
评论 #10402906 未加载
toomimover 9 years ago
Write a better diff! Make the computer do more work, rather than bending over backwards to fit the whims of your diff.<p>Try searching for &quot;semantic diff&quot; or &quot;tree-diff&quot; or &quot;syntax-aware diff&quot;.<p><a href="http:&#x2F;&#x2F;martinfowler.com&#x2F;bliki&#x2F;SemanticDiff.html" rel="nofollow">http:&#x2F;&#x2F;martinfowler.com&#x2F;bliki&#x2F;SemanticDiff.html</a> <a href="https:&#x2F;&#x2F;www.semanticmerge.com" rel="nofollow">https:&#x2F;&#x2F;www.semanticmerge.com</a>
twblalockover 9 years ago
Diffs are a secondary concern. Writing code that is well-structured and readable as it currently exists is the most important thing.
devitover 9 years ago
Changing if&#x2F;else sequences that cannot be written as switch&#x2F;match constructs in that way is probably too rare to make this a good idea.
luckydudeover 9 years ago
Not code, but docs, here is what I do:<p>Start each sentence on a new line (I have a perl script that formats text like this).<p>The reason is that when you version control the docs it&#x27;s easier to pick out the diffs if the sentences are boundaries.<p>It&#x27;s simple, easy, makes looking at diffs faster.
Syrup-tanover 9 years ago
for quality-of-life;<p><a href="https:&#x2F;&#x2F;gist.github.com&#x2F;Syrup-tan&#x2F;c78f781c9b500b802fec#file-w-sh-L33-L56" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;Syrup-tan&#x2F;c78f781c9b500b802fec#file-...</a><p>vs <a href="https:&#x2F;&#x2F;gist.github.com&#x2F;Syrup-tan&#x2F;c78f781c9b500b802fec#file-w2-sh-L33-L56" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;Syrup-tan&#x2F;c78f781c9b500b802fec#file-...</a>
评论 #10401762 未加载
LarryMade2over 9 years ago
I have come to prefer putting commas in lists at the beginning of things<p>SELECT<p>field1<p>,field2<p>,field3<p>,field4<p>FROM table<p>More easy to realize you are missing separators and it just looks nicer to me.
评论 #10403996 未加载