473,480 Members | 3,021 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Suggestion on refactoring existing code

Dear All,

I have a code developed by former employees. I extract some part of it
as below:

// definition of class CWNPrimitiveFace, it represent a face
class CWNPrimitiveFace : public CWN3DObjBase
{
friend ofstream& operator<<( ofstream& f, CWNPrimitiveFace& obj );
friend ifstream& operator>>( ifstream& f, CWNPrimitiveFace& obj );
public:
CWNPrimitiveFace();
CWNPrimitiveFace( wxString name );
CWNPrimitiveFace( unsigned int nid );
CWNPrimitiveFace( const CWNPrimitiveFace& face );
virtual ~CWNPrimitiveFace();

CWNPrimitiveFace& operator=( const CWNPrimitiveFace& obj );

// override
virtual void Scale( double k );
//
bool IsPlane();
bool IsReferenceFace();
bool IsRect();
bool IsEllipse();

void SetRefFace( void* pAcisFace );
void* GetRefFace();
void SetRect( double w, double h );
bool GetRect( double& w, double& h );
void SetEllp( double r0, double r1 );
bool GetEllp( double& r0, double& r1 );

protected:
int m_nFaceType; // 0 - unknown, 1 - referent to other face,
// 2 - rect, 3 - ellp, 4 - ....
union{
struct {
void *m_pOwner;
} ref_face;

struct{
double width;
double height;
} rect;

struct{
double r0;
double r1;
} ellp;
} m_Para;

private:
void InitData();
void CopyData( const CWNPrimitiveFace& obj );
void RemoveRefFace();
};

void CWNPrimitiveFace::CopyData( const CWNPrimitiveFace& obj )
{
// remove old face
RemoveRefFace();

//
m_nFaceType = obj.m_nFaceType;

if( m_nFaceType == 1 )
{
m_Para.ref_face.m_pOwner =
g_Acis.CopyEntity( obj.m_Para.ref_face.m_pOwner );
}
else if( m_nFaceType == 2 )
{
m_Para.rect.width = obj.m_Para.rect.width;
m_Para.rect.height = obj.m_Para.rect.height;
}
else if( m_nFaceType == 3 )
{
m_Para.ellp.r0 = obj.m_Para.ellp.r0;
m_Para.ellp.r1 = obj.m_Para.ellp.r1;
}
}

In the code, most classes have the similar structure: prefer to union
other than polymorphism. Some even have nested switch-cases. The code
are not fully tested. It has been only used to run some cases and
several crash bugs were found. The code is of 70K line. The code is
wrritten by a guy with 10 years of c++ experiences in 8 moths. I am
wondering is the code worth refactoring?

Thanks,

Shuisheng

Mar 13 '07 #1
4 1621
shuisheng wrote:
Dear All,

I have a code developed by former employees. I extract some part of it
as below:

[..]

In the code, most classes have the similar structure: prefer to union
other than polymorphism. Some even have nested switch-cases. The code
are not fully tested. It has been only used to run some cases and
several crash bugs were found. The code is of 70K line. The code is
wrritten by a guy with 10 years of c++ experiences in 8 moths. I am
wondering is the code worth refactoring?
Unless the code is totally not working, it's always worth refactoring.
But don't refactor for the sake of refactoring, though. Deterimine
what you're going to get out of it. It's not improbable that even
code like what you posted can be made to work with minimal changes.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 14 '07 #2
JLS
On Mar 13, 5:25 pm, "shuisheng" <shuishen...@yahoo.comwrote:
Dear All,

I have a code developed by former employees. I extract some part of it
as below:

// definition of class CWNPrimitiveFace, it represent a face
class CWNPrimitiveFace : public CWN3DObjBase
{
friend ofstream& operator<<( ofstream& f, CWNPrimitiveFace& obj );
friend ifstream& operator>>( ifstream& f, CWNPrimitiveFace& obj );
public:
CWNPrimitiveFace();
CWNPrimitiveFace( wxString name );
CWNPrimitiveFace( unsigned int nid );
CWNPrimitiveFace( const CWNPrimitiveFace& face );
virtual ~CWNPrimitiveFace();

CWNPrimitiveFace& operator=( const CWNPrimitiveFace& obj );

// override
virtual void Scale( double k );
//
bool IsPlane();
bool IsReferenceFace();
bool IsRect();
bool IsEllipse();

void SetRefFace( void* pAcisFace );
void* GetRefFace();
void SetRect( double w, double h );
bool GetRect( double& w, double& h );
void SetEllp( double r0, double r1 );
bool GetEllp( double& r0, double& r1 );

protected:
int m_nFaceType; // 0 - unknown, 1 - referent to other face,
// 2 - rect, 3 - ellp, 4 - ....
union{
struct {
void *m_pOwner;
} ref_face;

struct{
double width;
double height;
} rect;

struct{
double r0;
double r1;
} ellp;
} m_Para;

private:
void InitData();
void CopyData( const CWNPrimitiveFace& obj );
void RemoveRefFace();

};

void CWNPrimitiveFace::CopyData( const CWNPrimitiveFace& obj )
{
// remove old face
RemoveRefFace();

//
m_nFaceType = obj.m_nFaceType;

if( m_nFaceType == 1 )
{
m_Para.ref_face.m_pOwner =
g_Acis.CopyEntity( obj.m_Para.ref_face.m_pOwner );
}
else if( m_nFaceType == 2 )
{
m_Para.rect.width = obj.m_Para.rect.width;
m_Para.rect.height = obj.m_Para.rect.height;
}
else if( m_nFaceType == 3 )
{
m_Para.ellp.r0 = obj.m_Para.ellp.r0;
m_Para.ellp.r1 = obj.m_Para.ellp.r1;
}

}

In the code, most classes have the similar structure: prefer to union
other than polymorphism. Some even have nested switch-cases. The code
are not fully tested. It has been only used to run some cases and
several crash bugs were found. The code is of 70K line. The code is
wrritten by a guy with 10 years of c++ experiences in 8 moths. I am
wondering is the code worth refactoring?

Thanks,

Shuisheng
I think it depends on circumstances. If the code is going to be
modified and enhanced and is part of active development, then it is
probably worth refactoring; The code will be easier to understand and
maintain. If the code is not going to be maintained then the costs and
risks involved in refactoring may outway the benefits.

Mar 14 '07 #3
On Mar 13, 5:25 pm, "shuisheng" <shuishen...@yahoo.comwrote:
[code sample snipped]
In the code, most classes have the similar structure: prefer to union
other than polymorphism. Some even have nested switch-cases. The code
are not fully tested. It has been only used to run some cases and
several crash bugs were found. The code is of 70K line. The code is
wrritten by a guy with 10 years of c++ experiences in 8 moths. I am
wondering is the code worth refactoring?
This is kind of peripheral to language issues. This is
getting much more into software engineering. Oh well.

It is not really possible to tell you what to do from what
you have provided. But there are some suggestions I can make.

A union is not automatically good or bad. There are situations
in which a union makes sense. That's part of why it has been
kept in the language. An example might be, as you mention, a
form of allowing a class to handle different situations in
the same memory. There are, of course, some rules you need
to follow to make unions sensible. Such as, do not use them
to try to convert one type to another.

Nested switch cases are, again, not automaticlaly good or bad.
It indicates a certain complexity of coding, and that isn't
necessarily a good sign. Usually there are ways to get around
such a level of complexity. But they are not always a gain
overall, as they sometimes involve "tricky" coding methods.
And they often involve either more functions or more classes.
It's not automatic that there is a net gain.

Generally speaking, don't modify code unless there is some
business reason to do so. Making code "pretty" or "elegant"
is not a good enough reason to open it to possible bugs
due to coders. Remember that every bug in the code was
put there by a coder. If it works, don't change it.

But when there are already bugs, and you are touching
the code anyway, it may be that there are things that
are worth doing while you are there. Again, from the
snippet you provided, it isn't possible to decide.

Consider how much effort it will take to make the
changes you are contemplating. Consider what the
benefit will be from having the modified code.
Try to make an estimation of whether the changes
will in fact remove all important bugs, or whether
the code is too complex for a reasonable coder to
be able to maintain it.

Also give some thought to the level of testing you
are applying, the quality of the documentation,
what specifications for performance you have, etc.
For a code with 70K lines, and 8 months of development
effort, there should be some significant effort in
these directions. Making these efforts easier may be
a good reason to make changes to the code. But again,
make sure it makes sense to spend the time on it
before you start paying some software guy(s) to spend
a long time working over it.
Socks

Mar 14 '07 #4
"shuisheng" <sh*********@yahoo.comwrote in message
news:11*********************@l77g2000hsb.googlegro ups.com...
Dear All,

I have a code developed by former employees. I extract some part of it
as below:

// definition of class CWNPrimitiveFace, it represent a face
class CWNPrimitiveFace : public CWN3DObjBase
{
friend ofstream& operator<<( ofstream& f, CWNPrimitiveFace& obj );
friend ifstream& operator>>( ifstream& f, CWNPrimitiveFace& obj );
public:
CWNPrimitiveFace();
CWNPrimitiveFace( wxString name );
CWNPrimitiveFace( unsigned int nid );
CWNPrimitiveFace( const CWNPrimitiveFace& face );
virtual ~CWNPrimitiveFace();

CWNPrimitiveFace& operator=( const CWNPrimitiveFace& obj );

// override
virtual void Scale( double k );
//
bool IsPlane();
bool IsReferenceFace();
bool IsRect();
bool IsEllipse();

void SetRefFace( void* pAcisFace );
void* GetRefFace();
void SetRect( double w, double h );
bool GetRect( double& w, double& h );
void SetEllp( double r0, double r1 );
bool GetEllp( double& r0, double& r1 );

protected:
int m_nFaceType; // 0 - unknown, 1 - referent to other face,
// 2 - rect, 3 - ellp, 4 - ....
union{
struct {
void *m_pOwner;
} ref_face;

struct{
double width;
double height;
} rect;

struct{
double r0;
double r1;
} ellp;
} m_Para;

private:
void InitData();
void CopyData( const CWNPrimitiveFace& obj );
void RemoveRefFace();
};

void CWNPrimitiveFace::CopyData( const CWNPrimitiveFace& obj )
{
// remove old face
RemoveRefFace();

//
m_nFaceType = obj.m_nFaceType;

if( m_nFaceType == 1 )
{
m_Para.ref_face.m_pOwner =
g_Acis.CopyEntity( obj.m_Para.ref_face.m_pOwner );
}
else if( m_nFaceType == 2 )
{
m_Para.rect.width = obj.m_Para.rect.width;
m_Para.rect.height = obj.m_Para.rect.height;
}
else if( m_nFaceType == 3 )
{
m_Para.ellp.r0 = obj.m_Para.ellp.r0;
m_Para.ellp.r1 = obj.m_Para.ellp.r1;
}
}

In the code, most classes have the similar structure: prefer to union
other than polymorphism. Some even have nested switch-cases. The code
are not fully tested. It has been only used to run some cases and
several crash bugs were found. The code is of 70K line. The code is
wrritten by a guy with 10 years of c++ experiences in 8 moths. I am
wondering is the code worth refactoring?
As opposed to what? Most likely, yes. Are you asking is the code worth
refactoring, or should we start from scratch? It's hard to say without
looking at all the code, but I've found in most cases refacting is better as
long as the original design wasn't way off course.

I've started refactoring some badly written code and by the time I was done
nothing in the program was the same, I had rewritten all of it. The code
here doesn't look that bad, and if I actually spent some time looking at it
I could probably fix any errors relatively click. But this is just at a
quick glance.

70k is not that big. I just looked at the source for one of my projects,
the client .cpp is 170k The server .cpp is 155k. Client launcher is 49k.
Plus various other .cpps generally less that 10k each. 70k is probably
small and shouldn't be that bad.

I would say, if the program crashes, fix the existing program instead of
rewriting it.
Mar 18 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1580
by: | last post by:
Hi, Refactoring a winform causes problems if moving this form to a subdirectory in the same project. What is the workaround for this and will this be fixed in future? Thanks /BOB
2
2085
by: Sachin Garg | last post by:
Hi, I was trying to find (like many others here) a tool for refactoring C++ code as I have lately been noticing that I spend most of my coding time doing refactoring and some refactoring which...
10
1379
by: Eivind Grimsby Haarr | last post by:
When using polymorphism in a big system, I have sometimes come across problems when changing the signature of the function in the base class, and forgetting to change the signature of the derived...
8
1608
by: Gunnar G | last post by:
Hi. I'm sorry for this somewhat off-topic message, but since this is the best place to find clever C++ programmers with a lot of wisdom. What I need is suggestions for a C++ project that takes...
8
1995
by: Frank Rizzo | last post by:
I keep hearing this term thrown around. What does it mean in the context of code? Can someone provide a definition and example using concrete code? Thanks.
4
1382
by: loulou384 | last post by:
Refactoring by C. Keith Ray Software development on your project has slowed to a crawl. Instead of adding new features, you and your fellow programmers want to rewrite the existing code to make...
15
4357
by: Simon Cooke | last post by:
Does anyone know of any tools for refactoring header files? We're using a third party codebase at work, and pretty much every file includes a 50Mb precompiled header file. I'm looking for a tool...
24
2075
by: Mike Hofer | last post by:
Please forgive the cross-post to multiple forums. I did it intentionally, but I *think* it was appropriate given the nature of my question. I'm working on an open source code library to help...
2
1559
by: pingu219 | last post by:
Hi I'm currently in the midst of building a C high-level refactoring program in Java but I was wondering if there are any good parsers (or some other alternative) which are able to read in C files...
0
7055
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6760
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7022
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3013
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
572
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.