Let's start with an example.
Let's say, I have a method called export
that depends heavily on the DB schema. And by “depends heavily” I mean I know that adding a new column to a certain table often (very often) leads to the corresponding export
method change (usually you should add the new field to the export data as well).
Programmers often forget to change export
method since it's not really clear you should even look at this. My goal is to force programmer explicitly make a decision to determine whether he forgot to look at the export
method or just don't want to add a field to the export data. And I'm looking for the design solution for this problem.
I have two ideas, but both of them have flaws.
Smart “Read all” wrapper
I can create the smart wrapper that makes sure all data is explicitly read.
Something like this:
def export():
checker = AllReadChecker.new(table_row)
name = checker.get('name')
surname = checker.get('surname')
checker.ignore('age') # explicitly ignore the "age" field
result = [name, surname] # or whatever
checker.check_now() # check all is read
return result
So, checker
asserts if table_row
contains another fields that were not read. But all this thing looks kind of heavy and (maybe) affects perfomance.
“Check that method” unittest
I can just create the unittest that remembers the last table schema and fails every time the table is changed. In that case programmer would see something like “don't forget to check out the export
method”. To hide the warning programmer would (or wouldn't — that's a problem) check out export
and manually (that's another problem) fix the test by adding new fields into it.
I have a few other ideas but they are too troublesome to implement or too difficult to understand (and I don't want the project to become a puzzle).
The above problem is just an example of the more wide class of problems I encounter from time to time. I want to bind some pieces of code and/or infrastructure so changing one of them immediately alerts programmer to check another one. Usually you have some simple tools like extracting common logic or writing reliable unittest, but I'm looking for the tool for more complex cases: maybe some design patterns I'm now aware of.
export
based on the Schema?export
has everything you realistically need?