views:

228

answers:

6

Code generated for swing always fails when it comes to code quality. Inevitably, there's one method that builds the entire interface, and there's anonymous event handling code that calls member methods.

Does anyone have some nuggets on transforming this monstrous code to well organized code.

Is it worth going without a GUI builder tool, for the sake of code quality.

Thanks

+2  A: 

The answer to your question lies in how much this code is going to be maintained, and how central it is in your module.

If this is a low-maintenance side component which its quality suffices for most needs, I see no point in refactoring code which works now, and can easily be thrown away and replaced by something new in the future (given it is a side component). Given the fact the Swing/AWT code is generally not nice to look at, you might be able to pass this one by.

However, if you are going to be working on this code in the long run, and it is central in your architecture, the time spent on refactoring is going to be worth it when it comes to maintainability and supportability in the future.

Yuval A
+1  A: 

Generally, you shouldn't be hand-editing generated code.

If you use an editor with code-folding, I suggest using that to keep the rest of the code looking tidy.

or generating the code to a seperate file, and then never opening it.
MatthieuF
+8  A: 

If you will keep using the GUI builder, then there's not much point to refactoring any output of the GUI builder. Depending on the specific builder, any refactoring you do to its code may mean that you can no longer use the builder on that GUI. Yes, GUI builders often generate ugly code. But as long as you will continue to use the GUI builder, just set findbugs and checkstyle, etc, to ignore the file(s) created by the GUI builder and move on.

I have personally done this both ways. In some cases, I've abandoned the GUI builder. For these, I ended up refactoring the code to make it more maintainable.

Eddie
Agreed. I have yet to work on a project where we didn't wind up throwing out the auto-generated code at some point in the development cycle. I always code my GUIs by hand now (using MiGLayout), and find that I'm much more productive b/c of it. I now do my preliminary layouts on paper or using a drawing program. This also turned out to be a big time saver over using a gui builder. Who'd of thunk?!?
Kevin Day
+3  A: 

A lot of code generation systems have a model in which they generate two classes for everything (let's say a screen): one which they are free to revise at will and a second, a subclass of the first, which you the developer are free to revise at will (they will only generate the subclass once). If your system is doing this, then I would say it makes sense to clean up the subclass - the one you'll be working on - and leave the superclass alone (because of course any work you put into cleaning it will be wiped out the next time you invoke the generator).

Carl Manaster
+1  A: 

My approach is to avoid code generation. Bad formatting is the least of your worries.

It's not too hard to build a GUI with straight Swing if you get to know a few layouts (border and flow at a minimum), and you can also create routines to automatically generate your GUI based on your data or metadata--a nice trick that a GUI builder cannot even start to do (but if you're using a GUI builder, you'll probably never even try to make it data driven).

Personally I'd like to see more builders that generate a "Class" file instead of a "Java" file--something with nothing but controls laid out in a reasonable manner, allowing you to access the controls remotely either through a getter or something. (Heck, since it's such a specific use case, I wouldn't even have a problem making the members public..).

Objective-C does something like this, it's only the second builder I've seen that doesn't try to use code generation (the first was a private one we built for a product I worked on a long time ago that never got very far)

Bill K
Intellij's IDEA uses this model for its builder. You have the option of generating a Java source file if you want, but by default it stores it in a .form file, and gives you a jar with an ant extension so you can generate the class even without IDEA around
Yishai
GridBagLayout is the one true layout manager you need. Seriously border and flow isn't going to work for much.
Tom Hawtin - tackline
Take a look at MigLayout, you may change your mind. The Layout Manager that IDEA uses instead of GridBagLayout (optionally) is also decent, but its proprietary. I also found that BoxLayout works, if you can draw your UI in Excel, but it gets a little weird in some places.
Yishai
Any of the several table-based layouts is an excellent foundation for building GUI's - all other layouts are then needed only occasionally for sub-component groups. Mine is published at: http://www.SoftwareMonkey.org/Code/MatrixLayout
Software Monkey
I am not likely to change my mind about code generation, I've seen the things it does--it's just bad.I wasn't saying those would solve every problem, but when i've needed to throw together a quick GUI, they have often done the trick. Many others are useful as well, but borderlayout does a better job than most at resizing, it's predictable and trivial to nest. Flow sucks, but when properly nested it can solve a few problems..So let me take back any specific recommendations about layouts and just re-assert, never use a tool that requires you to edit generated code. Ever.
Bill K
A: 

I would avoid a GUI builder as much as possible. Firstly it creates ugly code that generally can't be refactored (good luck trying to get your GUI builder to parse refactored code).

But more importantly, using a GUI builder really hampers re-usability. Like anything, if you're smart you can use OOP to your advantage. Create re-usable components. Re-usable behaviours. If you're application is even mildly complex, you'll want to use a framework to make things more manageable. If you don't, good luck changing things in the future. You'll probably be ok for one, maybe two version, but after that you'll be fighting hard to get anything done. Every change will have to be done in a million places.

Plus you can learn Swing pretty fast. It's not that hard once you get the hang of it. Using proper OO design in your Swing code will make a world of difference for the future.

Stephane Grenier