Widgets declared as method local (final) VS class members

GWT Designer allows you to quickly create the modules, composites, panels, remote services and other elements that comprise Google Web Tookit applications.

Moderators: Konstantin.Scheglov, gnebling, Alexander.Mitin, jwren, Eric Clayberg

Widgets declared as method local (final) VS class members

Postby Apikoulas » Sun Feb 17, 2008 10:28 am

Could someone highlight the pros and cons of declaring widget variables:

a) locally on the method (as final to allow reference in anonymous class listerner)
eg
Code: Select all
public void onModuleLoad() {            
      final  Button button = new Button();
                .....
}


b) declaring as class member variables (which to me looks more clear)
Code: Select all
public class ImageViewer implements EntryPoint {
   private Button button = new Button();
}


I have also come across some extremes, such as declaring vars in a method inside a block eg. { Button b = new Button()...} - Is there any point in that ?

I feel that b) has these pros :
- More clear code, seperation of declarations & code
- Vars can be referenced by other methods
Apikoulas
 
Posts: 2
Joined: Sun Feb 17, 2008 9:59 am

Re: Widgets declared as method local (final) VS class members

Postby Eric Clayberg » Sun Feb 17, 2008 1:27 pm

This is very simple. If you need to reference a widget programatically outside of the method that created it, you should create it as a field. If you don't need to access it, you can have it created as a simple local variable. Most static text labels, for example, can be created as local variables while most text widgets, tables, etc. should be created as fields. Fortunately, GWT Designer gives you full control over how each individual widget is declared. You can make all widgets fields by default or only widgets of specified types. You can also convert a widget from being a field to a local variable and vice versa at any time with a single click.
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA

Re: Widgets declared as method local (final) VS class members

Postby Apikoulas » Sun Feb 17, 2008 4:45 pm

Thank you Eric,

I understand we can do both.

The question is what are the benefits of local 'final' declaration inside the method, instead of always declaring everything as fields/class members, which is more 'clean' and OO.

I cant see any out-of-the-box benefit with local declaration (such as optimization for memory management , lazy initialization etc). Instead I consider it as cluttered spaghetti code.

Actually I think its better having widgets declared AND initialized on the class as fields, ie
Code: Select all
Class Foo extends ... {
     Button b = new Button("Click me");
}

which BTW initialasation breaks the Designer's ability to convert from 'field' to 'local' (not crucial anyway).
Apikoulas
 
Posts: 2
Joined: Sun Feb 17, 2008 9:59 am

Re: Widgets declared as method local (final) VS class members

Postby Eric Clayberg » Sun Feb 17, 2008 7:38 pm

I think the question you should be asking is what is the benefit of assigning a widget to a field that is never referenced. If you never need to reference a widget, there is no reason to declare it as a field.

Doing so does not make it more 'clean' or more OO. Cluttering your class with a bunch of extra, unneeded fields is more a symptom of bad OO design than good.

That said, if you prefer a GUI style that has every widget declasred as a field, GWT Designer will happily accomodate you.
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA

Re: Widgets declared as method local (final) VS class members

Postby peterblazejewicz » Mon Feb 18, 2008 11:57 am

Apikoulas wrote:Could someone highlight the pros and cons of declaring widget variables:

a) locally on the method (as final to allow reference in anonymous class listerner)
eg
Code: Select all
public void onModuleLoad() {            
      final  Button button = new Button();
                .....
}


b) declaring as class member variables (which to me looks more clear)
Code: Select all
public class ImageViewer implements EntryPoint {
   private Button button = new Button();
}


I have also come across some extremes, such as declaring vars in a method inside a block eg. { Button b = new Button()...} - Is there any point in that ?

I feel that b) has these pros :
- More clear code, seperation of declarations & code
- Vars can be referenced by other methods

Hello,

this could be answered in many ways, especially if you also want to take JVM/Javac optimization into scope. There are sometimes rules within organization that put some design/implementation details rules to follow, sometimes one is free about implementation details and can follow own habits, etc.,
Here is a nice book you could read on that subject:
http://safari.informit.com/9780321413093
it covers most of answers you would probably receive from coders :D

In GWT (toolkit) terms: you're free of choice on implementation details, Even "final" has no real importance in GWT. That's because GWT (toolkit - i'm not writing about GWT Desinger plugin+Eclipse) does not use Javac - at least on code that is to run on client side. Everything is compiled by GWT compiler (transcoded from java to javascript, optimized, etc),

regards,
Peter
Peter Blazejewicz
GWT groups profile
peterblazejewicz
 
Posts: 153
Joined: Fri Jul 27, 2007 7:09 pm
Location: Europe/Poland/Warsaw

Re: Widgets declared as method local (final) VS class members

Postby Eric Clayberg » Wed Feb 20, 2008 5:59 am

Thanks for the info.

Our position with GWT Designer is to try and support most reasonable code generation styles as opposed to dictating the one, true, correct choice.
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA

Re: Widgets declared as method local (final) VS class members

Postby Olivercomputing » Fri Apr 23, 2010 5:20 pm

Eric - you'd said earlier, re: local vs. field variable definitions:

"Fortunately, GWT Designer gives you full control over how each individual widget is declared."

I can see under Eclipse Preferences how to change the general default, but how is this default changed on a per-widget basis?
Olivercomputing
 
Posts: 12
Joined: Fri Apr 23, 2010 5:03 pm

Re: Widgets declared as method local (final) VS class members

Postby Eric Clayberg » Fri Apr 23, 2010 5:49 pm

You can set defaults for each type...

http://download.instantiations.com/D2WB ... ables.html

Image

...or set the local/field status of each widget... Image Image

http://download.instantiations.com/D2WB ... _menu.html

Image
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA


Return to GWT Designer

Who is online

Users browsing this forum: No registered users and 3 guests