Skip to content

Commit 890fa5d

Browse files
committed
[AGNETS.md] guide control flow
1 parent 2c291dc commit 890fa5d

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

AGENTS.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,31 @@ Run `./scripts/whats_left.py` to get a list of unimplemented methods, which is h
125125
- Follow Rust best practices for error handling and memory management
126126
- Use the macro system (`pyclass`, `pymodule`, `pyfunction`, etc.) when implementing Python functionality in Rust
127127

128+
#### Avoid Duplicate Code in Branches
129+
130+
When branches differ only in a value but share common logic, extract the differing value first, then call the common logic once.
131+
132+
**Bad:**
133+
```rust
134+
let result = if condition {
135+
let msg = format!("message A: {x}");
136+
some_function(msg, shared_arg)
137+
} else {
138+
let msg = format!("message B");
139+
some_function(msg, shared_arg)
140+
};
141+
```
142+
143+
**Good:**
144+
```rust
145+
let msg = if condition {
146+
format!("message A: {x}")
147+
} else {
148+
format!("message B")
149+
};
150+
let result = some_function(msg, shared_arg);
151+
```
152+
128153
### Python Code
129154

130155
- **IMPORTANT**: In most cases, Python code should not be edited. Bug fixes should be made through Rust code modifications only

0 commit comments

Comments
 (0)