Another language running on the VM which is worthwhile to check out: Fan.
Jazoon: The Closures Controversy
26. June, 2008If you ever wanted to know if it is possible to go through 60 information-packed slides in 50 minutes: Yes, it is. I’ve been there and Joshua pulled it off in from of about 100 people, so it’s not a delusion of mine, either.
In his talk, Joshua presented a host of reasons why the BGGA proposal is a really bad idea. The key information here is “BGGA”. We all want closures and Joshua is no exception, it’s just that the BGGA proposal is like Generics on steroids and if you can’t wrap your brain about Generics wildcards, then you won’t understand BGGA closures as well.
The code in the proposal doesn’t look too bad at first glance:
{int x, int y => x+y}
Unless you try to use an array. If you did “int[] x”, you’d get an error because closures are based on generics and generics can’t handle arrays or primitives.
But the next example is better to understand why BGGA will add a new level of hell to Java which will be worse than generics:
{int,String=>Number throws IOException} xyzzy;
is translated into
interface Function1 { // system-generated
R invoke(int x1, A2 x2) throws E;
}
Function1 xyzzy;
Doesn’t bother you because you’re never going to see this? Well, think again because if you try something that the generics type system doesn’t understand, you’ll get a generics error message like this one:
NewtonWithClosures.java:26: invoke(capture#418 of ? super {double => double}) in {capture#418 of ? super {double => double} => capture#928 of ? extends {double => double}} cannot be applied to (double)
The reason for this is that the closures are implemented using generics and without any high level support in the compiler so the compiler can’t generate a more useful error message. And this was a simple example. We all know how quickly generics get messy and for me, that means that any implementation of any new feature that is based on generics is a threat to the future of Java.
Again, I’m for closures and I use them as often as I can in Groovy but the current proposal is just a new way to make Java harder to use. Why don’t they simply change the compiler to allow to access field and method objects (from java.lang.reflect) via the class? Like so:
collect(list, Math.class.min)
If we could use Method objects like first class citizens (instead of via the horrible reflection API with it’s horde of checked exceptions), we could use Method objects as simple closures. Then, all we would need is a set of util classes for single mutable primitives and we’re done. Sure, the syntax wouldn’t be as compact but every normal Java developer would be able to understand the concept and how it’s supposed to be applied in a few hours.
If you care, here is a link to the whole set of arguments by Joshua Bloch. Read it, keeping in mind that Joshua is pro closure, he just wants to avoid a second generics debacle.
Jazzon: Weblog in 15 minutes II
26. June, 2008I wish I could link that presentation because it was the best I’ve seen in the whole show, both visually and how it was presented. So here is the link to the abstract. There is something to be learned by this talk for anyone who wants to demo a software: One human being can’t talk and use a computer at the same time. While David explained things, Bertrand wrote the code. Thumbs up!
And in this case, the visual candy didn’t distract from the fact that these guys were really showing off something that ought to have an impact. They showed agile web development with Apache Sling.
A sling gives you range and power and Apache Sling does just that. With just a few pieces of JavaScript added to an existing static HTML web site, they built a blog in roughly 10 minutes. Impressive. I’ve got to try this out myself, probably this weekend.
Jazoon: Thursday Keynote
26. June, 2008Thursday’s keynote was “Effective Java Reloaded” by Joshua Bloch where he presented a few key points from his new book.
The examples showed how to replace bit fields (or rather int constants which are used a bit fields) with EnumSet and EnumMap, and how to do lazy initialization for various cases effectively and correctly. If you’re a Java programmer who does more than “Hello World”, this book is a must have because it explains not only how to do things but how to do things elegantly and why.
prefuse – Information Visualization Toolkit
20. June, 2008The Best Java Tools You Never Knew Existed
20. May, 2008Jakub Korab posted a list of neat Java tools for about every purpose in his blog.
Complexity Budget
8. May, 2008Are you a human? If not, then this is probably not for you.
If you are, then you have a “complexity budget“. I define it a little bit differently than the author of the article. My definition is that you can spend only so much on understanding something. Example. What does this little C program do?
_(__,___,____){___/__1&&___%__<___/__?_(__,1+
___,____+!(___/__%(___%__))):___<__*__?_(__,___+1,____):0;}main(){_(100,0,0);}
It prints the primes below 100. And it blows the complexity budget. C++ also often blows the complexity budget. A friend of mine once said: “To understand C++, you have to be a C++ compiler.”
Java once was a simple language but the wise guys with a sun-burn (from being exposed too long to the sun, get it?) decided to do something about it and came up with Generics. And since they weren’t sure that this would indeed make the language too complex, they added annotations on top of that. Excellent move!
But it didn’t work. There are still too many people who use and understand Java. So they came up with JSR 308 which allows you to use annotations in even more places to write code like this (from this article):
@NotEmpty List strings
= new ArrayList();
If that doesn’t do it, nothing will.
On a positive side note, in Groovy, I can not only register an annotation processor (AP) in the compiler, I can even manipulate the AST from the AP, allowing to create code like this:
@GroovySQL(type=DemoType.class)
def load(long id) {
return """select * from demo_table where id = ${id}""";
}
That doesn’t return a string but an object of type DemoType filled with the data from the database. Life can be so simple.
And to the guys from Sun: Thanks a lot! We really appreciate your help! 🙂
Wrapping Reflection with FEST-Reflect
6. February, 2008Tired of handling all those pesky exceptions in Java Reflection? I’ve patched commons-beanutils to convert them into RuntimeExceptions but the patch was rejected because it’s a breaking change. Now, you can try FEST Reflection.
FEST Reflection has a nice and small API with an emphasis on making it easy to produce readable code.
Links: Found on DZone Javalobby
Safer Java: Think Positive
2. February, 2008If you’ve ever developed software for X11, you probably stumbled over “unmap”. For everyone else: If you wanted to make something in a X11 user interface visible, you’d set “unmap” to “false”. A great way to interrupt work because we’re not used to negative thinking. If I want to make something visible, I think “I set some attribute to true” not the other way round.
In Java, we have learned but some API still smells. Collection.isEmpty(), for example. With positive thinking, I think of collections with elements as “better” than one without, so I’d prefer Collection.hasElements() even though it’s longer and boolean properties should use “is” instead of “has”. Oh well. But it’s simple to fall for the negative trap in your daily work as well.
If you find yourself stumble mentally when using a boolean API, refactor and turn it positive. Modern IDEs make this simple and you’ll write more error-free code in less time. commons-lang offers isBlank() and isNotBlank for Strings which is a good example because you don’t have to put a “!” negator in your if’s no matter if blank is good or bad in your specific case and the JIT will inline the code anyway, so there isn’t even a speed penalty (only a LOC penalty).
Safer Java: No Pseudo Constants
24. January, 2008Even the pros do it: String “constants” i.e. a string literal (that is something between ” quotes) used as a “constant”.
That’s almost as bad as using integer values as arguments instead of defining symbolic names for them! I mean, the values passed into this API are defined by the standard, aren’t they? Sure, you can pass additional feature names as well but that doesn’t stop anyone from defining those which the standard defines somewhere! So what that not all of them must be supported!
There are several reasons why you should always define string constants when your API need strings as parts of it’s “configuration”:
- It makes it much more simple to find possible values for your API.
- Auto completion in an IDE is possible and helps to avoid typos.
- You can add JavaDoc to your strings explaining what they do.
- Modern IDEs can find out where your strings are being used in the project.
- You can rename the constant and change the string value, even if you have several constants with the same value!
- Even if the literals are read from a file, still define a constant for the string and map the raw string to the constant as soon as possible. It makes it so much more simple to track!
Tip: If you still use Java 1.4, define your string constants in an interface (without any other methods). That allows you to use “implements” anywhere where you want to use the constant names without having to prefix them with the name of the interface:
public interface Constants {
public final static String A = "a";
}
class Demo implements Constants {
private String value = A;
}
With Java 5, you’ll use enum, of course (especially because you can then use these “strings” in switch statements.
Posted by digulla