• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

code convention and swing construction

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Mark
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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.
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
Mark
 
Who among you feels worthy enough to be my best friend? Test 1 is to read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic