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

https://news.ycombinator.com/item?id=10401530
by Syrup-tan • 11 years ago
15 14 11 years ago

While this isn't a very dire or pressing matter, I think it would be interesting to discuss.

It's often considered good practice to use trailing commas in multi-line arrays (where language syntax rules permit); e.g.

  [
    'a',
    'b',
    'c',
  ]

going further, would it also make sense to start an else-if check train with ``if false; ' ' ? e.g.
  if false;
  else if check 1; etc
  else if check 2; etc
  else if check 3; etc

for a real-ish world example:
  https://gist.github.com/Syrup-tan/c78f781c9b500b802fec#file-w-sh-L33-L56
  https://gist.github.com/Syrup-tan/c78f781c9b500b802fec#file-w2-sh-L33-L56
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.

However, in exchange, it sort of violates ``Principle of Least Surprise ' ', as someone reading the code may not understand what the ``if false; then :; ' ' is doing there. ( ``: ' ' is the null-operation in sh)

I'd love to hear more thoughts on this practice.

edits: readability & highlighted section in gist ( no wording changes )

Related Stories

Loading related stories...

Source preview

news.ycombinator.com