C ++ programming naming convention

Source: Internet
Author: User
C ++ programming naming convention 0 Preface

Based on years of work experience and other naming rules, my personal experience is more standard and is now applied to my development team.

1 Naming rules The file name, function name, and variable name should be descriptive and should not be excessively abbreviated. type variables are nouns, and function names are verbs or Verbs + nouns. The function name must be directive and can be abbreviated to be very general. 2 File Name

The C ++ file should end with. cpp, And the header file should end with. H. All file names should be in lower case, and the file names should be the same as the class names.

Example:

Publictools. h

Publictools. cpp

3 Type name

Types include class, struct, typedef, and enum.

Type name: the first letter of each word is capitalized.

Example:

Class ):

Class testclass

{

};

Struct (struct ):

Struct teststruct

{

};

Type Definition (typedef ):

Typedef struct testtype

{

};

Enumeration (Enum ):

Enum testenum

{

};

4 Variable name

The first letter of a common variable is lowercase, the member variable ends with "_", and the function parameter starts. Start with the global variable g _ and start with the static variable S.

Example:

Common variables:

Int index;

Char type;

String name;

Member variables:

Int index _;

Function Parameters

Void setindex (INT _ index)

{

};

Global variables:

Int g_count;

Static variables

Int s_number;

5 Constant name

All uppercase letters are separated by underscores.

Example:

Const string max_filename255;

6 Function Name

The first letter is capitalized. The value matches the set-value function with the variable name.

Example:

Int index _;

Int getindex ()

{

Returnindex _;

};

Void setindex (INT _ index)

{

Index _ = _ index;

};

7 Namespace

All lowercase letters.

Example:

Namespace mynamespace

{

};

8 Type name

The first word is fully written, and the first letter of the next word is capitalized.

Example:

Struct teststruct

{

Int number,

String studentname

};

Enum testenum

{

Errorin,

Errorout

};

9 Macro name

All uppercase letters are separated by underscores.

Example:

# Define pi_raud3.14159265

10 # Define header file protection naming

All uppercase.

Example:

# Ifndef foo_bar_baz_h _

# Define foo_bar_baz_h _

...

# endif // foo_bar_baz_h _;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.