4 ms·
Java especially in its enterprisey form is reasonably good at covering the maximum number of possible use cases, but it's bad at making the common cases simple.
by orangecat 17y ago
Java especially in its enterprisey form is reasonably good at covering the maximum number of possible use cases, but it's bad at making the common cases simple. Yes, I should be able to write the one-line Future without boilerplate to get reasonable default behavior, and then add custom configuration later if I actually need it. That both reduces the amount of code and makes nonstandard usage more prominent.
- jrockway 17y agoYes, this is exactly what Java doesn't get. Reading a file with only the standard libraries is 5 lines of code. Hello world is 6. The main problem is that this encourages cargo-culting. Once someone has something working, they will just cut-n-paste-n-modify it in similar situations. Better to cargo-cult 10 lines of code than to write 5 of your own, right? The moving parts won't cut off your fingers as long as your fingers don't get too close. (Normally I would rant about how "proper abstraction is not possible with Java", but it actually is. It just seems that the majority of libraries and programmers would rather cargo-cult than to design proper abstractions. And no, "proper abstraction" is not always "cut-n-paste from GoF".)
- TeHCrAzY 17y agoThis is why I love .Net. Want a quick and dirty file read? string fileContent = System.IO.File.ReadAllText(); Want to read it into lines? string[] fileLines = System.IO.File.ReadAllLines(); Want fine tuned byte by byte access? var fs = new System.IO.FileStream(path); They certainly looked long and hard at Java before writing .net, and covered many of the sore points carefully.
- cubicle67 17y agoI think you've missed specifying the filename, but otherwise cool. Here's Ruby for comparison: file_content = IO.read(filename) file_lines = IO.read(filename).split("\n")
- deleted 17y ago[deleted]
- regularfry 17y agofile_lines = File.readlines(filename) Not that it matters hugely :-)
- mmastrac 17y agoNot all of those methods were in .Net 1.0. I worked through the painful years of .Net 0.9 to .Net 2.0 and it wasn't always roses. The core libraries were really terrible and sparse in the early versions. Microsoft was a lot more aggressive in how they extended the core libraries post-2.0. There was an amazing amount of baggage in the language by the time 2.0 came out. FWIW, you can do the first two items you mention using the Apache Commons IOUtils class in Java. The latter is part of Java: http://java.sun.com/j2se/1.4.2/docs/api/java/io/FileInputStream.html http://java.sun.com/j2se/1.4.2/docs/api/java/io/FileInputStr...
- pyre 17y agoTrue enough. Why do I have to write this: SingleFilenameFilter myFilenameFilter = new SingleFilenameFilter() List myList = new File("/tmp").listFiles(myFilenameFilter) Why should I have to create a class that implements FilenameFilter, just to filter on some file names? Especially in a one-off instance, why should I have to create a class that I will only ever use once? Why isn't there a default implementation? Something like: public class RegexFilenameFilter implements FilenameFilter { private String regex; public RegexFilenameFilter(String re) { this.regex = re; } public accept(File dir, String name) { // matching here } } Then I could just write: List relevantFiles = new File("/tmp").listFiles(new RegexFilenameFilter("^tmp\\.[a-z0-9]+$")) Why should I have to create this class for every project (or create my own little 'utils' library)? It's easily one of the simplest usages of a FilenameFilter.
- stuff4ben 17y agoanonymous inner classes are your friend (poor mans closure): File[] textFiles = directory.listFiles(new FileFilter() { public boolean accept(File file) { return file.isFile() && file.canRead() && file.getName().endsWith(".txt"); } });
- mmastrac 17y agoFile enumeration has sucked for a long time in Java. java.nio.file should hopefully fix this in 1.7, but I haven't had a chance to play with it.