Once, long ago, I use to be a janitor cleaning up after executives in office buildings as I put myself through school. At the time I would be sweeping the floors as I saw some of those workers crunching through their spreadsheets late at night getting ready for their presentations the next day. One guy I saw did a lot of print shop type of stuff, graphic work, troubleshooting etc and I guess you could say at the time I really knew then that I wanted to be involved with computing. I found myself befriending him on occasion and helping him with his work with the computer skills I did have. Sometimes I think they ripped off my story to make Good Will Hunting, but that is just speculation. Anyways, that was years ago and today I work in executive offices writing code and doing what I love.
But I have to say that I never stopped cleaning. Just ask my spouse and I she will tell you that I still do my fair share of it around the house. Even though I write code now, one really great habit I never let go of, and for programmers of all languages to get into, is leaving code in better shape than you found it. Just like the days when I had to clean offices after some kind of disaster (dropped ink cartridges, children’s playrooms, New Years office parties) I do the same when I run into code that is just messy.
After years of practice I don’t even think about it much anymore. I run across a repetitive group of lines and I refactor it, document it and make it as readable as possible and move on. If I run into a loop that I could cut out half the lines and make it smaller and more efficient I do it. The trick is to do a little bit at a time and take your time to make sure it is done right and that you fully test things after making any type of change. I often revisit the same function over and over again and whittle it down over time.
If you are going to clean up code though, I have a few suggestions you might want to follow as you do this:
1) Make sure you communicate with the developer who wrote the code if possible. The idea here is that we want to check with them if things are ok to modify and get them involved. They may be a little nostalgic or they may not care a darn bit and let you have full reign over it. If you can clean things up, two bodies are simply better than one… especially if that other person is the one who knows the building… err code.
2) Clean up one function, or possibly one class, at a time. Even if you see messy code just next door don’t touch it. It can seem overwhelming at first in codebases that are filthy. I ran into similar buildings and I had to just pick one thing to focus on at the moment and put the rest on a todo list for the future. Eventually you can revisit those sections too (if you are doing any work in that section) and clean those up as well.
3) Do bite size changes. Rename a few variables to be more descriptive, cut out a line here or there if you can or maybe focus on simplifying a loop and nothing more. We don’t want to introduce any bugs if we can help it. So small incremental changes can help us avoid introducing new bugs and help us find bugs we might have introduced since we changed very few things.
4) Make sure you fully test everything that you change. Again, we don’t want to introduce any new bugs and testing is key.
5) Never be a janitor when it comes to production. Yeah that function may be as messy as hell but if it is in production then it is off limits. Go make the changes in your development environment and fully test there, have it then go in production later. As a janitor I would always skip a room if the person is still working in there. I would do what I can to clean around their office until they leave and then clean.
6) Don’t let it consume you to the point that you are not doing what you need to do. There will always be more cleanup, more tweaking, more misc tasks that you could do so do it at a pace that is comfortable and you can still get your priorities accomplished.
But this habit of cleaning code as you do your work is a really great thing to have and I know for a fact that companies are looking for those types of programmers. You are adding value to existing code when you clean it up and update it. Eventually you will get a system that was once littered with bad code into something that is functional and looking rather good. By the end of the night, after focusing on one task after another, I would eventually leave the office sparkling.
I can’t say you will always be showered with praise or untold riches for doing this, but in the end eyebrows will be raised in surprise when they realize their software is performing beyond their wildest expectations. Then perhaps, just perhaps, they will ask the question “Who did all this?” and the janitorial programmer can raise his hand and be pulled out of the shadows for a change. Just don’t drop the broom.
Thanks for reading!