Geertjan is a DZone Zone Leader and has posted 466 posts at DZone. You can read more from them at their website. View Full User Profile

Judicious Java IDE Usage and the Road to Enlightenment

02.11.2008
| 7559 views |
  • submit to reddit

An often heard complaint in relation to the Java IDE's goes like this: "I want to do my own coding. I don't want stuff being generated for me. Firstly because I don't learn anything that way, except how to use the tool, which locks me in to that tool, and secondly because I don't trust that the code being generated is always optimal." In this article, I provide an alternative approach to using an IDE, an approach for sceptical programmers.

Often, trainings and tutorials that lean heavily on IDEs are very point-and-clicky. Let's take JSR-295 as our example, i.e., the JSR for beans binding. Using NetBeans IDE, our beans binding tutorial could easily be as follows:

  1. Use the New Project wizard to create a new Java application.
  2. Use the New File wizard to create a JFrame.
  3. Use the Palette to add a JTextArea.
  4. Use the Palatte to add a JTextField.
  5. Right-click the JTextField, choose Bind and then "text". The Bind dialog opens.
  6. Under "Binding Source", choose jTextArea1 and under "Binding Expression", choose ${text}. Click OK.
  7. Now deploy the application and notice that when you type something in the JTextField, the JTextArea is automatically updated.

Hmmm. What have I learned about JSR-295? Nothing at all. But, possibly that's not the intention of the tutorial in the first place. However, if this were to be an introduction to JSR-295 for me, I, as a sceptical programmer, would throw the IDE out the window. (A virtual window, I guess.) Not only have I learned nothing about beans binding, but if I want to achieve the above connection between components at any point in the future, I will ALWAYS have to use NetBeans IDE, because I have learned no general principles at all.

However, let's now leverage the IDE to its full potential and REALLY learn something significant about beans binding. We'll continue with the same tutorial, so we assume that the user has taken the steps above already. So, the steps that follow below would come after those above.

  1. Create a new JFrame in your application and again add a JTextArea and a JTextField to it.
  2. Open the Source view of the JFrame you created in the first section above.
  3. Copy the org.jdesktop.beansbinding.BindingGroup declaration to your new JFrame and instantiate it in the JFrame's constructor.
  4. Copy the definition of org.jdesktop.beansbinding.Binding into your constructor as well.
  5. Copy the method that adds the binding to the group and the method that binds it.
  6. Finally, you should now see the following, all copied from your original JFrame into your new one:

    ...
    ...
    ...
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;

    public NewJFrame2() {
    initComponents();
    bindingGroup = new org.jdesktop.beansbinding.BindingGroup();
    org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.
    createAutoBinding(
    org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE,
    jTextArea1, org.jdesktop.beansbinding.ELProperty.create("${text}"),
    jTextField1, org.jdesktop.beansbinding.BeanProperty.create("text"));
    bindingGroup.addBinding(binding);
    bindingGroup.bind();
    }
    ....
    ....
    ....

Now, we've made some real progress in terms of learning about beans binding. When I run the JFrame above, I have the same result as before. The difference is that I, as the user of the IDE, constructed the code myself, based on the code that had previously been generated for me. At this point, I am using the IDE as an advanced code snippet generator. I'm not going straight into production with code that I don't understand at all. I have had some code generated for me, then I have looked at it carefully, and extracted those parts that are relevant for my scenario. I recreated the original scenario, minus the generated beans binding stuff, and then rebuilt those parts into it.

I am still no expert at beans binding, based on where I am above. However, right now I do know that I need a BindingGroup and a Binding. I also know that the binding is added to the binding group and that the binding group is bound. I can also see from the definition of the binding that there's something referred to as an update strategy, which here is marked as READ_WRITE. I can imagine what that might mean. I also see my JTextArea and JTextField represented in the binding. I am now a lot further. Still not there, but a lot further than I would have been had I simply been using the point-and-click approach to learning.

Hence, judicious use of an IDE can clearly be a help, rather than a hindrance, when introducing yourself to new techniques, such as beans binding. By the way, if you're interested in beans binding, I highly recommend this recent interview with Shannon Hickey.

Published at DZone with permission of its author, Geertjan Wielenga.

Comments

Pete Cox replied on Mon, 2008/02/11 - 5:45pm

Still sceptical! (And yes I've used GUI Builders in NeatBeans, Eclipse, Visual Cafe, Visual Age for Java, JDeveloper and JBuilder...)

Was that method snippet your code or that of NeatBeans? It demonstrates common reasons why people hate autogenerated code.

  • clutter. Standard developmental practice is to import classes at the top of the file rather than polluting the method definition. Particularly if this JSR ever gets excepted into Java 7 and the package names change.
  • "jTextArea1"??? Autogenerated code is horrible to maintain when it's not absolutely clear from the context what said text area is supposed to do. User error in not naming the field, sure, but it's annoying. An IDE that automatically created sensible variable names from bound bean property names would be a plus.

Geertjan Wielenga replied on Mon, 2008/02/11 - 5:52pm

Pete, agree with you on both points. Both should be added to NetBeans Issuezilla. Firstly, generated code should put import statements at the top of the class. Secondly, generated names should be sensible (but how do you define "sensible")? So, my point here is that these are both (in my opinion) errors of this particular IDE and do not undermine my argument as such. (By the way, it isn't called "NeatBeans", but "NetBeans", though "NeatBeans" would have had my preference for sure!)

Pete Cox replied on Mon, 2008/02/11 - 6:39pm in response to: Geertjan Wielenga

Hi Geertjan,

Sorry to distract from your focus on beans binding but it's a pet peeve - you can always tell 'GUI-built'. Much IDE generated code can't be manually re-parsed and hand optimised nor shared across multiple IDEs. (The project I'm working on has a sanctioned IDE which some developers ignore and use their own anyway).

My point about 'sensible' is that if you're to bind a property to UI elements through the beans binding then the wizards should be smarter about creating said fields.

e.g. you have a Person object with a property "middleName". Along with your ${middleName} binding code I'd hope for the following to magically appear.

// internationalization pseudo code

private JLabel middleNameLabel = BindingResourceHelper.getString(myPersonBean, " ${middleName}");

private JTextField middleNameTextField;

initMethod(){

middleNameLabel.setLabelFor(middleNameTextField);

// pseudo - code

bind(middleNameLabel, "vetoableChange", Color.RED);

}

Thus you get a label prompt and a validation to it as red when constrained properties fail. Of course this should all be configurable via the wizard popup.

Oh, and the "NeatBeans" thing: once upon a time a manager gave an external presentation referencing the technology we were using. Embarassingly we were enable to proof-read her presentation. So from that day, at least to me and former colleagues, it's NeatBeans. :)

Geertjan Wielenga replied on Tue, 2008/02/12 - 2:21am

"My point about 'sensible' is that if you're to bind a property to UI elements through the beans binding then the wizards should be smarter about creating said fields."

Wait a minute: you need to read the article again. The wizards didn't create any fields at all. I first created a JTextField and a JTextArea myself. I named them jTextArea1 and JTextField1. My bad. Don't blame the IDE. Then I bound them to each other. So, are you saying the field names should have changed once I bound them together? That doesn't seem right to me. 

Patrick Gotthardt replied on Tue, 2008/02/12 - 8:41am

And if you had read a tutorial on how to do it by hand (in example without wizards) you would now have a much deeper understanding of BeansBinding. You'd have read a paragraph or two about the expression language used with ELProperty, you would've known if you can swap ELProperty and Property freely (or if the order in your sample is mandatory). You would've had a real explaination of the update strategy and the available options (instead of just a guess). And it would've taken you pretty much the same time to read...

A big additional bonus: You wouldn't have needed to start/install NetBeans and you could've printed the tutorial and read it without a computer next to you.

I'm very happy that my IDE generates code for me (except for GUI-builders, those are still useless and a source for trouble) but this is nothing that should be part of a tutorial. Except for "point-and-click"-like tutorials where you're trying to learn how to use your IDE. But then you'd be better of watching a screencast anyway.

Larry Wales replied on Tue, 2008/02/12 - 10:28am

THIS IS FREAKIN AWESOME!! YOU HIT THE NAIL ON THE HEAD I THINK. THERE'S A CATCH 22 WITH IDE TUTORIALS AND THAT IS THAT THERES NEVER ANY DESCRIPTION OF WHAT THE GENERATED CODE DOES AND YOUR SUPPOSED TO JUST KNOW. WHAT DOES IT DO? DAMNED IF I KNOW. WELL IF YOUR AN EXPERT YOU PROBABLY KNOW, BUT THEN YOU PROBABLY WOULDN'T BE HERE IN THE FIRST PLACE. SO MAYBE THERES A IDE TUTORIAL THAT DOES EXPLAIN WHAT THE IDE'S GENERATED CODE DOES. OKAY, I SAY, I'LL GO LOOK FOR THAT. BUT THEN YOU FIND OUT THERE'S NOTHING AND YOUR BACK AT SQUARE ONE WONDERING IF USING AN IDE IS THE ANSWER FOR YOU. I NEED A BEER NOW

Jeroen Wenting replied on Wed, 2008/02/13 - 2:20am

Now if someone were to write a module that would automatically downcast posts made in all upper case this site (and many others) would get even better...

Larry Wales replied on Wed, 2008/02/13 - 6:25pm

HAY JEROME - YOU MY BROTHER!!:)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.