After a while, we might end up with a pretty good suite of characterization tests for the legacy code. We can now approach this code like any other well-tested code and start applying the bigger refactorings with an aim to improve the design before adding our new features.
For example, we might decide to extract the print_action
method into a separate Action
class, or the parse_file
method into a Reader
class.
The following is a FileReader
class where we have moved the contents from the parse_file
local method:
class FileReader: def __init__(self, filename): self.filename = filename def get_updates(self): updates = [] with open("updates.csv", "r") as fp: for line in fp.readlines(): symbol, timestamp, price = line.split(",") updates.append((symbol, datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S.%f"), int(price))) return updates
We then use the Inject Dependencies pattern to pass the reader
as...