What are the most common naming conventions in C?

前端 未结 11 2347
走了就别回头了
走了就别回头了 2020-12-02 04:13

What are the naming conventions commonly use in C? I know there are at least two:

  1. GNU / linux / K&R with lower_case_functions
  2. ? name ? with UpperC
相关标签:
11条回答
  • 2020-12-02 04:28

    You should also think about the order of the words to make the auto name completion easier.

    A good practice: library name + module name + action + subject

    If a part is not relevant just skip it, but at least a module name and an action always should be presented.

    Examples:

    • function name: os_task_set_prio, list_get_size, avg_get
    • define (here usually no action part): OS_TASK_PRIO_MAX
    0 讨论(0)
  • 2020-12-02 04:35

    Coding in C#, java, C, C++ and objective C at the same time, I've adopted a very simple and clear naming convention to simplify my life.

    First of all, it relies on the power of modern IDEs (such as eclipse, Xcode...), with the possibility to get fast information by hovering or ctrl click... Accepting that, I suppressed the use of any prefix, suffix and other markers that are simply given by the IDE.

    Then, the convention:

    • Any names MUST be a readable sentence explaining what you have. Like "this is my convention".
    • Then, 4 methods to get a convention out of a sentence:
      1. THIS_IS_MY_CONVENTION for macros, enum members
      2. ThisIsMyConvention for file name, object name (class, struct, enum, union...), function name, method name, typedef
      3. this_is_my_convention global and local variables,
        parameters, struct and union elements
      4. thisismyconvention [optional] very local and temporary variables (such like a for() loop index)

    And that's it.

    It gives

    class MyClass {
        enum TheEnumeration {
            FIRST_ELEMENT,
            SECOND_ELEMENT,
        }
    
        int class_variable;
    
        int MyMethod(int first_param, int second_parameter) {
            int local_variable;
            TheEnumeration local_enum;
            for(int myindex=0, myindex<class_variable, myindex++) {
                 localEnum = FIRST_ELEMENT;
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-02 04:37

    I would recommend against mixing camel case and underscore separation (like you proposed for struct members). This is confusing. You'd think, hey I have get_length so I should probably have make_subset and then you find out it's actually makeSubset. Use the principle of least astonishment, and be consistent.

    I do find CamelCase useful to type names, like structs, typedefs and enums. That's about all, though. For all the rest (function names, struct member names, etc.) I use underscore_separation.

    0 讨论(0)
  • 2020-12-02 04:40

    Well firstly C doesn't have public/private/virtual functions. That's C++ and it has different conventions. In C typically you have:

    • Constants in ALL_CAPS
    • Underscores to delimit words in structs or function names, hardly ever do you see camel case in C;
    • structs, typedefs, unions, members (of unions and structs) and enum values typically are in lower case (in my experience) rather than the C++/Java/C#/etc convention of making the first letter a capital but I guess it's possible in C too.

    C++ is more complex. I've seen a real mix here. Camel case for class names or lowercase+underscores (camel case is more common in my experience). Structs are used rarely (and typically because a library requires them, otherwise you'd use classes).

    0 讨论(0)
  • 2020-12-02 04:43

    I'm confused by one thing: You're planning to create a new naming convention for a new project. Generally you should have a naming convention that is company- or team-wide. If you already have projects that have any form of naming convention, you should not change the convention for a new project. If the convention above is just codification of your existing practices, then you are golden. The more it differs from existing de facto standards the harder it will be to gain mindshare in the new standard.

    About the only suggestion I would add is I've taken a liking to _t at the end of types in the style of uint32_t and size_t. It's very C-ish to me although some might complain it's just "reverse" Hungarian.

    0 讨论(0)
提交回复
热议问题