Search code examples
refactoringcode-cleanuplegacy-code

Python: Detect code which gets never executed in production


I need to do refactoring in a big legacy Python code base.

Often I think "these lines don't get executed in production any more".

But I am unsure.

There are some tests which touch these lines. But I can't tell for sure if really no usage happens in production.

What can I do in this situation?

This question is about coverage on a production system. This question is not about coverage during testing/CI.

I don't want to comment out that lines, since I don't want to produce an error in the production system.


Solution

  • Common practice is to use logging inside that lines of code. e.g. you have a block of code you think is not in use. You add try catch block in the beginning of that block of code. Inside trycatch you add line to a specific json named same as your suspicious block of code.

    try:
        with open("block1.dat", "rb") as file:
            activity = pickle.load(file)
    
        curtime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        currentact = "dt = {}; code done that: var1 = {}, 
            var2 = {}".format(curdate, var1, var2)
    
        activity.append(currentact)
    
        file = open("block1.dat", "ab")
        pickle.dump(activity, file)
        file.close()
    except Exception: pass
    

    You can use telegram api to log code to. After a while You'll get info how often your code works and what does it do. Then you monitor for a while and if nothing happens in a month, You can comment the block.