Create APIs that are both easy to use correctly and hard to use incorrectly. Focus on: 1. **Use pattern matching for safer error handling** instead of unwrapping values that might be null:
Create APIs that are both easy to use correctly and hard to use incorrectly. Focus on:
// Instead of:
let catalog_name = specifier.strip_prefix("catalog:").unwrap_or("default");
// Prefer:
if let Some(catalog_name) = specifier.strip_prefix("catalog:") {
if let Some(catalogs) = &self.catalogs {
// Use catalog_name directly
}
}
// Instead of:
fn token(mut self, value: String) -> Self {
self.output.token = Some(value);
self
}
// Prefer:
fn token(mut self, value: &str) -> Self {
self.output.token = Some(value.into());
self
}
// Instead of:
Result<TurboJson, Error>
// Consider:
Result<Option<TurboJson>, Error>
// Instead of:
fn view(app: &mut App<Box<dyn io::Write + Send>>, f: &mut Frame, rows: u16, cols: u16)
// Prefer:
fn view<W>(app: &mut App<W>, f: &mut Frame, rows: u16, cols: u16)
// Instead of manually implementing Serialize:
impl<'a> Serialize for RepositoryDetails<'a> { ... }
// Prefer using serde attributes:
#[serde(into)]
// With a From implementation to handle the conversion
These practices lead to APIs that are more intuitive, safer, and require less documentation to use properly.
Enter the URL of a public GitHub repository