Ensure your comparison algorithms handle edge cases properly and avoid common pitfalls: 1. **Normalize values before comparison** - When comparing strings, consider if you need to normalize whitespace, case, or other variants:
Ensure your comparison algorithms handle edge cases properly and avoid common pitfalls:
// Problematic: Direct comparison may fail due to trailing whitespace
if (lineA === lineB) { /* ... */ }
// Better: Normalize before comparing when appropriate
if (lineA.trimEnd() === lineB.trimEnd()) { /* ... */ }
// Problematic: 'model' shadows the outer parameter
for (const model of specificModels) {
if (model.toLowerCase() === model) { // Always true!
return true;
}
}
// Better: Use distinct variable names
for (const specificModel of specificModels) {
if (model.toLowerCase() === specificModel.toLowerCase()) {
return true;
}
}
// Problematic: Breaking loop on first non-match may skip valid items
for (let i = chatHistory.length - 1; i >= 0; i--) {
if (item.message.role !== "assistant") {
break; // Stops looking at earlier messages
}
}
// Better: Continue searching when appropriate
for (let i = chatHistory.length - 1; i >= 0; i--) {
if (item.message.role !== "assistant") {
continue; // Skips this item but continues searching
}
// Process assistant messages
}
// Problematic: Strict equality might miss semantically equivalent items
if (i.id.providerTitle === item.id.providerTitle) {
// Only checks exact IDs, might miss different references to same resource
}
// Better: Consider what equality means in this context
if ((i.id.providerTitle === item.id.providerTitle) ||
(i.uri && item.uri && i.uri.value === item.uri.value)) {
// Checks both ID equality and URI reference equality
}
Enter the URL of a public GitHub repository