Stretchwickster
Programmer
Hi everyone,
I'm fairly new to Java but have a lot of experience (including object-oriented) in other languages such as Delphi. I'm trying to get to grips with the basics of Extreme Programming (XP) and in particular "Refactoring". I need a starting point on some code I have - and this is not a homework assignment! The aim of refactoring is to improve the code, often by making it simpler to read.
I've started by introducing a new variable Pi so I've added the line
and substituted Pi for the 3.1415929 parts.
The 2 area expressions basically perform the same calculation with different parameters so I was thinking of pulling this out into a separate method and calling it each time.
Is there a simpler form of the conditional statements I've got i.e. that don't use a string comparison?
I know this is a very basic example, but what would be your starting point for improving the readability of this code. Any help would be much appreciated.
Clive
I'm fairly new to Java but have a lot of experience (including object-oriented) in other languages such as Delphi. I'm trying to get to grips with the basics of Extreme Programming (XP) and in particular "Refactoring". I need a starting point on some code I have - and this is not a homework assignment! The aim of refactoring is to improve the code, often by making it simpler to read.
Code:
class GeomFigure
{
public:
char type[20];
float area;
float radius;
float major_axis;
float minor_axis;
float calcArea()
{
if(strcmp("circle", type))
{
area = radius * radius * 3.1415929;
}
if(strcmp("ellipse", type))
{
area = major_axis * minor_axis * 3.1415929;
}
return area;
}
};
Code:
final float Pi = 3.1415929
The 2 area expressions basically perform the same calculation with different parameters so I was thinking of pulling this out into a separate method and calling it each time.
Is there a simpler form of the conditional statements I've got i.e. that don't use a string comparison?
I know this is a very basic example, but what would be your starting point for improving the readability of this code. Any help would be much appreciated.
Clive