Forums Register Login

what would you change if this was your program?

+Pie Number of slices to send: Send
i got my SCJP in 2001 and wrote code for several years but now after about 6 years of not writing i am kind of like a greenhorn again. anyway i threw together this program using copy/paste/edit from other programs i have written(and a little help from here at the ranch) and it is raising questions and doubts about how i am doing things.
1 the methods: i think i will get rid of initComponents() since the constructor doesn't do much anyway. i think i will keep center() since i have to use it more than once. about main(), i always did it that way, but can't i set the size,center,and all that in the constructor?
2 the imports: i think i am importing too much. using * is good if there are a lot of things to import from that package but this is a very simple program.
3 the variables: this is my main question. like i said i threw this together in a hurry, so it is a hodgepodge of member and local variables. sometimes we have no choice:we need the variable in more than one method, we can't instantiate when we declare, or we need a new instance each time a method is called. i think it is best to instantiate when we declare if we can. member vs local, each has advantages. if we need a new instance each time and the method gets called a lot then i think it is better to use a local variable. but sometimes we can do it either way. so anyway what would you change if this was your program?
+Pie Number of slices to send: Send
For one thing I wouldn't have my class implement ActionListener. Sure, my class is a JFrame, but it isn't an ActionListener. It should HAVE an ActionListener, but it shouldn't BE one.

That decision allows it to have more than one ActionListener, which could happen as soon as you put two JButtons into your GUI. The ActionListener should be a nested class; I'm in the habit of making it an anonymous inner class but the other day when I found I had anonymous inner classes nested two deep I decided that was falling off the "best practices" cart and redesigned it to use named inner classes.
+Pie Number of slices to send: Send
good advice for the future, but as you can see this is a very simple program with only one action. my main question is about member vs local variables. maybe that would have been a better topic, but i want any feedback.

Sure, my class is a JFrame


some people would say my class should have a JFrame not be one
+Pie Number of slices to send: Send
 

Randall Twede wrote:some people would say my class should have a JFrame not be one



Also a valid point of view.
+Pie Number of slices to send: Send
 

Randall Twede wrote:my main question is about member vs local variables.



Ah yes. You have some of your GUI setup done as part of declaring those member variables, and some of it done in the initComponents method. You should put it all in one place -- that would be in the initComponents method, since it can't all be done as a side effect of declaring variables. So just declare the member variables, don't initialize them at the same time. Put the initializations into the initComponents method along with the other initialization code.
+Pie Number of slices to send: Send
 

it can't all be done as a side effect of declaring variables


really? i forgot to mention the program works fine. also it was my thought it was better to initialize(instantiate) at the top when declaring when possible.
+Pie Number of slices to send: Send
 

Randall Twede wrote:

it can't all be done as a side effect of declaring variables


really?


Yes, really. Look at your initComponents method for an example of initialization which can't be done as part of the declarations.

i forgot to mention the program works fine.


Not relevant, since you were asking about coding style.

also it was my thought it was better to initialize(instantiate) at the top when declaring when possible.


Sure. It's also better to keep related code together.
+Pie Number of slices to send: Send
point taken. it is desirable to keep related code together.i guess what is bothering me is i can make most or all of the member variables local. and of course i could make the local variables members.
We cannot change unless we survive, but we will not survive unless we change. Evolving tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1024 times.
Similar Threads
Position etc. of Webstart windows
File Chooser Dialog box
Help with displaying data in JTextfield
slight problem using JScrollPane
composition vs inheritance
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 02:02:08.