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

2.5 KiB
Raw Blame History

DOs

  • Use is_some/is_none: Prefer direct Option checks when the value isnt used.
// 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.
// 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.
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.
// 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.
// 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.
// Don't
if let Some(_) = event.response { /* ... */ }
  • Dont define empty struct-like variants: Avoid Created {} when no fields exist.
// Don't
pub enum ResponseEvent {
    Created {},
}
  • Dont clone large structs unnecessarily: Avoid prompt.clone() unless youre actually modifying it.
// Don't
let prompt = prompt.clone(); // unnecessary if unchanged
  • Dont rebuild every field manually: Prefer ..prompt.clone() over listing unchanged fields.
// 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.