I came across an anti-pattern earlier this week:
execute sql query returning customerId, somethingElse
foreach customerId, somethingElse
map[customerId]=somethingElse
The problem there was that the customerIds were not unique. The author of the code assumed it was. The result was an issue that was detected in production, and was traced back to this code. I believe that assumptions should be documented by the code, in a way that fails fast.
I would propose something like:
execute sql query returning customerId, somethingElse
foreach customerId, somethingElse
if exists map[customerId]
throw RuntimeException("multiple customerIds found")
map[customerId]=somethingElse
This documents the assumptions of the author in an unambiguous way, furthermore the moment that the assumptions are no longer true the operators/maintainers will find out before production data are corrupted.
execute sql query returning customerId, somethingElse
foreach customerId, somethingElse
map[customerId]=somethingElse
The problem there was that the customerIds were not unique. The author of the code assumed it was. The result was an issue that was detected in production, and was traced back to this code. I believe that assumptions should be documented by the code, in a way that fails fast.
I would propose something like:
execute sql query returning customerId, somethingElse
foreach customerId, somethingElse
if exists map[customerId]
throw RuntimeException("multiple customerIds found")
map[customerId]=somethingElse
This documents the assumptions of the author in an unambiguous way, furthermore the moment that the assumptions are no longer true the operators/maintainers will find out before production data are corrupted.