Friday, December 3, 2010

Is Java suitable for today’s web-development?

I know it’s a provocative subject and I might get flamed for it but I have to share my thoughts on the suitability of Java for today’s web development.

As always, the applications are developed to solve business or other human problems. Therefore the closer the application implementation is to the domain of the problem – the faster and better the application development is. If you’re programming bank application you want to operate with bank accounts, not object arrays. If you’re developing gaming application you want to develop players profiles and game rules, not low-level 3d rendering.

If you’re developing a web-application, for example facebook app, you want to deal with RESTful Facebook Graph API, with social features, and at least with somewhat interactive pages. What you don’t want to do is to deal with low-level plumbing like mechanics of reading the URL content. Such details are nothing but an accidencial complexity within the system.

A simple example:
Let’s say you need to read content of RESTful URL something like https://graph.facebook.com/me. All you really want is to get a string with the URL's content.
In Groovy it would look like this: “https://graph.facebook.com/me”.toURL().getText()

After spending few hours trying to find something similar in Java I have reached a conclusion that the best I can do is either:
  1. Use apache HttpClient and deal with hierarchy of its objects. This is a bit more than few lines of code.
  2. Try to find another obscure library.
  3. Write custom code like this:
public static String getText(String url) throws Exception {
        URL website = new URL(url);
        URLConnection connection = website.openConnection();
        BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                    connection.getInputStream()));

        StringBuilder response = new StringBuilder();
        String inputLine;
        while ((inputLine = in.readLine()) != null) 
            response.append(inputLine);
        in.close();
        return response.toString();
    }

Now, of course, I can write it and encapsulate it in some helper utility object etc.
But the problem is that this code has nothing to do with the problem that I’m trying to address in my application. It represents a much lower level of abstraction. At yet I’m forced to resort to spending time on researching, implementing, testing, maintaining such a basic functionality.

For comparison, Ruby/Groovy/Python developer does not need to know anything about BufferedReaders, InputStreams, using StringBuilder for loops etc. All he really needs to know is the URL, and the string where to put the content.

It might seem that I’m blowing this issue out of proportion but I think it exemplifies a much deeper problem. Java was invented and evolved in a different era. Today’s web applications use different and higher abstractions. This is normal cycle of development of computer languages. Each new generation abstracts low-level details of the previous generation allowing developers to focus more on their problem domain. 15 years ago java simplified development by allowing programmers not to care about pointer arithmetic, destructors and other C++ plumbing.

I know that Java is a generic language and it will continue to be used for a very long time. But guess what: C++, and shockingly even COBOL are still in use, but in only very narrow domains (embedded or low-level programming for C++ and ancient legacy systems for COBOL).

I suspect that Java is going in the same direction. Today I would be hard pressed to start development of a decent web-application in pure Java.

By the way – I had exact same argument about C++ and Java about 10 years ago with my former colleagues.

18 comments:

  1. Your article should have been named something like "What is the simplest way to execute a web/REST request in Java?". You've somehow generalized not knowing it to questioning suitability of Java for web development. This kinda kills the discussion from the start, doesn't it?

    ReplyDelete
  2. @Philopator - What you described is clearly a common use case. Why hasn't anyone released a framework/utility that helps with those use cases?

    Java's #1 strength is its large ecosystem, and the ease of adding to that ecosystem. While Java's founders may not have considered RESTful querying a priority, Java developers have the power to make it just as easy as Groovy or Scala.


    @murz - Play is intriguing, but I question their choice of Java syntax. Example: if Play is Java, why do they use "public" access modifiers on variables, then inject getters/setters in the middle?

    In other words, why does Play modify what Java is? Why not just have its own syntax, or use a syntax closer to home?

    ReplyDelete
  3. The basic use case here should be addressable via assignment of servlets to those URLs via you're web.xml descriptor. Refer this article for further details...
    http://onjava.com/pub/a/onjava/2005/04/27/restweb.html

    There are also a wide variety of frameworks to support this, Googles first hit would be...
    http://www.restlet.org/

    ReplyDelete
  4. I think that today Java should not only be seen as a programming language but as an execution environment. With the ability to use AspectJ, Groovy, JRuby, Jython and others with Java, Java offers a lot of possibilities. Don't be afraid to mix Java with other languages.

    ReplyDelete
  5. You should look into the Jersey client (which, btw, will probably serve, at least in part, as the basis for standardization in JAX-RS 2.0):

    String text = Client.create().resource("https://graph.facebook.com/me").get(String.class);

    Easy peasy.

    ReplyDelete
  6. This completely disregards the discussion about performance. You can see some fairly good comparisons over at the "Computer Language Benchmark Game" for example: http://shootout.alioth.debian.org/u32/benchmark.php?test=fasta&lang=all

    For this particular benchmark Java 6 run with a -server flag runs it in 1.74 seconds where Ruby 1.9 runs it in 223.01 seconds. JRuby is 5 minutes. PHP is 13 minutes. You are sacrificing significant performance in some cases based on language choice.

    ReplyDelete
  7. Dave wrote: "You are sacrificing significant performance in some cases based on language choice."

    There truly is nothing new under the sun. This performance argument was the main object to moving from C++ to Java.

    ReplyDelete
  8. Slava Imeshev wrote: "You've somehow generalized not knowing it to questioning suitability of Java for web development. This kinda kills the discussion from the start, doesn't it?"

    I don't need to know some specialized libraries or techniques for this basic operation in Ruby/Groovy/PHP/Python. Why should i have to know it in Java.

    ReplyDelete
  9. littlezopper wrote: "You should look into the Jersey client"

    I certainly can do that but it's not standardized yet and, once again, i have to know and depend on some other library for such basic functionality.

    ReplyDelete
  10. Python/PHP/Groovy/Ruby follow a completely different aproach. They "overgrow" they standard libraries. Java could follow this path but as it stands now it would also grow the size of the JDK and probably increase the startup time of the JDK. Bloat is still a massive argument against Java so I don't think Oracle will follow this path. Given the wast landscape of 3rd party libraries (Apache commons keeps most typical use cases covered) it's not a very valid argument agains Java as a base for webapps. I can assure You that performance is a valid argument.

    ReplyDelete
  11. Philopator wrote: "I certainly can do that but it's not standardized yet and, once again, i have to know and depend on some other library for such basic functionality."

    Sure, which I don't have a problem with, but even once it's standardized, it will be a Java EE standard, and not the Java SE to which I can only guess you mean. I kind of like having that kind of thing in a library rather than in the core language, as the language itself can be smaller (yes, I know I'm talking about Java :)

    Are certain dyna-langs better suited in some ways for web dev than Java? Maybe, but they don't add enough to offset the downside (especially in terms of performance) to make it suitable for many large-scale applications. Throw in clustering, etc that you get from modern Java EE environments, and I'm pretty content (well, content enough, for the time being) with the choices I have in Javaland.

    Different strokes, I guess, but my answer to the question in title is, for me, without question, "Yes." :)

    ReplyDelete
  12. Nice sharing, its really very useful for me, Java Server Pages, which is enhanced known as JSP, is an additional open-source programming language that can be consummate without even knowing Java Script. Thanks for sharing this.
    micheal

    ReplyDelete
  13. This is nice blog…
    The Web Development Chicago process and helps related to SEO and other Development according to Business requirement.

    ReplyDelete
  14. You already know therefore significantly when it comes to this topic, made me in my opinion believe it from numerous various angles. Its like men and women aren’t involved until it is one thing to do with Girl gaga! Your individual stuffs outstanding. Always take care of it up!
    How I can Create YouTube Channel?

    ReplyDelete
  15. This comment has been removed by the author.

    ReplyDelete
  16. This comment has been removed by the author.

    ReplyDelete
  17. Digital marketing service
    is important whether website is build in Java or PHP. Java is good but I prefer PHP for ecommerce website.

    ReplyDelete