convention for naming identifiers

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,

This is actually one of those points I'd love to disambiguate, or at least make consistent.

Glad to hear, Ted! What's the rule you have in mind regarding this?

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.

This is actually one of those points I'd love to disambiguate, or at least make consistent.

Glad to hear, Ted! What's the rule you have in mind regarding this?

How about lower-case if it's a cheap operation (e.g a simple accessor) and upper if not ?

-Argiris

IMHO that leads to the random coding conventions we have now. :slight_smile:

I personally prefer that functions/methods start with lower-case characters, and types start with upper-case letters. I have to admit, however, that it isn’t a convention I have strictly adopted in my own code, but wouldn’t mind doing so.

Since we are talking about preferences that are against the majority of llvm/clang code, how about lower case for parameters and local variables.

That would be great too.

I like it. Since this rule doesn't affect the function signatures, we
can easily bring legacy code into conformance as needed without
breaking existing clients.

I haven't heard from Chris yet, who I assume is in charge of the
Coding Standards. Chris, what do you think? Shall I draft a patch?
Thanks,

I've been somewhat intentionally staying out of it :).

My thoughts: I completely agree we should have a standard, it is a mishmash and embarrassing not to :slight_smile:

I think that method names should either be javaStyle or StrictCamelCase. I don't have a preference either way. I tend to use isFoo() and DoFoo() in my code, but that isn't a useful convention to standardize.

There is various code that tries to follow the_stl_conventions, with methods like empty() etc. We have to decide that they are either exceptions or that they should be changed.

I don't have a strong opinion about type names, saying they should start with a capital letter makes sense to me.

-Chris

Awesome! I'll try to come up with a patch soon. Thanks,