Saturday, 15 August 2015

Identifiers, and naming them

Leave a Comment
Identifiers, and naming them
The name of a variable, function, class, or other object in C++ is called an identifier. C++ gives you a lot of flexibility to name identifiers as you wish. However, there are a few rules that must be followed when naming identifiers:
  • The identifier can not be a keyword. Keywords are reserved.
  • 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.
  • The identifier must begin with a letter (lower or upper case) or an underscore. It can not start with a number.
  • C++ distinguishes between lower and upper case letters. nvalue is different than nValue is different thanNVALUE.
Now that you know how you can name a variable, let’s talk about how you should name a variable or function.
First, it is a convention in C++ that variable names should begin with a lowercase letter. If the variable name is one word, the whole thing should be written in lowercase letters.
int value; // correct
int Value; // incorrect (should start with lower case letter)
int VALUE; // incorrect (should start with lower case letter)
int VaLuE; // incorrect (see your psychiatrist) ;)
Identifier names that start with a capital letter are typically used for structs, classes, and enumerations (all of which we will cover later).
If the variable or function name is multi-word, there are two common conventions: separated by underscores, or intercapped (sometimes called CamelCase).
1  int my_variable_name; // correct (separated by underscores)
2  int myVariableName; // correct (intercapped/camelCase)
3  int my variable name; // incorrect (spaces not allowed)
4  int MyVariableName; // incorrect (should start with lower case letter)
In this tutorial, we will typically use the intercapped approach because it’s easier to read (it’s easy to mistake an underscore for a space in dense blocks of code). But it’s common to see either -- the C++ standard library uses the underscore method for both variables and functions. Sometimes you’ll see a mix of the two: underscores used for variables and intercaps used for functions.
Second, you should avoid naming your identifiers starting with an underscore, as these names are typically reserved for OS, library, and/or compiler use.
Third, and this is perhaps the most important rule of all, give your identifiers names that actually describe what they are. It is typical for inexperienced programmers to make variable names as short as possible, either to save on typing or because they figure the meaning is obvious. This is almost always a mistake. Ideally, variables should be named in a way that would help someone who has no idea what your code does be able to figure it out as quickly as possible. In 3 months, when you look at your program again, you’ll have forgotten how it works, and you’ll thank yourself for picking variable names that make sense. The more complex the code the variable is being used in, the better name it should have.
int ccountBadNobody knows what a ccount is
int customerCountGoodClear what we’re counting
int iBad*Generally bad unless use is trivial, such as loop variables
int indexEitherOkay if obvious what we’re indexing
int totalScoreGoodDescriptive
int _countBadDo not start variable names with underscore
int countEitherOkay if obvious what we’re counting
int dataBadWhat kind of data?
int value1, value2EitherCan be hard to differentiate between the two
int numberOfApplesGoodDescriptive
int monstersKilledGoodDescriptive
int x, yBad*Generally bad unless use is trivial, such as in trivial mathematical functions
* Note: it is okay to use trivial variable names for variables that have a trivial use, such as loop variables, or trivial mathematical functions.
Fourth, a clarifying comment can go a long way. For example, say we’ve declared a variable named numberOfCharsthat is supposed to store the number of characters in a piece of text. Does the text “Hello World!” have 10, 11, or 12 characters? It depends on whether we’re including whitespace or punctuation. Rather than naming the variablenumberOfCharsIncludingWhitespaceAndPunctuation, which is rather lengthy, a well placed comment on the declaration line should help the user figure it out:
If You Enjoyed This, Take 5 Seconds To Share It

0 Questions:

Labels

Additional Questions Advantages of C Language Advantages of C over C++ and naming them array programs arrays Basic Problems c language projects C plus plus project C Primer Plus c programming sample projects C programs C Projects C Structures C Tutorials C/C++ Shapes C++ Basic Problems C++ Books Solution C++ Functions C++ Language C++ Loops and Decisions C++ Program Examples C++ Programs c++ projects C++ Shapes Code C++ Structures C++ Tips and Tricks C++ Tutorials Control Structures CPP Enum CPP Pointers CPP Vector cppexamples CppStruct Cryptography DataStructures difference between a keyword and a user-defined identifier Disadvantages of C Language Ds malik Ds malik c++ programming Chapter 2: Basic Elements of C++ Solution Ds malik c++ programming Chapter 4: Control Structures I (Selection) Solution else if nested if File Handling in C++ fstream History of C language how to learn c language how to learn c programming Identifiers if statement ifstream language c learn c programming Learn C++ Linked List LinkedList list of all the C++ keywords Loops Loops practice using shapes Magic Squares Puzzle without using Recursion MathProblems nested loop Object Oriented Programming in C++ by Robert Lafore - 4th Edition Chapter 1 Object Oriented Programming in C++ by Robert Lafore - 4th Edition Chapter 2 Object Oriented Programming in C++ by Robert Lafore - 4th Edition Chapter 3 Object Oriented Programming in C++ by Robert Lafore - 4th Edition Chapter 4 Object-Oriented Programming in C++ Fourth Edition Solution Manual ofstream OOP OOP Quiz oop1ch2e oop1ch3e output QTProgramming Recursion Simple C plus plus Programs solution Sorting Strings Temperature 5 Scale Converter Tools of the trade Urdu Training Videos