This post originated from an RSS feed registered with Scala Buzz
by Ricky Clarkson.
Original Post: My Scala Coding Style
Feed Title: Ricky's technical blog
Feed URL: http://rickyclarkson.blogspot.com/feeds/posts/default
Feed Description: A blog about programming languages, and Java.
This was originally a posting to the scala-tools mailing list, in response to someone asking how to make the Eclipse plugin format using braces on the next line and spaces instead of tabs.
I used the braces-on-next-line rule, lining up the { to the previous
line rule for years, and tabs and no spaces, for years. I was happy
with it. I used an 80-character line length limit, which made coding
in a text terminal easy, and tabs equivalent to 8 characters (though
the actual tab width didn't really matter as I only used indentation
to signify nesting depth, not to line up individual characters). My
code was very structured.
If you look at my blog's very early days, you will find a post
somewhere saying to avoid anonymous classes in Java. This is exactly
the opposite of what I'd say now (now I'd say avoid Java :) ). It
dawned on me after a while that the reason I didn't like anonymous
classes was that my coding style made them a real pain in the backside
to use. Specifically, if you have one anonymous class inside another
with the above coding style, you end up splitting most lines of actual
code.
Then I learned Lisp.
I realised that my overly structured Java code penalised me for
nesting. So nesting must be bad, or the coding style must be bad.
Most human languages are highly recursive - within one sentence you
can set up phrases, talk about what would happen in the future, and
discuss two possible futures or even possible pasts. Only one
language is known of that isn't highly recursive - the Piraha
language. In Piraha the culture discourages talking about the future,
or any event that you haven't seen personally, or been told about by
another. A researcher who lived with them and learned their language
tried telling them about Jesus (yes, there are God-botherers in
linguistics too). One Piraha asked what Jesus looked like - and when
the researcher said he didn't know, as Jesus lived 2,000 years ago,
the Piraha wasn't interested anymore. This actually caused the
researcher to start questioning his own religion, but that's a little
beside the point.
I imagine that if you asked a Piraha to design a computer programming
language (or a suitable analogy to one - perhaps they wouldn't be
interested in computers), I imagine it would be limited to a nesting
level of 2, by virtue of a 25-space tab and an 80-character line
limit. And if they invented braces, I'm sure they would be lined up.
This has parallels in our society too. The more fluent amongst us can
happily deal with long sentences, discussing multiple futures. The
less fluent (perhaps speakers of a foreign language learning English)
will generally prefer shorter sentences, discussing only the present.
In summary, pick a coding style that doesn't punish nesting, unless
you want to make the language 'unnaturally' statement-orientated. I
have taken this to an extreme - I use one space for indentation, and
never place opening OR closing parens on their own lines. I think
this is a direct result of learning Lisp, but it took a long time
after learning Lisp for me to change.
Plus, silly Scala makes putting the brace on the next line fail to
parse sometimes anyway.