Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variables and Powerful Naming Ryan Ruzich. Naming Considerations The most important consideration in naming a variable is that the name fully and accurately.

Similar presentations


Presentation on theme: "Variables and Powerful Naming Ryan Ruzich. Naming Considerations The most important consideration in naming a variable is that the name fully and accurately."— Presentation transcript:

1 Variables and Powerful Naming Ryan Ruzich

2 Naming Considerations The most important consideration in naming a variable is that the name fully and accurately describes the entity the variable represents. Examples of Bad names are x, xx, xx1, n,nn, n3,js7H29 Examples of Good Names are runningTotal, CheckTotal, velocity

3 Problem Orientation A good mnemonic name generally speaks to the problem rather than the solution. A good name expresses the *What* rather than the *how*.

4 Name Length Variable names that are too long aren’t useful either. While MaximumNumberOfPointsInOlympics is very descriptive, it’s far too long to use. An optimum length has been found to be between 9 and 15 characters in length.

5 Scope Are short variable names always bad? No, a variable I says something of it’s scope in the length of it’s name. A variable I is a temporary variable by tradition, created for a short task and promptly deleted/discarded/freed.

6 Computed-Value Qualifiers Many programs have variables that contain computed values: totals, averages, maximums, and so on. If you modify a name with a qualifier like Total, Sum, Average, Max, put the modifier at the end of the name or the beginning. revenueTotal, expenseTotal

7 Consistancy Be consistant in your variable naming. If you put the modifier at the end of the name, such as expenseTotal, don’t create a variable totalRevenue. Do one or the other, not both.

8 Looping naming Variables in loops are often created just for the purpose of keeping track of the loop. As such, short names such as I,j,k, are customary. Variables used outside of the loop should be given more descriptive names.

9 If the variable is important, it’s customary to give it a good name, even if it’s temporary.

10 Status Variables Status variables should never be named Flag. Always give them a good name to avoid confusion.

11 Temporary Varaibles Be leery of actually naming a variable Temp or temporary. Usually a more descriptive name is better, but for a truly temporary variable, it’s okay. Int A=5; Int B=6; Int temp=A; A=B; B=temp;

12 Boolean Variables Give Boolean variables names that imply a true or false existance. An example would be the variable Done, Error, Found, Success. These variable names naturally conjure a true/false status in english, and thus are preferred for boolean variables.

13 Enumerated Types Make sure to make Enumerated types descriptive as well, often by including a word in the variable name that describes what the variable is. In a list of enumerated planets, Planet_Earth and Planet_Mars are good names.

14 Naming Conventions When to use one? Often using a naming convention is wise when multiple programmers are working on the same project. You can use a pre-established Naming convention, or create a naming convention amongst those working on the project.

15 Naming Conventions Does the convention distinguish among local, class, and global data? Does the convention distinguish among type names, named constants, enumerated types, and variables? Does the convention identify input-only parameters to routines in languages that don’t enforce them? Are names formatted for Readability.

16 Avoid Naming problems Names that are midleading, names with similar meanings, different by only one or two characters, sound similar, use numerals, intentionally misspelled to make them shorter, commonly misspelled in English, names that conflict with Standard Library routine names, Totally arbitrary names, and hard to read characters.

17 Questions?


Download ppt "Variables and Powerful Naming Ryan Ruzich. Naming Considerations The most important consideration in naming a variable is that the name fully and accurately."

Similar presentations


Ads by Google