• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Can you help me to write better code ?

 
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have written following simple program but seems not satisfied. I would like to ask following questions:-

1. Will it take less memory?
2. Is there better way of declaring variables?
3. Is this monolithic code for ur actionPerformed() OK? or can I adopt some better way for same?
4. Can some body make changes,if required, and let me know how to make it better efficient?

regards,
Arun
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, here's a couple things I'd like to point out.
Your code is too long (kinda). Obviously, you need all of the code here, but you've got too much of it grouped into big functions. Try to keep your functions short and to the point. Some of the methods, like login, can be broken down into smaller methods. This will help keep your code more maintainable, readable, and flexible. Well-focused methods are a good thing. Try not to let them get too long.
It's often useful to use another class as an ActionListener rather than using the panel itself. Obvisouly, it works, but you might want to think about using an inner class or just a separate top-level class to keep the event handling code separated from the code for building and displaying the form. Check out this thread where this was discussed.
Try those couple of things and see if you like your code any better.
Corey
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One other thing I noticed. I see that you're creating a lot of new Color objects, even though they're the same color. Create just one Color object for each color and then reuse it. For example, do this:

If you're using these same objects in multiple methods, you might even want to think about making this Color object a member variable.
Corey
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a big fan of Junilu's OOCalc Tutorial over in the OO, Patterns, UML and Refactoring forum.
Good Luck.
 
arun mahajan
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It's often useful to use another class as an ActionListener rather than using the panel itself. Obvisouly, it works, but you might want to think about using an inner class or just a separate top-level class to keep the event handling code separated from the code for building and displaying the form


I am sorry to ask this but I have little experience on inner classes. can you rewrite a part of actionPerformed method to let me understand it better.

Making my login method shorter to make it more readable and maintainable


I could not grasp this also. because in this method I am just decalring all labels and other compontnets required by this. How to make it more efficient? can you re-write a code snippent for me.
Hope I am not putting you pepople on extra work.
Thanks once again for help extending.
regards,
Arun :roll:
[ April 03, 2002: Message edited by: arun mahajan ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, first, let's see what this looks like if we use a couple inner classes to handle the ActionEvent. Originally, you had the Panel itself acting as the ActionEventListener. I'm going to make a couple of inner classes that can do the listening for us. One goal you should always have in mind is the "separation of concerns." You want to keep your code to display your screen and your code to handle events separate. That way, if you have to change the way your screen looks, you don't necessarily have to change the way events are handled. This can greatly increase application flexibility and maintainability.
I'd rewrite your test class to be something like this:

You see, you can declare a class as a member variable just as if you were declaring any other member. This is known as an inner class. Do a search in this forum or the Programmer Certification Forum to find gobs of information on inner classes. By creating two separate inner classes, you can separate the action events that you'll be handling. The class MyMenuListener only has to worry about MenuItem actions - it doesn't have to worry about Button actions because those will be sent to and instance of the MyButtonListener class.
Next, for the extra methods. Just think "divide and conquer." You have a method called login, which is pretty long. In that method, you're repeatedly doing the same things, just to different objects. Let's make another method:

As you can see, this makes your code much shorter and more readable. You don't need to rewrite code to do the same thing repeatedly, just write a method and call it over and over again.
One other thing, you might want to look at using a LayoutManager, rather than setting the bounds for all of your components by hand. It might make your life a lot easier.
I hope that helps,
Corey
[ April 03, 2002: Message edited by: Corey McGlone ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, first, let's see what this looks like if we use a couple inner classes to handle the ActionEvent. Originally, you had the Panel itself acting as the ActionEventListener. I'm going to make a couple of inner classes that can do the listening for us. One goal you should always have in mind is the "separation of concerns." You want to keep your code to display your screen and your code to handle events separate. That way, if you have to change the way your screen looks, you don't necessarily have to change the way events are handled. This can greatly increase application flexibility and maintainability.
I'd rewrite your test class to be something like this:

You see, you can declare a class as a member variable just as if you were declaring any other member. This is known as an inner class. Do a search in this forum or the Programmer Certification Forum to find gobs of information on inner classes. By creating two separate inner classes, you can separate the action events that you'll be handling. The class MyMenuListener only has to worry about MenuItem actions - it doesn't have to worry about Button actions because those will be sent to and instance of the MyButtonListener class.
Next, for the extra methods. Just think "divide and conquer." You have a method called login, which is pretty long. In that method, you're repeatedly doing the same things, just to different objects. Let's make another method:

As you can see, this makes your code much shorter and more readable. You don't need to rewrite code to do the same thing repeatedly, just write a method and call it over and over again.
One other thing, you might want to look at using a LayoutManager, rather than setting the bounds for all of your components by hand. It might make your life a lot easier.
I hope that helps,
Corey
 
It's exactly the same and completely different as this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic