This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes code convention and swing construction Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "code convention and swing construction" Watch "code convention and swing construction" New topic

code convention and swing construction

Perry Board

Joined: May 03, 2003
Posts: 29
I know people have said to strictly follow Sun's coding standards, but this one is just hard for me to completely comply with:
"Don't wait to declare variables until their first use..."
I completely agree for MOST methods, but when you are creating Swing gui's, I feel like it makes much more sense to keep the declaration close to the use.
For example, in creating a menu bar, you might have three different menus, each with several menu items. By Sun's standard, you declare all of those swing components at the top of your method, then manipulate/add them later. This has the effect of jumbling up everything so that if you want to remove a single menu item you have to scour the entire method and carefully extract it from the declaration section like picking fishbones out of your dinner. "Which JMenuItem was that? [scroll, scroll...] Oh, you want to change the color of that JLabel? Now you'll have to declare it at the top of your method so you can hold it in a variable temporarily."
Ugg. Anyone have a comment? I hate to break the standard, but I think it also breaks the rule of code clarity to do it the "correct" way.
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17271

I see nothing wrong in their standard. Actually all my components on a GUI are declared as instance variables. This way you can have setter and getters to get the values that are on the GUI.

Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
"Don't wait to declare variables until their first use..."
I completely agree for MOST methods,

Naah, I don't agree with this standard at all. Swing code is a good example of the ugliest effects of this dictum, but I think it's pretty pointless in general. The Book of Joshua, Chapter 7, Item 29: Minimize the scope of local variables.
The most powerful technique for minimizing the scope of a local variable is to declare it where it is first used. If a variable is declared before it is used, it is just clutter - one more thing to distract the reader who is trying to figure out what the program does. By the time the variable is used, the reader might not remember the variable's type or initial value. If the program evolves and the variable is no longer used, it is easy to forget to remove the declaration if it's far removed from the point of first use.

I agree, and further I have a strong preference for short methods wherever possible; I like to be able to see the entirely of a method on my screen at once, without needin to scroll about. Declare-before-use interferes with this.
But, Sun's coding standard is unfortunately clear here. (Though I could argue that Effective Java is at least as indicative of Sun's recommended practices as the coding standard is, but I guess I won't.) So, comply with the standard, however ugly it may be.
However there are ways to minimize the ugliness. Note that you're required to declare at the beginning of a block, not necessarily the beginning of a method. If a variable is only needed inside a for loop, you can still declare it at the beginning of the loop. And you're allowed (encouraged even) to initialize during the declaration - you can accomplish quite a lot here if you call another method in the initialization. And I will often move work to other methods Here's an example of the sort of thing I sometimes do for swing:

I don't mind having lots of (private) methods if they have decent self-descriptive names - I just want the individual methods to be simple and easily understood. Lots of people have GUI setup methods taking hundreds of lines, which gets even worse if you're required to declare before use. The structure above allows me to avoid that. Dunno it anyone else likes it, but it works for me, and seems to comply with the coding standards.

"I'm not back." - Bill Harding, Twister
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17271

"I just want the individual methods to be simple and easily understood. "
I completely agree. I don't think I have ever had a method that was more that 20 lines. If it is then it is doing more than one thing in the method and can be broken out further for easier to read code.
Of course the size is different with GUI's
I agree. Here's the link:
subject: code convention and swing construction
It's not a secret anymore!