Wednesday, March 26, 2008

Bug bite

It's a very subtle bug but results in great headache. I didn't notice my bug until someone pointed it out. Good lesson learned: Always have tests when you refactor.

See if you could spot it.


// BEFORE
private LockID[] getAllLockIDs(LockID id) {
LockID[] lids = new LockID[transactionStack.size() + 1];
lids[0] = id;
for (int i = 1; i < lids.length; i++) {
TransactionContext tc = (TransactionContext) transactionStack.get(i - 1);
lids[i] = tc.getLockID();
}
return lids;
}


// AFTER
private List getAllLockIDs(LockID id) {
List lids = new ArrayList();
lids.add(id);
for (int i = 1; i < transactionStack.size(); i++) {
TransactionContext tc = (TransactionContext) transactionStack.get(i - 1);
lids.add(tc.getLockID());
}
return lids;
}