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

Post on 17-Dec-2015

213 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Variables and Powerful Naming

Ryan Ruzich

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

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

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.

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.

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

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.

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.

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

Status Variables

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

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;

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.

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.

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.

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.

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.

Questions?

Questions?

top related