

Why I love everything you hate about Java - nkallen
http://magicscalingsprinkles.wordpress.com/2010/02/08/why-i-love-everything-you-hate-about-java/

======
Tichy
Um, I don't hate Factories, Dependency Injection and Decorators. I hate that
Java makes it very cumbersome to use them, typically via xml configuration
files (so that it can be dynamic) and adding complex syntax (generics,
annotations). And I hate that I can not pass functions as parameters. As he
says in the article: "function composition is a degenerate case of the
Decorator pattern." (not sure why "degenerate") - so instead of just composing
functions, in Java I have to write countless classes and interfaces. That's
what I hate.

~~~
axod
What the hell has XML got to do with Java? You're confusing frameworks with
languages. Most Java frameworks suck. Agreed.

~~~
Tichy
No, it is not a coincidence that Java and XML go hand in hand. Heavy XML usage
was introduced into common Java practice to compensate for the lack of dynamic
features in Java. So dynamic features were introduced (badly) through the XML
backdoor. Also often XML is used as a poor mans DSL.

It could have been something similar to XML (like JSON or YAML), but that is
not the point. Also, the verbosity of XML aligns itself well with the Java
mindset.

~~~
axod
Utter BS. I've been doing Java programming for 10 years and have never touched
XML thank god.

"the Java mindset" - more utter BS. There isn't a 'Java mindset' unless you're
a bad programmer who is ruled by his language instead of using languages as
tools.

The "mindset" you have depends on how good a coder you are. Which is the same
regardless of which language you decide to use to solve a particular problem.

~~~
Tichy
Fine, then you are the exception to the rule.

As for the mindset, it seems to me that at least SUN was very busy churning
out cookbooks for a lot of things. Mostly Enterprise Java - if you managed to
avoid that, more power to you. But I think most people tried to do things the
"official" way.

~~~
axod
Totally @ Enterprise. Horrible horrible stuff that, but really, there's some
great technology in the JVM, and built in libs.

And the Java language is a pretty sane general purpose language. It's fairly
easy to spot all the crap and avoid it.

------
emmett
Quoth the author:

In a very dynamic language like Ruby, open classes and method aliasing (e.g.,
alias_method_chain) mitigate this problem, but they don’t solve it. If you
manipulate a class to add logging, all instances of that class will have
logging; you can’t take a surgical approach and say “just objects instantiated
in this context”.

This is not true in Ruby!

    
    
      myinstance = MyClass.new
      def myinstance.special_method()
        puts "only on these instances!"
      end
      myinstance.special_method()
    

The author's point about avoiding unnecessary binding is well taken, but he
should refrain from declaiming on languages he doesn't know that well.

~~~
nkallen
Since you don't control the code that manufactures instances of MyClass.new,
you don't have an opportunity to manipulate the metaclass/singleton class of
instances of MyClass. So this technique doesn't work.

~~~
mr_eel
The basic idea is adding methods to single instances. It _does_ work. What
you're talking about is sugar. You could easily create a factory which
manipulates instances as they are created.

~~~
nkallen
Yes but you still need to inject the Factory. My point is not that Java is
better than Ruby but that this style, which is commonly associated with Java,
is better in any language.

~~~
nostrademons
Parameterizing behavior is usually a good thing in any language, yes...

The part that Python/Ruby got right is that their culture says _don't
parameterize until you have to_. While Java culture tends to build in
generality for the sake of generality, whether or not the actual system needs
it. Many systems don't, but if you use typical Java libraries, you need to pay
the costs for it regardless.

There was a red-flag in one of your comments above: "since you don't control
the code, this technique doesn't work". If you don't control the code enough
to make necessary modifications, you have a cultural problem, not a technical
problem. Fix the culture, not the code. You should be building the simplest
system that possibly works, not hacking around code ownership restrictions.

Parameterization is useful when you have one piece of code that you know needs
to work in multiple contexts, not when you're trying to make sure a piece of
code will work in all possible contexts a priori.

------
panic
This article has nothing to do with Java. You can write modular code in any
decent language. In fact, I would argue that the author's approach would work
better in a more dynamic language.

------
orangecat
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.

~~~
pyre
True 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
anonymous 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");
        }
      });

------
wooster
That's Scala, not Java.

And this doesn't even begin to cover everything I dislike about Java.

~~~
bjclark
Fine sir, I believe you have missed the point.

~~~
slpsys
Disagree. There's 'boilerplate' introduced by the framework/pattern, which may
be necessitated when you throw out default assumptions to scale (which I,
wooster, the OP, and yourself probably all agree are not so cumbersome after
all). Then there's boilerplate introduced by the language. That's where
wooster diverges, and I agree.

------
johnwatson11218
I was a little confused by the fact that the post lists Scala as one of the
"hipster" languages in the opening paragraph and then starts using it as an
example of the java way? Just a slip?

~~~
plinkplonk
"I was a little confused by the fact that the post lists scala as one of the
"hipster" languages in the opening paragraph and then starts using it as an
example of the java way? "

Well once you define the "java way" to be "build highly configurable code by
providing a way to combine small granular pieces of code into larger chunks of
functionality" (which is just Programming 101 , See SICP for e.g) it probably
still makes sense to write the "java way" programs in Scala ;-)

Makes sense to me! :-P

As nostrademons says elsewhere on this thread ,"Parameterizing behavior is
usually a good thing in any language, yes..."

------
ighost
Writing Java apologetics with using Scala sounded like a great idea at
first...

------
theashworld
I think folks who complain about java must make sure that they are using a
very powerful ide first (example: eclipse or intellij idea) If, having used
and mastered a good ide and all it's features, you still don't like it, it's a
fair point.

Eclipse can make so many things in java drastically fast and easy. And I think
that's where java shines. I'm yet to see as powerful an IDE for any other
language. (Although I admit, I do not program for msft platforms and hence,
never used msft ides)

~~~
jrockway
Here's a Java IDE question: why doesn't Eclipse have a REPL in the default
install? That's the one thing I use most in other languages. It's nice to be
able to right click to rename things (although a perl oneliner works just as
well in practice), but it's nicer to be able to know that your mini-algorithm
works before you design a class around it. Why is the focus on "rename" and
"extract method" instead of "write code that actually works"?

~~~
guelo
Instead of writing throw-away code in your REPL you can write a unit test
around your function and you get a permanent unit test as a bonus.

~~~
jrockway
I would generally say the same thing, but most of the experimentation I do in
the REPL is "is this crazy thing possible", not "is f(4) = 3.23432198"?

And sometimes it's just not worth testing, because it's something like ``hmm,
I wonder if 'intercalate ", " ["foo", "bar", "baz"]' returns 'foo, bar, baz';
ahh yes, it does.'' The only reason to test that is because you want your test
suite to run slower.

------
cageface
Abstraction is essential in building complex systems but it comes at a price.
Every layer increases the overall complexity of the code base and increases
the cost of maintaining and extending it.

Ideally languages and tools would allow you to begin with concrete & explicit
code and gradually introduce increasing layers of abstraction as necessary.
The problem I have with most Java frameworks is that they assume you're going
to need a lot more abstraction than you likely need, so just getting an idea
off the ground takes a lot of extra work.

It's really just worse is better all over again. The reason things like Ruby
and Rails are so popular is that they let you toss together something that
"works" so much faster. It's a lot easier to rewrite a good first-mover app
than it is to try to hoist all that Java baggage into a new problem space you
don't understand.

Come to think of it, isn't this exactly what Twitter did? If Twitter had been
written as the article suggests would it have ever taken off?

------
grandalf
It's interesting to consider that Ruby blocks are essentially function
composition with an anonymous function.

------
jorsh
"If you’re one of those hipster programmers who loves Clojure, Ruby, Scala,
Erlang, or whatever, you probably deeply loathe Java and all of its giant
configuration files and bloated APIs of AbstractFactoryFactoryInterfaces."

Can we just get a moratorium on usage of "hipster" now? Everybody's using it
wrong.

