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