CppCoreGuidelines icon indicating copy to clipboard operation
CppCoreGuidelines copied to clipboard

Using f as function name and internal variables invites confusion. Recommending using better name for functions.

Open aerosayan opened this issue 3 years ago • 1 comments

Hello everyone,

I was reading R12 and observed that the function name f is shadowed by the file variable inside the function. Then, f is used to call the finally block.

void f(const string& name)
{
    FILE* f = fopen(name, "r");            // open the file
    vector<char> buf(1024);
    auto _ = finally([f] { fclose(f); });  // remember to close the file
    // ...
}

This is potentially very confusing to the general reader who might not know about lambda scope captures.

"Does finally use the variable or the function f?" would be their question.

R13 uses fun for the function name. Using func might be better, but fun is still good, and much easier to understand than f.

Recommending using fun at least for the function name used in R12.

Thanks :)

aerosayan avatar Aug 13 '22 13:08 aerosayan

If you want you can create a pull request with exactly that change.

beinhaerter avatar Aug 14 '22 17:08 beinhaerter

Editors call: Sure, let's change f(...) to func(...). Thanks!

hsutter avatar Sep 22 '22 21:09 hsutter