Sunday, November 23, 2008

Do Java Web Application Frameworks *Really* Provide Too Much?

The other day we witnessed a DZone disaster. The article (Web Frameworks - We Need Less, But They Keep Adding More (Part 1)) was a five-paragraph claim that typical/most Java web frameworks were full of bloat and too complex, and that this is bad. I don't think anybody would argue that. The disaster's underpinnings? He was voted down, and called the downvoter a jerk.

I also voted the article down--I didn't like the article. IMO that's what the voting system is for. I was also called a jerk, and great hilarity ensued. Yes, I should have stopped feeding the troll, and I take full responsibility for my end of the discussion. I still, however, don't like the article, and believe his reaction was about as ridiculous as I've ever seen.

Are Java web frameworks actually "adding more"? Does "doing a simple task requires pages of code and configuration..." and do current frameworks want "... control of both sides of the web application coding (server and browser)"? Do "... many/most web frameworks is the fact that they try to do too much and require large amounts of configuration (usually with XML)"?

The popularity of Ruby on Rails prodded most Java-based web frameworks to move, often rapidly, *away* from static configuration. Some have moved towards annotations (still configuration), some towards convention-over-configuration, many allow multiple configuration (and configuration-less) styles. Even the annotation-based solutions are, at this point, pretty minimal in terms of cognitive overhead (which is the ultimate issue), although annotations are still configuration. Most frameworks, even *with* XML configuration, allow convention-style configuration via wildcards and the like, often allowing large web applications to be configured, even in XML, with only a few lines.

Do frameworks "force me to use your embedded code and/or tags in my HTML"? I know of very few that *force* us to use framework tags, although it's often unreasonable not to. The article is basically a commercial for jWebApp, and several jWebApp examples use jWebApp-specific tags. The explanation given in the DZone meltdown is "The tags are framework independent and optional, but are obviously needed to repopulate the form if validation fails." I'll largely discount the entire tag issue, then, if they're obviously needed for functionality normally associated with a web application. I'll admit a certain amount of confusion as to why it was brought up, however.

The second article begins the actual technical discussion, using the "perfect web framework" (which turns out to be jWebApp). It includes code and configuration (which we didn't want, if I recall) samples as well as a laundry list of features.

The example request handler skeleton given is this (slightly reformatted and redacted):

public class Customer extends RequestHandler {
public String validateSaveCustomer(ServerInterface serverInterface) { ... }
public String processSaveCustomer(ServerInterface serverInterface) { ... }
public String processGetCustomer(ServerInterface serverInterface) { ... }
}

ServerInterface is a wrapper around an HttpServletRequest, HttpServletResponse, and ServletContext, directly tying request handlers to the servlet framework. The wrapper class also defines a slew of convenience methods that access things like request objects and so on.

Directly tying to the servlet spec creates an awkward dependency that unnecessarily complicates testing. These days it's relatively unusual to find a framework with such an explicit dependency on the servlet specification. If time has taught us anything it's that dependencies like this are (largely) unnecessary and impede development.

The Hello World example on the jWebApp site shows an example with a similar skeleton:

public class HelloWorld extends RequestHandler {
public String processHello(ServerInterface serverInterface) { ... }
public String processHelloAgain(ServerInterface serverInterface) { ... }
}

This example is intended to service the /helloWorld/hello and /helloWorld/helloAgain URLs. The "configuration-less" configuration, however, looks like this:


jwaRequestServlet
jwebapp.RequestServlet



jwaRequestServlet
/helloWorld/hello



jwaRequestServlet
/helloWorld/helloAgain


The first mapping is reasonable, the jwaRequestServlet is acting as a controller. What's less clear to me is why the next two servlet mappings are necessary: they seem redundant and unnecessary. Again, in modern Java frameworks this would be handled automatically via convention, wildcarding (XML or annotation configuration), or classpath-scanning convention (configuration-free or annotations).

Note that as with most other modern Java frameworks it's possible to use convention-based URLs in jWebApp, but we're then back to comparing apples to apples.

The article then goes on to say that he would like to "optionally define simple configuration":









This brings us back into the realm of a typical XML-configured framework, which seems to go against the grain of part one, which makes me wonder what the point was in the first place. At the end of part two is a laundry list of features desired in "the perfect framework", but none of them seem to be features that are particularly lacking in other modern frameworks.

Some features present in jWebApp *are* nice to have, and it's cool they're integrated into jWebApp--but those features aren't necessarily features that *should* be integrated into a web application framework. If they meet the needs of the developer and/or project then that's a Good Thing, and a definite win. Some of the features, however, like emailing, payment processing (I only found support for PayPal, but I suspect there's more), and so on may be so project- or environment-specific that having them included in the framework itself is at best useless, at worst an unnecessary distraction. It may not be *fundamentally* bad they're included, but directly integrating cross-cutting concerns like that always raises my eyebrow.

Moving away from the articles and towards the framework itself there are several claims made regarding the features of jWebApp. The lack of need for custom tags is mentioned (no *need*, but again--they're used by the framework to provide the same functionality provided by most other frameworks' tags). Easy AJAX support is touted, but appears to be handled by creating a JSP page that returns JSON-formatted data which is then evaled in JavaScript, rather than the automatic JSON de/serialization offered by other frameworks (often including automatic help in avoiding scripting attacks).

Something not mentioned in the jWebApp documentation is Spring integration, which for the types of applications I write is a very important requirement: yes, Spring *can* be complicated and it's definitely overkill for many types of applications--but those aren't the applications I write. If all we need is dependency injection (something I didn't find support for in jWebApp--again, that doesn't mean it isn't there) then Spring is a pretty heavy solution--there are much lighter-weight DI containers. Once we throw in transaction management, AOP, and so on, I find out-of-the-box Spring support compelling, although there are other good solutions.

Simply put, I don't find anything compelling about jWebApp. That doesn't mean it's not an adequate for some problem domains or developers--it simply doesn't meet my needs. For quick, one-off, Java-based e-commerce apps it may be exactly what somebody is looking for, since it has typical functionality built-in.

The bottom line is that I still don't find the article I voted down interesting, particularly well-written (although it's not *badly* written), or of any great value. It was five paragraphs that didn't say very much. I feel badly that the author reacted so poorly--quit frankly if I'd known he'd fly off the handle like that I probably wouldn't have bothered voting it down, but part of the point of voting is to provide feedback to the community at large, and at the time I read it I didn't think it provided enough value to recommend it to others, and it seems to ignore the Java web app frameworks that alleviate the bulk of what's being complained about.

On the social aspects of the original DZone submissions, I'm somewhat at a loss. The author decries the childishness of "arbitrary" downvoting, then downvotes *multiple* submissions by his downvoters in retaliation... We may just be operating under different definitions of "childish", but IMO that's even *more* childish. It appears that the tantrums brought nothing more than *more* "arbitrary" downvoting. Is this a Good Thing? No, he's right that downvoting just because he played the fool isn't appropriate behavior: the only thing that should enter in to submission voting is the article itself. His playing tit-for-tat, however, completely demolishes any credibility he has to complain about arbitrary downvoting. I was called a jerk because I didn't like the article--this is also inappropriate behavior; not liking the article *is* an appropriate reason to vote it down.

Lesson(s) learned? For my part, I'm less likely to downvote an article I think has no redeeming value, depriving other readers of a reasoned opinion. On his part? I don't know--I'd like to think he'll come out the wiser, and I hope he does. I'm glad he's developed a framework that meets his needs, and if it meets the needs of others, I think that's terrific; more power to everybody involved. Despite his sarcastic parting comment: "Thank you all! You've shown me what the open-source community and netizenship is all about." (it was originally followed by a "Fuck off", but that part was deleted, which is good) I actually *do* believe this is what the open-source community and netizenship is all about: open discussion that sometimes happens to go horribly, horribly wrong.

Friday, February 01, 2008

Today's Java Irritant: No sense of closure.

There's still enough back-and-forth about the inclusion of closures in Java 7 that I'm nervous it might not make it in to the language.

James Gosling supports the addition of closures and states that the reason they weren't there in the first place was due to time pressures and seems to have some regret about their absence (understandable; he's a Pretty Smart Dude and probably feels the pain of Java wartage more keenly than most).

Ricky Clarkson blogs about why we need a new syntactic construct to make the use of closures cleaner and less verbose, even while admitting that Java does, in fact, have a form of support for closures already.

My languages of choice (Lisp, Smalltalk, Ruby, etc.) all have cleaner support for closures without the syntactic overhead that current Java has. For a trivial example we can compare the use of closures for processing a file.

file.each_line do |line|
# Process line of file.
end

With current Java syntax we might end up with something like this:

import static bar.baz.eachLine;
...
eachLine(file, new EachLine() {
void doLine(String line) {
// Process line of file.
}
});

We could also create a File-like object with an eachLine method.

This isn't the Worst Syntax in the World, but it still makes me think more when writing the code and when I'm reading it later.

A relatively minor point is that any variable used in the closure must be declared as final in the enclosing method. This may obfuscate the actual functionality of the enclosing method and, in some cases, require the introduction of a final variable just to satisfy the requirements of Java's current "closure" syntax/implementation.

It'll be interesting to see what finally happens; I hope that (a) support for closures is added and (b) it doesn't irritate me too much.

Monday, December 31, 2007

Today's Java Irritant: Design-by-Contract Disconnects.

Design-by-contract systems enforce API behavior. In Eiffel, contracts specify both internal and external behavior, at the language level.
connect_to_server (server: SOCKET)
require
server /= Void and then server.address /= Void
-- etc.
end

This does what it looks like, at runtime. (/= == != :)

The cost of doing the same in Java is higher; most frameworks use some form of aspect-oriented programming. This isn't a bad thing in itself, but gives some people (managerial types and unimaginative programmers) the heebie-jeebies. Dealing with issues created by lots of aspects can be daunting, but are relatively straight-forward in a well-educated Java house.

Lighter-weight solutions exist, may be preferable in many situations, and are easy to grok. Putting a chunk of precondition tests at the start of a method is trivial:
public String foo(final String bar_) {
Pre.notBlank(bar_);
// etc...
}
Here, the issue is that there's no automagic way to track or document preconditions other than either manually documenting them in Javadocs (preferably through a simple doclet tag, like @pre, or whatever) , or by programmatically scanning the source and pulling out "useful" information and doing something useful with it. Neither are particularly appealing: Javadocs go stale quickly, and writing a robust source parser is non-trivial.

Another answer is to use one of the annotation- and/or aspect-based solutions (or XDoclets, but... ew?) and swallow the bitter pill that is Java, and pay the cost of educating your developers. The packages I'm currently considering are Contract4J, which uses pre-built AspectJ aspects, and SpringContracts (which is nice since most my projects use Spring).

Wednesday, December 19, 2007

OLPC: Minimal First Impressions

Hooray, my OLPC arrived this morning!

Without having the time to go in to any detail, here some initial first impressions.

- It's really small, but the carrying handle and LCD hinge/swivel assembly make it larger than my Eee PC. Not a huge issue for me, and the hinge/swivel makes for some cool functionality that I want in a small notebook. Handle I can live without, but it won't be a deciding factor for me. Tie.

- It's marginally heavier than the Asus. Subjectively I prefer holding the Asus in one hand; the weight distribution seems better/more convenient. Winner: Asus. UPDATE: I just found a nice way to hold the OLPC; if you wrap your arm around it you can rest the back end on your forearm while the front rests on your chest. It's a little awkward, but the additional depth makes it a bit more convenient than the Asus (I think). NEW WINNER: Tie.

- You've seen the keyboard, you don't need me to add anything, right? Fine. The Asus keyboard is obviously better for most of what I need to do. That said, I'd be more comfortable taking the OLPC into harsher environments, which I need on occasion. Winner: Asus, with caveats that make it seem more like a tie.

- Boot-up time is acceptable, but slower than the Asus. Again, this won't be a major deciding factor, but a quick boot-up is rather important to me: the quicker it starts up, the quicker I can do something useful, the more likely I'll be to make use of the functionality I can hold in my hand. Asus (20-25 seconds worst case), OLPC (90+seconds?!). Winner: Asus, hands-down.

- The OLPC screen is pretty nice, and the no-backlight mode is really easy to read in bright light, and passable in low-light. The Asus screen is good too, but (so far, anyway) I haven't been able to turn the backlight off. Winner: OLPC.

- Apps: The Asus is a typical Linux box. The OLPC as delivered isn't set up for non-kid use, but there's a shell available, vi is installed by default, and I'm assuming I'll be able to use it as a generic Unix box w/o much of a hassle. I could be wrong. As delivered, winner: Asus. In reality, most likely dead even.

- Speed: Asus, no contest, at least so far. The OLPC is running a Geode at 433MHz, the Asus a Celeron M (at 630MHz?). I'm surprised the perceived speed difference is so great, and I'm not sure where the biggest differences are. Hopefully someone with more time will post some I/O benchmarks.

- Battery life: Don't know yet. I'm guessing the OLPC, perhaps even by a landslide. The Asus will go ~3 hrs, depending on what you're doing. I'd rather it was... a full day :)

- Noise: OLPC, hands down. The Asus gets pretty hot and has a fan and isn't afraid to use it.

- Networking: So far, I can't get the OLPC to connect to my WPA-PSK wireless; this is a MAJOR lose. I'm sure somebody else has this figured out, though. Temporary winner: Asus. UPDATE: Heng's comment does, indeed, solve the problem. New winner: Tie.

First Final Verdict: if only they could combine the two... I may end up going to dynamism.com and getting a UMPC at this rate, but they cost 2-10x as much, so I'm in no hurry. I suspect I'll be using the Asus more because of the keyboard, but once I have time to start hacking on the OLPC (and mod the case) that might change.

I'm excited by both, which is an unfortunate commentary on my life. This *is* a useful form factor (at least for me, and, I suspect, a lot of other people) but I'm not sure the optimal ergonomics have been figured out yet, and, as usual, we still need better batteries.

Thursday, November 29, 2007

Asus Eee PC: First Impressions

Good heavens, it's small, and I got the one with the whiny fan.

The screen, while low on density, is easily readable indoors, and adequate for my needs. The LED backlight is adjustable via Fn-F3/Fn-F4. It goes to sleep via Fn-F1 in about 3 seconds and comes back with the power switch in about 5 seconds.

Typing is... challenging, although I suspect I'll adapt with a few days use... So far the only stumpers have been the right-hand shift key (it's small, and I often hit the up-arrow by mistake), the number keys, which are shifted a half-key or so to the left, and re-training the fingers to not track as far--the keys are quite close together.

Ctrl-Alt-T brings up xterm; I replaced that by editing my ~/.icewm/keys file to use konsole:
key "Ctrl-Alt-t" konsole

Setting up virtual desktops was also straight-forward; I edited ~/.icewm/preferences to include them, and a start menu:
TaskBarShowStartMenu=1
ShowProgramsMenu=1
TaskBarShowWorkspaces=1
WorkspaceNames="1", "2"

Putting the unit to sleep seems to mess up wireless connections; it's irritating--hopefully I'll figure out why that's happening so I don't have to keep typing in my ridiculous wireless password (it supports WPA, which is handy).

A real desktop can be had by installing kicker:
apt-get install kicker
apt-get install ksmserver

I had some issues with the task bar in full desktop mode that haven't been resolved yet; it redraws incorrectly when there's more tasks than taskbar. I may actually stick to "simple" mode for the time being.

While some have panned OpenOffice.org's performance, it's not that bad. I probably won't use it very often, but it's nice to know it's there. AbiWord is another option. VIM comes pre-installed... while I'm an Emacs person, I'm a little scared to even try it (I dread an even more-cramped Emacs claw), so I'll probably stick with something else on the little guy.

When my OLPC arrives I'll be comparing the two to see which will be my "drag it around" machine--the Asus is light but I suspect not as robust as the OLPC, and I really like the OLPC's screen.

For now the Asus will serve as a note-taking little-bits-of-work machine used during commutes. Battery life will be an issue; my MacBook Pro 17" Hi-Res will go round-trip w/o recharging, about 2.5-3 hrs, doing word processing and Java web development. The Asus is claimed to last 3.5 hrs and I doubt I'll be doing any Java development on it, but I may be able to continue using OOo for some books-in-progress.

It will also make an interesting hackery platform.

Monday, November 26, 2007

Today's Java Irritant: Java's Impoverished Mixology

Today's Java Irritant is the lack of mixins or similar functionality, although Warth et al.: Expanders have an implementation.

A current project has an interface consisting of about two dozen getters (and setters). This interface is implemented by a minimum of two classes due to design and lack of multiple inheritance.

Being forced to implement this functionality in even one place, not tucked away in some base class, module, mixin, etc. is irritating enough: having it in two places is more than twice as bad. Keeping the implementations in sync is irritating. Looking at the code so I know I can ignore it is irritating. Maintaining three sets of code (the interface and two classes) is irritating.

This functionality is available for Java--sort of. Warth et al. created Expanders via the Polyglot compiler front end. It allows classes to be non-invasively updated with new methods, fields, and superinterfaces.

Expanders look like what we want, and like the rest of Java, are statically-typed, preventing at least some types of errors.
package some.pkg;

public class SomeJavaClass {
// Normal class definition.
}

...

package interfaces;

public expander Foo of SomeJavaClass {

private String _someProp;

public void setSomeProp(final String someProp_) {
_someProp = someProp_;
}

public String getSomeProp() {
return _someProp;
}
}

...

import some.pkg;
use interfaces.Foo;

public class UseThatShiznit {

public static void main(final String[] args) {
SomeJavaClass anInstance = new SomeJavaClass();
anInstance.setSomeProperty("No brainer.");
}

}

This is a crude use of Expanders. Consider the creation of a Swing JTree-aware class via Expanders: rather than include Swing-specific information in the class itself we can create an Expander that implements TreeNode and ILabelProvider and since Expanders are typed, we can create different expanders for different classes. If there is shared behaviors, no worries; Expander behavior can be overridden just like class behavior.
use StringIconExp;
public expander PublicationExp of Publication implements TreeNode, ILabelProvider {
// Enumeration children()...
// String getText()...
public Icon getIcon() {
return "/icons/publication.gif".getIcon();
}
}

What's that getIcon() code doing?! Strings don't have a getIcon() method... but strings with an expander do.
public expander StringIconExp of String {
private Icon icon = null;
public Icon getIcon() {
if (icon == null)
icon = new ImageIcon(Object.class.getResource(this));
return icon;
}
}

Yes, this could be implemented with source generation tools, but that's a poor substitute for type-safe, naturally-composable functionality in the language itself, and it adds a layer of build-time complexity that must be documented and maintained.

If you think Groovy, Scala, and so on are tough sells, try selling Expanders. It's tantalizing, but experimental, and with the current crop of JVM languages, I doubt we'll be seeing it in mainline Java.