pub struct Wayland { connection: Connection, state: WaylandState, } impl Wayland { pub…
- User
pub struct Wayland { connection: Connection, state: WaylandState, }
impl Wayland { pub fn new(assign_gamma: bool) -> anyhow::Result { let connection = Connection::connect_to_env()?;
let display = connection.display(); let mut event_queue = connection.new_event_queue(); let qh = event_queue.handle(); let mut state = WaylandState::new(); display.get_registry(&qh, ()); event_queue.roundtrip(&mut state)?; if let Some(gamma_manager) = &state.gamma_manager { if assign_gamma { for output in state.outputs.iter_mut() { let mut output = output.write(); let wl_output = &output.wl_output; output.gamma_control = Some(gamma_manager.get_gamma_control(wl_output, &qh, ())); } } event_queue.roundtrip(&mut state)?; if state.outputs.is_empty() { anyhow::bail!(WaylandError::Outputs); } } else { anyhow::bail!(WaylandError::Compositor); } Ok(Self { connection, state }) }}
the new fn accepts a bool arguement which is bad for clean code. refactor it to eliminate the bool arguement
Assistant
To eliminate the boolean argument from the
newfunction, we can refactor the code by introducing two separate constructors: one that enables gamma assignment (with_gamma) and one that doesn't (new). This makes the API more explicit and adheres to clean code principles.Here's the refactored code:
pub struct Wayland { connection: Connection, state: WaylandState, } impl Wayland { /// Creates a new Wayland instance without gamma control assignment pub fn new() -> anyhow::Result<Self> { Self::new_internal(false) } /// Creates a new Wayland instance with gamma control assignment pub fn with_gamma() -> anyhow::Result<Self> { Self::new_internal(true) } /// Internal common constructor fn new_internal(assign_gamma: bool) -> anyhow::Result<Self> { let connection = Connection::connect_to_env()?; let display = connection.display(); let mut event_queue = connection.new_event_queue(); let qh = event_queue.handle(); let mut state = WaylandState::new(); display.get_registry(&qh, ()); event_queue.roundtrip(&mut state)?; if let Some(gamma_manager) = &state.gamma_manager { if assign_gamma { for output in state.outputs.iter_mut() { let mut output = output.write(); let wl_output = &output.wl_output; output.gamma_control = Some(gamma_manager.get_gamma_control(wl_output, &qh, ())); } } event_queue.roundtrip(&mut state)?; if state.outputs.is_empty() { anyhow::bail!(WaylandError::Outputs); } } else { anyhow::bail!(WaylandError::Compositor); } Ok(Self { connection, state }) } }Key changes:
-
Split into two public constructors:
new(): Creates instance without gamma assignmentwith_gamma(): Creates instance with gamma assignment
-
Common internal implementation:
new_internal()handles the common setup with a boolean parameter- Maintains all existing functionality and error checks
-
Improved API clarity:
- Callers explicitly choose their desired behavior:
Wayland::new()for no gamma controlWayland::with_gamma()for gamma control
- Eliminates "magic boolean" in function signature
- Self-documenting method names
- Callers explicitly choose their desired behavior:
This approach preserves all existing functionality while making the interface more intuitive and maintainable. The boolean parameter is now encapsulated within the implementation and not exposed in the public API.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.