Files
codex/prs/bolinfest/study/PR-1387-study.md
2025-09-02 15:17:45 -07:00

95 lines
2.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
**DOs**
- **Use `is_some`/`is_none`:** Prefer direct Option checks when the value isnt used.
```rust
// Do
if event.response.is_some() {
let _ = tx_event.send(Ok(ResponseEvent::Created)).await;
}
```
- **Prefer unit variants:** Use unit enum variants when they carry no data.
```rust
// Do
#[derive(Debug)]
pub enum ResponseEvent {
Created,
// ...
}
match evt {
ResponseEvent::Created => { /* ... */ }
_ => {}
}
```
- **Borrow with `Cow` to avoid clones:** Only allocate when you must change the data.
```rust
use std::borrow::Cow;
fn with_missing_calls<'a>(prompt: &'a Prompt, missing_calls: Vec<ResponseItem>) -> Cow<'a, Prompt> {
if missing_calls.is_empty() {
Cow::Borrowed(prompt)
} else {
Cow::Owned(Prompt {
input: [missing_calls, prompt.input.clone()].concat(),
..prompt.clone()
})
}
}
// Usage
let prompt = with_missing_calls(prompt, missing_calls);
let mut stream = sess.client.clone().stream(&prompt).await?;
```
- **Explain non-obvious transformations:** Add a concise comment where behavior isnt obvious.
```rust
// The Responses API rejects follow-ups until all prior call_ids have outputs.
// If the previous turn was interrupted, prepend synthetic "aborted" outputs
// so this turn wont 400 on missing call_ids.
let prompt = with_missing_calls(prompt, missing_calls);
```
- **Use struct update syntax:** Keep reconstructed structs concise and correct.
```rust
// Do
let prompt = Prompt {
input: [missing_calls, prompt.input.clone()].concat(),
..prompt.clone()
};
```
**DONTs**
- **Dont pattern-match just to test presence:** Avoid `if let Some(_) = ...` when you dont use the value.
```rust
// Don't
if let Some(_) = event.response { /* ... */ }
```
- **Dont define empty struct-like variants:** Avoid `Created {}` when no fields exist.
```rust
// Don't
pub enum ResponseEvent {
Created {},
}
```
- **Dont clone large structs unnecessarily:** Avoid `prompt.clone()` unless youre actually modifying it.
```rust
// Don't
let prompt = prompt.clone(); // unnecessary if unchanged
```
- **Dont rebuild every field manually:** Prefer `..prompt.clone()` over listing unchanged fields.
```rust
// Don't
let prompt = Prompt {
input,
prev_id: prompt.prev_id.clone(),
user_instructions: prompt.user_instructions.clone(),
store: prompt.store,
extra_tools: prompt.extra_tools.clone(),
};
```
- **Dont leave rationale implicit:** When rewriting inputs (e.g., injecting missing call outputs), document why to aid future maintenance.