Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Basic Refactoring

Basic Refactoring

Basic Refactoring

Hi, I'm just starting to look at refactoring and have some code I wish to get some pointers on. This is not a homework assignment, I just need some tips from java programmers.

class GeomFigure {

    char type[20];
    float area;
    float radius;
    float major_axis;
    float minor_axis;

    float calcArea()
      if (strcmp("circle",type))
      if (strcmp("ellipse",type))
      return area;

Is the polymorphism the way to go with this one?

Your help/advice/pointers would be much appreciated!


RE: Basic Refactoring


Replace your code with


class GeomFigure {
   float majorAxis;
   float minorAxis;

   float calcArea() {
      return majorAxis * minorAxis * java.lang.Math.PI;
Refactoring points are:

1. get rid of area, return the value directly
2. replace magic numbers with a constant (pi)
3. as a circle is just a special case of an ellipse where major_axis = minor_axis, then you can get rid of radius, type and all the kludgy ifs.

If you subsequently want to extend to other shapes, you could go for polymorphism to support it, but as you are starting from a simpler, refactored code base it will be much easier to do.

Java textbooks always seem to use 'Shape' classes to illustrate polymorphism. I have to say, it smells like a homework assignment to me. Let me know what score you get...

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close