variables and powerful naming ryan ruzich. naming considerations the most important consideration in...

17
Variables and Powerful Naming Ryan Ruzich

Upload: linda-joseph

Post on 17-Dec-2015

213 views

Category:

Documents


1 download

TRANSCRIPT

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

Variables and Powerful Naming

Ryan Ruzich

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

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

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

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*.

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

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.

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

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.

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

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

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

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.

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

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.

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

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

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

Status Variables

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

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

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;

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

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.

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

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.

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

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.

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

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.

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

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.

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

Questions?

Questions?