|
|
|
|
|
by peheje
159 days ago
|
|
I don't necessarily disagree with providing context, but my concern is that comments eventually lie. If the business rule evolves (say the window moves to 5 days) the comment becomes a liability the moment someone updates the code but forgets the prose. The comment also leaves me with more questions: how do you handle multiple identical amounts in that window? I would still have to read the implementation to be sure. I would prefer encoding this in an Uncle Bob style test. It acts as living documentation that cannot get out of sync with the code and explains the why through execution. For example: test("should_match_debit_with_credit_only_if_within_three_day_settlement_window", () => {
const debit = A_Transaction().withAmount(500.00).asDebit().on(JANUARY_1);
const creditWithinWindow = A_Transaction().withAmount(500.00).asCredit().on(JANUARY_4);
const creditOutsideWindow = A_Transaction().withAmount(500.00).asCredit().on(JANUARY_5);
expect(Reconciliation.tryMatch(debit, creditWithinWindow)).isSuccessful();
expect(Reconciliation.tryMatch(debit, creditOutsideWindow)).hasFailed();
});
This way, the 3 day rule is a hard requirement that fails the build if broken rather than a suggestion in a comment block. |
|
Yes, comments can drift out of sync with the code. That doesn't mean the comments were a bad idea; it just means the programmer didn't update the comments when they should have.
Similarly, even with zero comments, variable names can drift out of sync with their intended purpose, e.g. when they are repurposed to hold values that are different from what their name implies. Again, the solution is to rename the variable so it reflects its purpose (or possibly create another variable).
> This way, the 3 day rule is a hard requirement that fails the build if broken rather than a suggestion in a comment block.
What happens when a dev changes the test code, but fails to update the string "should_match_debit_with_credit_only_if_within_three_day_settlement_window"? That's effectively no different than comments being out of sync with the code.