Refactor often

in programming •  7 years ago 

When working in a large code base over many years you will encounter older code that is battle scared from bug fixes and function creep. You will also find code that is now cumbersome as it was written in an earlier versions of the language.

When in old code that has good test coverage I will refactor it as I touch it. I rely on the wonderful Resharper tool for C# that plugs into Visual Studio to spot possible issues and refactors. With something like the following:

List<Person> employedPeople = new List<Person>();
foreach (Person person in people)
{
    if (person.Employed)
    {
        employedPeople.Add(person);
    }
}

It will replace it with something like the following.

var employedPeople = people.Where(p => p.Employed).ToList();

These quick sorts of clean ups as you move means the code stays cleaner so takes far less mental load to work with when you come back to it a year later.

Both are obvious but I know the one with less places for bugs and that is easier to read :)

Do you re-factor as you move through old code? What has your experience been doing so?

Happy coding

Woz

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!