[cfe-dev] convention for naming identifiers

Zhanyong Wan (λx.x x) wan at google.com
Wed Nov 17 10:21:33 PST 2010


Hi,

I didn't find any rule in the LLVM Coding Standards
(http://llvm.org/docs/CodingStandards.html) regarding how types,
functions, and variables should be named.  Perhaps as a result of
that, the LLVM/Clang codebase doesn't follow a naming style
consistently, and we can find non-descriptive names used here and
there.  This has caused me difficulty understanding the code a number
of times.  From what I heard, my experience is not unique.

I propose to impose some rules to promote good naming styles.  I
understand that it's hard to get people to agree on any particular
style, so we may not want to set the rules too strict.  Still, I think
there are non-controversial things that we can put in the Coding
Standards today.  Here are what I have in mind:

1. Names should be descriptive.  Try to pick a name that closely
matches the semantics or role of the entity it names, without going
overboard.  Avoid abbreviations unless they are well known.  Local
variables whose role is obvious can be an exception to this rule.  For
example:

F is not a good name for a member variable (it might be OK as a local
variable).  Factory is a better name.  PlaneFactory is even better.

2. In general, type names should be nouns and function names should be
verb phrases (OpenFile(), IsPointer(), etc).

3. Type names should be CamelCase,

One question is when a function/variable name should start with a
lower-case letter ("Should I call it getLoc() or GetLoc()?").  Perhaps
we can leave it unspecified for now though.

I would appreciate your thoughts on this.  Thanks,

-- 
Zhanyong



More information about the cfe-dev mailing list