8 ms·
Every time I see one of these ugly Java code examples, I always scratch my head because my code never looks like this. Here is what these code examples are exp
by srgseg 16y ago
Every time I see one of these ugly Java code examples, I always scratch my head because my code never looks like this.
Here is what these code examples are exploiting:
1. Java isn't designed for scripting. All of my Java programs require many classes. I hardly have any main() methods because most things I write are classes invoked by other classes, e.g. in the case of a servlet being invoked by the web application server.
2. It's rare in practice that I need to hard code the contents of data structures inside my own code. Normally they are loaded from data files.
3. Import statements are automatically thrown in by the IDE and most people hardly even notice they're there and type nothing more than Alt-Enter any time there is an import line missing.
So really, what I really am typing in java is only:
List<Integer> result = new ArrayList<Integer>();
for(int i : source) if(i%=2) result.add(i);
Now that's not really all that complicated is it?
- djacobs 16y agoThe point is that you have to write the for loop and if statement and concatenation for each different function you want to define. The better way is to use higher order functions, which Java lacks.