From 4e38dda5fd4b0dda9b6d593b019fe3618bccb953 Mon Sep 17 00:00:00 2001 From: "Jeong, YunWon" Date: Fri, 6 Feb 2026 09:27:36 +0900 Subject: [PATCH] [AGNETS.md] guide control flow --- AGENTS.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/AGENTS.md b/AGENTS.md index 85e839a8538..3dd13f6b967 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -125,6 +125,31 @@ Run `./scripts/whats_left.py` to get a list of unimplemented methods, which is h - Follow Rust best practices for error handling and memory management - Use the macro system (`pyclass`, `pymodule`, `pyfunction`, etc.) when implementing Python functionality in Rust +#### Avoid Duplicate Code in Branches + +When branches differ only in a value but share common logic, extract the differing value first, then call the common logic once. + +**Bad:** +```rust +let result = if condition { + let msg = format!("message A: {x}"); + some_function(msg, shared_arg) +} else { + let msg = format!("message B"); + some_function(msg, shared_arg) +}; +``` + +**Good:** +```rust +let msg = if condition { + format!("message A: {x}") +} else { + format!("message B") +}; +let result = some_function(msg, shared_arg); +``` + ### Python Code - **IMPORTANT**: In most cases, Python code should not be edited. Bug fixes should be made through Rust code modifications only