A Post Entitled New Year’s Cleanup: Off Color Code
For a number of reasons I have inherited a lot of code recently and I’m in the process of trying to clean up and put the finishing touches on a release we hope to see out the door soon. That’s the good news. The bad news is that my emphasis has had a lot more ‘clean up’ than ‘finishing touches’ of late.
What is Off-Color Code?
One of the things that I have had to clean up a fair bit of is off-color code. What is off-color code? It’s that code in your repository that is impossible to decipher at first glance because no source code highlighter can help. It looks something like this…
# def complete_payment(panel_id)
# Panel.transaction do
# panel = Panel.find(panel_id)
# court_ap_id = Court.find(panel.court_id).accounts_payable.id
# # identify all pending expenses for the panel
# pending_expenses = Financial::RosterExpense.for_panel(panel_id).for_state('approved')
# # collect hash fo summarized rosters to send for journalling to financials
# ...snip a few dozen lines...
# unless resp.instance_of?(Net::HTTPOK)
# logger.debug("ERRORED in Journaling with #{resp.body}")
# raise ActiveRecord::Rollback
# response = false
# end
# end
# response ||= true
# return response
# end
The syntax highlighting on this site cannot even help there. What’s that? It’s all comments? Exactly. It’s all comments.
The uncertain developer prefers to keep deprecated code in the code base while refactoring. He fears the code surgeon’s scalpel when performing a long overdue operation. Why? Most often there is a sense that if the code is removed completely then some ‘knowledge’ has been lost or we might forget some insight we once had.
Wrong. Don’t comment out code. That’s what git, subversion, cvs, and all the other source code management software is for. Cut the dead code loose from your code base. You will not miss it. Ever. If you doubt it then try the following test.
- Open a project that is old enough to eat solid food.
- Find a model or controller that has a method (or at least a large chunk of contiguous code) commented out.
- Summarize what the code does in less than ten seconds. (1pt)
- Name at least one insight contained in the code. (1pt each)
- Count the number of times you have read the code in the last month (1pt each).
If you score 1 point or less then it’s time to get rid of that code.
The Cost of Dead Code
The biggest problem with dead code is that it has a larger cost than benefit. The cost is measured in a few ways. It takes away from your code base by making it more difficult to read and comprehend the active code because you have to skip around it. It takes away from your code by adding to the questions that brought you to this particular code: is this code coming or going? Has it been recaptured elsewhere? Why was it removed in the first place?
Simply put, this code in suspended animation takes your eye away from what your source is otherwise trying to accomplish. Even worse, it does this every time you look at the source code.
Get rid of that dead code.
Simplify. Simplify. Simplify.