Break down complex code structures into simpler, more readable forms. This includes avoiding deep nesting, extracting complex logic into helper functions, simplifying boolean expressions, and reducing code duplication.
Key practices:
match
statements instead of multiple if
conditions for the same variable!(a || b)
instead of separate conditions)unwrap_or()
instead of imperative if/else
blocksExample of improvement:
// Before: Complex nested conditions
if filename == "package.json" {
// logic
}
if filename == "package.json" || filename == "pom.xml" {
// more logic
}
// After: Clear match statement
match (generate_opts.author_url, filename) {
(Some(url), "package.json" | "pom.xml") => replacement.replace(AUTHOR_URL_PLACEHOLDER, url),
(None, "package.json") => replacement.replace(AUTHOR_URL_PLACEHOLDER_JS, ""),
(None, "pom.xml") => replacement.replace(AUTHOR_URL_PLACEHOLDER_JAVA, ""),
_ => {},
}
This approach improves code maintainability, reduces cognitive load, and makes the codebase more approachable for new contributors.
Enter the URL of a public GitHub repository