Choose algorithms that correctly match the problem requirements and understand their computational properties and side effects. Avoid using overly general solutions (like regex) when simpler, more precise algorithms exist.
Choose algorithms that correctly match the problem requirements and understand their computational properties and side effects. Avoid using overly general solutions (like regex) when simpler, more precise algorithms exist.
Key principles:
Match algorithm to problem domain: Use domain-specific algorithms rather than general-purpose ones when precision matters. For example, TypeScript path mapping requires prefix/suffix matching, not regex pattern matching.
Understand algorithmic side effects: Operations like p.findSymbol
may have side effects (marking usage) that affect other parts of the system. Document and account for these effects.
Centralize complex algorithmic logic: Place intricate algorithms like collision resolution in dedicated, well-audited locations rather than scattering the complexity across multiple passes.
Example of incorrect algorithm selection:
// Incorrect: Using regex for simple pattern matching
if matched, err := regexp.MatchString("^"+key, path); matched && err == nil {
// This treats '*' as regex repetition, not literal asterisk
}
// Correct: Use prefix/suffix matching like TypeScript compiler
func isPatternMatch(prefix, suffix, candidate string) bool {
return len(candidate) >= len(prefix) + len(suffix) &&
strings.HasPrefix(candidate, prefix) &&
strings.HasSuffix(candidate, suffix)
}
Before implementing an algorithm, verify it handles edge cases correctly and matches the computational complexity requirements of your use case.
Enter the URL of a public GitHub repository