When designing APIs, favor simple and explicit interfaces over flexible but complex ones. This reduces cognitive load, improves compile-time checking, and makes the code more maintainable.
When designing APIs, favor simple and explicit interfaces over flexible but complex ones. This reduces cognitive load, improves compile-time checking, and makes the code more maintainable.
Key principles:
Example - Instead of flexible but costly:
pub fn vocab<S: BuildHasher>(mut self, vocab: HashMap<String, u32, S>) -> Self {
// Implementation
}
Prefer explicit:
pub fn vocab(mut self, vocab: HashMap<String, u32>) -> Self {
// Implementation
}
Or when dealing with multiple return types, instead of:
fn token_to_word(token: usize) -> Option<(usize, usize)> | Option<usize> {
// Implementation varies based on sequence count
}
Prefer separate methods:
fn token_to_word(token: usize) -> Option<usize> {
// Single sequence implementation
}
fn token_to_word_with_sequence(token: usize) -> Option<(usize, usize)> {
// Multi-sequence implementation
}
This approach makes APIs more predictable, easier to maintain, and reduces potential runtime errors by catching issues at compile-time.
Enter the URL of a public GitHub repository