The recent discussion [1] on the Black formatter for Python had me thinking about whether there is any innovation still happening in the realm of code formatting. Most of the possible ways to format code in each language must have been tried by now, and I would expect we've converged on a narrow range of permutations purely for practical reasons. But recently I did see an interesting example [2] of Java formatting from the Boot library for Clojure, with a style that I had never seen before in that language.<p>Are there other major variants of code formatting style still being developed out there?<p>[1] https://news.ycombinator.com/item?id=30130315<p>[2] https://github.com/boot-clj/boot/blob/master/boot/base/src/main/java/boot/App.java
I find the Horstmann indentation style [1], in which code is placed on the same line as the opening bracket, simultaneously disturbing and compelling. If it ever ends up added to clang-format as has been requested [2], it might become more popular.<p>[1] <a href="https://en.wikipedia.org/wiki/Indentation_style#Horstmann_style" rel="nofollow">https://en.wikipedia.org/wiki/Indentation_style#Horstmann_st...</a>
[2] <a href="https://bugs.llvm.org/show_bug.cgi?id=27263" rel="nofollow">https://bugs.llvm.org/show_bug.cgi?id=27263</a>
Two come to mind off the bat:<p>GNU C formatting is odd: <a href="https://en.wikipedia.org/wiki/GNU_coding_standards" rel="nofollow">https://en.wikipedia.org/wiki/GNU_coding_standards</a><p>Zlib still uses K&R-1st-edition-era function definitions: <a href="https://github.com/madler/zlib/blob/cacf7f1d4e3d44d871b605da3b647f07d718623f/deflate.c#L240-L250" rel="nofollow">https://github.com/madler/zlib/blob/cacf7f1d4e3d44d871b605da...</a>
<a href="https://github.com/boot-clj/boot/blob/master/boot/base/src/main/java/boot/App.java" rel="nofollow">https://github.com/boot-clj/boot/blob/master/boot/base/src/m...</a> looks like the gnu standard for C (<a href="https://www.gnu.org/prep/standards/standards.html#Formatting" rel="nofollow">https://www.gnu.org/prep/standards/standards.html#Formatting</a>), applied to Java.<p>I think that was done to make grepping for function definitions easier (when searching for the definition of <i>foo</i>, grep for <i>“^foo\b”</i>. That makes implementing <i>“go to function definition”</i> possible without having to parse code)
Code formatting is all about matching the impedance of the source code to that of the human author/readers of the code. The development of literature suggest that innovation in this area will continue for a very long time.<p>I myself am a huge fan of Pascal, and limiting code to one function per line. I find that clever/terse code is unmaintainable code.<p>Here's some code I wrote back in 1991 for a TECO clone that reflects that style. Looking back, I think some things could have a few more comments, but it seems fairly easy to read the intent of the code.<p><a href="https://github.com/mikewarot/teco" rel="nofollow">https://github.com/mikewarot/teco</a>
Historically it was common advice to avoid elaborate code formatting, as keeping up such formatting by hand was tedious, time consuming, and inconsistent, compared to the value add.<p>Given near all code (let's say for large projects) is authored with IDEs, elaborate formats can be applied consistently and automatically, at little or no cognitive or typing cost.<p>To me the question is then if such formatting can increase the legibility of the code. The grid of static variable declarations in [2] is nice, but I wonder if sorting the variable lexicographically would make it easier to read/find a var by name?<p>Not sure how splitting method signature names onto a second line helps, though.
APL/J/K C-sources (perhaps by Roger Hui):
<a href="https://github.com/jsoftware/jsource/blob/master/jsrc/a.c" rel="nofollow">https://github.com/jsoftware/jsource/blob/master/jsrc/a.c</a> and other *.c files<p>Niklaus Wirth, Verilog:
<a href="http://people.inf.ethz.ch/wirth/FPGA-relatedWork/RS232R.v" rel="nofollow">http://people.inf.ethz.ch/wirth/FPGA-relatedWork/RS232R.v</a><p>all of ioccc)
I like Google's HTML style where they don't close some tags </li> and omit optional tags like <html> and <body><p>Example:<p><pre><code> <!-- Recommended -->
<!DOCTYPE html>
<title>Saving money, saving bytes</title>
<p>Qed.
</code></pre>
<a href="https://google.github.io/styleguide/htmlcssguide.html#HTML_Formatting_Rules" rel="nofollow">https://google.github.io/styleguide/htmlcssguide.html#HTML_F...</a>
I recall someone (nameless) who tried to <i>teach</i> Pascal by putting the semicolon at the <i>start</i> of every line. It actually works (as the Pascal semicolon is a separator, not a terminator), although you end up with many null/empty statements.<p>Sound confusing - very. I could see his "logic" but as to teaching programming students ... yes confusion all round, and wait till the poor students move on to any other language #@%%!$#!!!
I've seen this in the wild and have been meaning to ask:<p>With the availabily of syntax highlighting including color, has anyone seen merit in skipping indentation altogether?<p>For that matter, what if block color or font size took the place of indentation (under control of syntax highlighting). I'll have to try it by hand.
If you need some ideas for brainstorming, here is the endless source for inspiration.<p><a href="https://shitcode.net/" rel="nofollow">https://shitcode.net/</a>
Double-spacing. As in, between every line of code - a blank line.<p>To make it "less crowded", and allow space for future comments, I was told.