Bad previous code. To fix or not to fix?

Posted by Viniyo Shouta on Programmers See other posts from Programmers or by Viniyo Shouta
Published on 2012-11-25T05:35:08Z Indexed on 2012/11/25 11:22 UTC
Read the original article Hit count: 190

Filed under:

As a freelancer programmer I am often asked to edit part of an application source code in order to add functionalities, fix bugs etc. While I'm on my adventure journey to study the source to do what I'm asked correctly I run into code like:

World::User* GetWorld()
{
    map<DWORD,World*>::iterator it = mapWld.find( m_userWorldId )
    if( it != mapWld.end() )
        return &it->second;
    return NULL;
}

if( pUser->GetWorld()->GetId() == 250 )

If I investigate further I end up finding that the DWORD class member of User, userWorldId can be a value non-found in the map mapWld, which will lead to a casuality as also known as crash! The obviously valid way to do it is:

World* pWorld = pUser->GetWorld(); if( pWorld && pWorld->GetId() == 250 )//...

Sometimes when it's something just 'small' I end up sort of 'fixing' it. But sometimes when I'm on a 500 thousand line source code and this kind of code is everywhere there is no much can do. The question is if it's politically correct to fix some of these things. Think of it;

  1. You are not paid to fix it.
  2. Perhaps you think it's right, but it was necessarily done that way for some reason and you should not be messing with it.
  3. You do not have authorization, you do not own the source and none of the copyrights belong to you.
  4. You have authorization to edit issues accordingly to the owners but you're in a hurry, you have many other projects to do, it's the end of the month, you must pay the bills.

Sincerely, I think of it as seeing an animal die from a disease in front of you, you have the cure in your hands but you do nothing. What is the best to do in this scenario?

© Programmers or respective owner

Related posts about c++