INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*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.

# Basic Refactoring2

## Basic Refactoring

(OP)
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 {

public:
char type[20];
float area;
float major_axis;
float minor_axis;

float calcArea()
{
if (strcmp("circle",type))
{
}
if (strcmp("ellipse",type))
{
area=major_axis*minor_axis*3.1415929;
}

return area;
}
};

Is the polymorphism the way to go with this one?

Clive

### RE: Basic Refactoring

2
Clive

#### CODE

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.

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:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!