Back to all reviewers

prefer const correctness

electron/electron
Based on 7 comments
Other

Always mark variables, parameters, and methods as `const` when they don't modify state. This improves code safety, enables compiler optimizations, and makes intent clearer to readers.

Code Style Other

Reviewer Prompt

Always mark variables, parameters, and methods as const when they don’t modify state. This improves code safety, enables compiler optimizations, and makes intent clearer to readers.

Apply const in these scenarios:

  • Variables that don’t change: const auto* command_line = base::CommandLine::ForCurrentProcess();
  • Parameters that aren’t modified: bool IsDetachedFrameHost(const content::RenderFrameHost* rfh)
  • Methods that don’t modify object state: bool IsWirelessSerialPortOnly() const;
  • Pointers to immutable data: const display::Screen* screen = display::Screen::GetScreen();

Example transformation:

// Before
auto* command_line = base::CommandLine::ForCurrentProcess();
display::Screen* screen = display::Screen::GetScreen();
bool IsDetachedFrameHost(content::RenderFrameHost* rfh);

// After  
const auto* command_line = base::CommandLine::ForCurrentProcess();
const display::Screen* screen = display::Screen::GetScreen();
bool IsDetachedFrameHost(const content::RenderFrameHost* rfh);

Const correctness prevents accidental modifications, enables the compiler to catch errors early, and serves as documentation of your code’s intent. When in doubt, start with const and remove it only if modification is necessary.

7
Comments Analyzed
Other
Primary Language
Code Style
Category

Source Discussions