5 ms·
One thing that I think is often missed in the discussion around LOC and features like closures is that, while sometimes denser code is terser, often it's more e
by akeefer 17y ago
One thing that I think is often missed in the discussion around LOC and features like closures is that, while sometimes denser code is terser, often it's more expressive. For example, the code:
List<String> userNames = new ArrayList<String>();
for(User user : users) {
userNames.add(user.getName());
}
is not just more verbose but also reveals the programmers intentions less than something like:
var userNames = userList.map(\u -> u.Name)
Java programs often devolve into a lot of for loops, which all kind of look the same regardless of what you're doing, and you have to read pretty closely. It's nice to replace a bunch of loops with explicit methods like "map" or "find" or "reduce" or "each" or other things that explicitly declare the intent of the programmer.
I write a lot of Java, and we also have an in-house Java-like language built on the JVM (that we're working to open source), and we added closures to it in order to gain those sorts of benefits. Even though IDEA or Eclipse can write my for loops for me, it can't read them for me.
There's no technical reason why you can't have a statically-typed language with excellent tools that isn't unnecessarily verbose: all you really need to add are type inference, closures/first-class functions, and properties, and a little bit of metaprogrammability (instead of code generation) is also both feasible and helpful.
- akrymski 17y agoIn theory, why can't it read the for-loop for you? Why not summarize the above as eg: List<String> userNames = users[@name] Just showing the line in a different colour could mean it's "summarized". Clicking on it could reveal the complete loop and allow you to modify it. But common idioms such as map/find/reduce/each can be summarized.
- urlwolf 17y agoactually, Intellij IDEA v.9 does just that. see: http://www.jetbrains.com/idea/nextversion/ http://www.jetbrains.com/idea/nextversion/ And it has been open-sourced...