4 ms·
I think the conciseness can improve readability. Consider these examples: List<String> list = getListofStrings(); List<String> capitalized = new ArrayList
by saryant 13y ago
I think the conciseness can improve readability. Consider these examples:
List<String> list = getListofStrings();
List<String> capitalized = new ArrayList<String>();
for(String st : list)
if(Character.isUpperCase(st.charAt(0)))
capitalized.add(st);
Or...
val list = getListOfStrings
val capitalized = list.filter(_.head.isUpper)
Grokking two lines will take less time than grokking six lines.
- VladRussian2 13y ago>Grokking two lines will take less time than grokking six lines. when your mental/visual pattern machinery have been trained for 15+ years on the latter case? :)
- dragonwriter 13y agoSure, if you are excessively habituated to reading Language X and have some kind of barrier to learning new things, Language X will be better for you for most uses. That's not really a feature of Language X, though.
- saryant 13y agoIMO, the latter example is more readably because you're describing the result of the function rather than the outlining the operation itself.
- Dewie 13y agoAn IDE that could take the first example, then hide it, and then infer what that would look like with more higher order functions (the latter example), and then display that, that would be a truly magical IDE...
- abalone 13y agoOk, first of all your Java example is five lines not six. Second, a lot of the concision comes from omitting type declarations. I will argue this decreases the readability of the Scala example. What kind of data structure does getListOfStrings return? With Java you know with complete certainty that it returns a List of String (and only String). With Scala you have to infer it from the method name and hope it's honest. Likewise with the filter function. Whatever your opinion is of explicit type declarations, they are not mere "boilerplate". There are certainly other savings in your Scala example apart from types, like the filter lambda function. I'm just a bit incredulous that Scala advocates genuinely think this saves them large quantities of time over the course of a project vs. an IDE-assisted Java version. I mean, how much longer would it take you to write or grok that Java for loop? Seconds? How much longer to do it 100X in a project? Minutes? There has got to be a deeper underlying emotional reason why people advocate Scala -- which is not to invalidate that reason at all. It's important to feel love for your tools. But be honest about it. It's not about saving hundreds of hours of time writing boilerplate.
- gclaramunt 13y agoIs not about writing the boilerplate, is about having to think at that level. In Java, you think "I loop through the Strings and if it starts with uppercase, I'll add it to a new collection (which I need to remember to intialize)", in Scala is more "filter the strings that starts with uppercase". And even if the IDE can generate most of it, it still shows it to you to modify the template, and even if later hides it, it still in your code, in your diffs. Besides, higher order functions are not the only thing, traits are an awesome tool for structuring separation of concerns, and java doesn't come closer.