4 ms·
Any time I see someone complaining about how much effort it is to "just print a string to std out in java" it boggles my mind. Java was not really designed for
by Deinumite 15y ago
Any time I see someone complaining about how much effort it is to "just print a string to std out in java" it boggles my mind.
Java was not really designed for ease of use in that way, clearly, and the syntax and vm itself heavily lends it to networked programming.
This post would be better if it was "Why I love Common Lisp" with the half hearted stabs at Java taken out of it.
- koko775 15y agoSeriously. I've been doing Java for years and it had been so long since I wrote public static void main etc. that I had to look it up. If you're writing mostly classes to be run straight-up from the command line, you're doing it wrong.
- Stormbringer 15y agoActually, running the class straight from the command line can be a really cool way of doing ad-hoc unit testing. You can have a main method that instatiates an instance of the class, populates it with whatever it needs (including mocks if you're into that sort of thing) and then exercises the various methods of the object. It's like IoC but without all the bread and circuses. NB: it's been about 8-10 years since I did anything like that. Running from the command line is good for something that can run headless but you want to see immediate success/failure type messages. The other main use I had for programs run from the command line was small 'one-off' file format conversion programs. Once you've done half a dozen of these it's relatively easy to just bang out the ~100 lines of code to do this (including proper exception handling). Would it be faster to write it out in perl? Well, if the regex is sufficiently simple then yes, if it is moderately complex then you still use far fewer keystrokes, but getting it to the point that it is correct, that's easier in Java. (Assuming you optimise for readability, something perl... struggles ... with)
- gruseom 15y agoActually, writing code that can be called directly from a prompt is the essence of using a REPL. The fact that Java, and OO programming generally, makes that so hard is a major weakness.
- nessus42 15y agoI write Java programs that are designed to be run on the command line all the time. Just what is it that I am supposedly doing wrong? (That's not to say that most of the classes are to be run from the command line, but each CLI program has to have a class with such a method.)
- 6ren 15y agoSpeaking for CLI utilities (not apps in general), startup time is a killer. Python, Ruby, Perl etc are so much faster (despite Java improvements) since they began as "scripting" languages. One solution is to reuse a warmed-up JVM (even faster if also reusing already-loaded classes): 1. use a Java REPL, like BeanShell or Groovy (or write one). 2. call a Java server from the CLI I did (2) for a while, but not worth the saving of a second or so. BTW: I find "you're doing it wrong" is so unreasoned that best ignored
- nessus42 15y agoAnd if you use Spring, then the start up time is even worse! I do wish the JVM had an option that would make it start up instantly, even if it meant that the program would run slower. I wonder if the slow startup time has to do with the JVM having to dig through .jar files to find stuff. I'm also surprised that there isn't a way to cache the machine code generated by the jit so that the machine code doesn't have to regenerated on every run. It seems very strange to me that Java was invented by a Unix company, and yet it seems rather Unix-hostile. For the stuff that I'm working on, the 1 second startup time is no biggie though. One CLI thing I wrote, for instance, crunched on genomics data for 3 minutes before finishing. By tuning tight loops and the like, I was able to get that down to 15 seconds, but even so, the extra second there is no biggie. Except for when the user uses the "--help" option. That extra second is tedious.
- 6ren 15y agoYeah, I'm puzzled by them not caching, e.g. they could copy an image of the JVM in its starting state. Maybe the "unix hostility" is due to platform agnosticism ("run everywhere"), servant of two (or three) masters. e.g. issues in file libraries and AWT. But py/rb/perl manage this OK... so idk. BTW: bad practice to split up functionality (and loses many benefits of Java), but a performance solution would be a script that handles the "--help", and for anything else starts the JVM.
- Jach 15y agoAdmittedly I skimmed the blog post but I saw a complaint against the "Hello World", not how to print out a string. I agree the "System.out.print" thing is such a non-issue. If it really bothers you, make a class in your package that's a wrapper around it so you can call Utl.print() or U.o() or however short you want to make it. Meanwhile the rest of us will get back to programming. In the few years I've done a lot of Java development it's never bothered me enough to even make a vim shortcut for it. However, the "Hello World" criticism does make sense to me in this sense. There are at least 18 separate facts you need to know to fully understand a Java "Hello World" program (including how to compile and run it). For [insert almost any other language (not C++)] there is considerably less. (Python is 4 facts.) If your goal is to teach someone who has never programmed before a new language, Java seems like a poor choice in that respect. In any case, Clojure is where it's at right now man. It's pure awesome-sauce on its own and you get all of Java's goodies as a bonus.