by T-R · 13y ago · 0 comments
Additionally, the abstraction features Java does offer are implemented with some heavy language syntax, and often can't be generated at runtime or pre-compile time, at least not easily, and so there's a limit to the abstraction, even taking into account Java Generics. That's something where homoiconic languages like Clojure have a particular advantage.
As a result, abstracting things in these ways (which in other languages is incredibly common) gets to be painfully verbose. Often, things that would be beautifully abstracted in these ways are just left unabstracted (as in-line boilerplate) - the verbosity introduced makes the code less readable, rather than more, and so there's a stigma against "over-abstracted" code. The result is that code that does, or could, make use of these things takes exceedingly more code in Java than in a language like Clojure. Of course, conventions in Java of, e.g., giving each curly brace its own line aren't helping its case in line counts, either.
My comment is basically a free-association from this.
In C and Java both (and, um, CSS), the people I've encountered seem to strongly prefer giving each matched pair of curly braces one line (that is, the opening { doesn't get its own line, but the closing } does).
In lisps (to my knowledge) it's more normal to have all your closing )s stuck together at the end of the previous line. Using a clump of )))))s to discern exactly which scope you're in afterwards is tricky (I'm pretty sure that's exactly the "problem" that the C/Java style is trying to solve), so you look at the indentation instead.
Python formalized the indentation-is-scope paradigm, which means a malicious programmer can't trick you into believing you're looking at a different scope than you are, but my Python code always ends up being a lot taller than I feel it should be, so I have a lingering sense that Python took it a little too far. It turns out that I occasionally appreciate the option you get with lisps to put a short bit of code all on one line and mark the parse tree with parens instead of with line-breaks-plus-indentation.
(All that said, I do like Python a lot.)
Another handy tool is an editor with rainbow delimiters. This feature changes the colour of each matched pair of parentheses so that they can be seen at a glance.
There's really no excuse not to, since refactoring functional code tends to be as simple as taking a node in a tree and moving it out.
In Java, if you have a class containing a function which itself contains an if-statement containing multiple lines. You will have three lines at the end of the file containing nothing but }. There is no reason you couldn't clump those together and thus end the last statement in your function with }}}.
The difference is, in Java it's good practise to give each } a seperate line. In Lisp, most people prefer to clump their ) together.
Also, you could easily end a function with alot of )))))).
(defn unique-large-squares [list-of-nums] (count (unique (filter #(> % 100) (map #(* % %) list-of-nums)))))
Of course, if the clump of ) are off-putting, you could always use ->
(defn unique-large-squares [list-of-nums] (-> list-of-nums (map #(* % %)) (filter #(> % 100)) (unique) (count)))
Aside from the fact that if you're color blind then apparently you're stuck with all the )))))))s.
I personally hate the opening { stuck on the end of the line in C/Java. For me it goes on a line on its own pretty much every time.
I've lost count of the amount of times there's been a subtle bug introduced by incorrect scope as a result of a { being missed from the end of a line and not being noticed due to the code being formatted as if it was there.
With a { sat on its own, directly lining up with the }, that's pretty much impossible to miss.
Hopefully the formatting will work for this -
For example,
if (isOK)
for(thing in list
{
doSomething(thing);
}
}
Screams out to me at very first glance that I've missed a {,whereas
if (isOK)
for(thing in list){
doSomething(thing);
}
}
needs me to scan to the end of each conditional line to see where the problem is.It's an abomination.