1/51
https://www.learncpp.com/cpp-tutorial/keywords-and-naming-identifiers/
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
92
Because they are reserved and have special meaning in the language
c) 2myVariable
A letter (uppercase or lowercase) or an underscore
b) int Value;
Identifiers starting with an underscore are typically reserved for OS, library, or compiler use
It doesn’t specify the unit (e.g., seconds, minutes, hours)
c) int numApples;
A convention where multi-word names start with a lowercase letter and each subsequent word starts with an uppercase letter (e.g., myVariableName)
Code is read more often than it is written, so writing readable code saves time in the long run
Is this conventional, unconventional, or invalid? - int sum {};
Conventional
Is this conventional, unconventional, or invalid? - int _apples {};
Unconventional -- variable names should not start with an underscore.
Is this conventional, unconventional, or invalid? - int VALUE {};
Unconventional -- single word names should be in all lower case.
Is this conventional, unconventional, or invalid? - int my variable name {};
Invalid -- variable names can not contain spaces.
Is this conventional, unconventional, or invalid? - int TotalCustomers {};
Unconventional -- variable names should start with a lower case letter.
Is this conventional, unconventional, or invalid? - int void {};
Invalid -- void is a keyword.
Is this conventional, unconventional, or invalid? - int numFruit {};
Conventional
Is this conventional, unconventional, or invalid? - int 3some {};
Invalid -- variable names can not start with a number.
Is this conventional, unconventional, or invalid? - int meters_of_pipe {};
Conventional
C++ special identifies
override, final, import, and module
Name of variable is called
An identifier
Rule 1 of identifier naming
The identifier can not be a keyword. Keywords are reserved.
Rule 2 of identifier naming
The identifier can only be composed of letters (lower or upper case), numbers, and the underscore character.
That means the name can not contain symbols (except the underscore) nor whitespace (spaces or tabs).
Rule 3 of identifier naming
The identifier must begin with a letter (lower or upper case) or an underscore. It can not start with a number.
Rule 4 of identifier naming
C++ is case sensitive, and thus distinguishes between lower and upper case letters. nvalue
is different than nValue
is different than NVALUE
.
It is conventional in C++ that variable names should begin with?
a lowercase letter.
If the variable name is a single word or acronym?
the whole thing should be written in lowercase letters.
Identifier names that start with a capital letter are typically used for
user-defined types
If the variable or function name is multi-word, there are two common conventions:
Either use underscores as spaces, or capitalize each word to separate them
When working in an existing program…
use the conventions of that program (even if they don’t conform to modern best practices)
Use modern best practices when…
you’re writing new programs.
Avoid naming your identifiers starting with an…
underscore. That’s typically reserved for OS, library, and/or compiler use.
The name of your identifiers should
make clear what the value they are holding means
int ccount | Bad |
What does the c before “count” stand for? |
int customerCount | Good |
Clear what we’re counting |
int i | Either |
Okay if use is trivial, bad otherwise |
int index | Either |
Okay if obvious what we’re indexing |
int totalScore | Either |
Okay if there’s only one thing being scored, otherwise too ambiguous |
int _count | Bad |
Do not start names with underscore |
int count | Either |
Okay if obvious what we’re counting |
int data | Bad |
What kind of data? |
int minutesElapsed | Either |
Okay if obvious what this is elapsed from |
int x1, x2 | Either |
Okay if use is trivial, bad otherwise |
int userinput1, userinput2 | Bad |
Hard to differentiate between the two due to long name |
int numApples | Good |
Descriptive |
int monstersKilled | Good |
Descriptive |
Code is read more often than it is written, so…
any time saved while writing the code is time that every reader, including future you, will waste while reading it
For variable declarations, it can be useful to use…
a comment to describe what a variable is going to be used for, or to explain anything else that might not be obvious.
An identifier that exists for only a few statements (e.g. in the body of a short function)
can have a shorter name.
An identifier that is accessible from anywhere
might benefit from a longer name.
An identifier that represents a non-specific number (e.g. anything the user provides)
can have a shorter name.
An identifier that represents a specific value (e.g. the length of an inseam in millimeters)
should have a longer name.