How GPT-4 refactors deprecated code for me
Following my attempts to refactor old code. I went further into the rabbit hole to see if I can use GPT-4 to specifically refactor deprecated code.
Refactoring deprecated code
I quickly searched the code base for some known deprecated methods usage and asked GPT-4 for help:
This is a really good use case — small amount of scoped code that does 1 specific thing.
Onto bigger fish — I remembered an old project that always throws deprecation warnings. The following file is a great example; I simplified it and added some comments for readability.
from pymongo import MongoClient, ASCENDING
class MongoDBHandler:
def __init__(self, host="localhost", port=27017, db_name="test_db"):
# MongoClient is not deprecated,
# but passing host and port as separate arguments is.
self.client = MongoClient(host, port)
self.db = self.client[db_name]
def insert_document(self, collection_name, document):
# insert is deprecated in favor of insert_one or insert_many.
return self.db[collection_name].insert(document)
def find_document(self, collection_name, query={}):
# find_one is not deprecated
# but passing an empty dict as a query is not recommended.
return self.db[collection_name].find_one(query)
def create_index(self, collection_name, fields):
# ensure_index is deprecated in favor of create_index.
return self.db[collection_name].ensure_index(fields, ASCENDING)
GPT to the rescue (again)
I questioned the implementation of find_document
and got a lesson:
So it turns out not only deprecated methods were updated but also a known bad practice was taken care of — but did I want that?
This is exactly the place to stop and say — whether some AI or LLM produced 99% of the code or whether you just asked for some pointer —
Own the final code, own the implications of any change to the code base, you are responsible for it and no one else. Code that was refactored or produced is to be used as assistance but the code committed is signed by you and you alone. There is no blaming a machine after production exploded because of generated code you did not understand or test well enough.
Limitations
All was going smoothly so I thought we’ll go ahead and upgrade the pymongo
package:
This is a limitation you should be aware of — if you are wanting or required to use the latest & greatest in your code — bad news. The training data stops at a certain point in time and you should always remember that when using these tools.
Tips & Takeaways
- You can use AI aid for different scenarios to your liking and as you see fit — it’s not a “one size fits all” situation.
- Own the end result
- Tests are your best friend when refactoring — check out How GPT-4 writes tests for me
- Don’t put any confidential/IP code into a black box on the web
- Remember this — messy input = messy output.
The input you supply should have all the information needed for refactoring — nothing less, nothing more. - Large Language Models (LLM) are trained on a set of data which is naturally finite and up to a certain point in time.