Here, I collect code bugs that I have made, so hope I can avoid to make same mistake again in future.
It's important to write correct code at first version when write code - especially during interview.
1. Forget to initialize variables: collection - especially instance or static variable.
-- Init variables when declare them.
2. ! nor
while (!queue.isEmpty())
Make sure no forever loop: while will exit
remove items from collection in for loop
Use wrong variable
public void concatRegexSrcField(SolrDocument doc, String srcPatternField,
StringBuilder sb) {
Pattern pattern = Pattern.compile(srcPatternField);
for (String docField : doc.getFieldNames()) {
Matcher matcher = pattern.matcher(docField);
if (matcher.matches()) {
// srcPatternField is previous called srcField, and I use srcField here, which I should really use srcPatternField
doConcat(doc, sb, doc.getFieldValue(srcField));
if (removeSrcFileds) {
doc.removeFields(docField); // remove items from collection in for loop, I should use iterator
}
}
}
}
It's important to write correct code at first version when write code - especially during interview.
1. Forget to initialize variables: collection - especially instance or static variable.
-- Init variables when declare them.
2. ! nor
while (!queue.isEmpty())
Make sure no forever loop: while will exit
remove items from collection in for loop
Use wrong variable
public void concatRegexSrcField(SolrDocument doc, String srcPatternField,
StringBuilder sb) {
Pattern pattern = Pattern.compile(srcPatternField);
for (String docField : doc.getFieldNames()) {
Matcher matcher = pattern.matcher(docField);
if (matcher.matches()) {
// srcPatternField is previous called srcField, and I use srcField here, which I should really use srcPatternField
doConcat(doc, sb, doc.getFieldValue(srcField));
if (removeSrcFileds) {
doc.removeFields(docField); // remove items from collection in for loop, I should use iterator
}
}
}
}