CSC/ECE 517 Fall 2010/ch5 5b mt

From Expertiza_Wiki
Jump to navigation Jump to search

Variable Naming Conventions

Variables are used in all computer programming languages and are used for various reasons; such as, holding the value of a constant, holding the value of something used many times throughout the program, or used briefly for counting. The names used for each variable are more difficult to choose than simply making any word or word combination. The rest of this article is dedicated to helping the novice programmer choose good variable names and convention-styles for when writing their code.


Introduction

A Variable is a name used within a program that holds the value of something that is known or unknown. For example, the variable "firstName" might be used to hold the string of letters that comprises a person's first name. The variable may not be set at the beginning of the program and it may change multiple times after being set. Naming conventions are a set of rules used to guide the programmer when creating the names of variables. Using the previous example, if there were no naming conventions, the variable could be named "fn", which is ambiguous and would be hard to follow when reading through many lines of code. Therefore, general naming conventions, which are incorporated by many languages are needed to aid the ability to follow the code without having the author present. General naming conventions are not perfect; thus, many coding languages have adopted their own type of convention. Furthermore, there are universally used variables and special types of variables that are used without regard for a language type.


||An example of multiple cites for the same resource [1] [1] If it doesn't have multiple cites for the same resource just leave off the a and b

||

The Good and The Bad

Imagine you are diving into someone's code, and you find something like this...

 1  function mystery_function(var1, var2):
 2      for each vertex v in var1:
 3          var3[v] := infinity ;
 4          var4[v] := undefined ;
 5      end for ;
 6      var3[var2] := 0 ;
 7      var5 := the set of all nodes in var1 ;
 8      while var5 is not empty:
 9          var7 := vertex in var5 with smallest var3[] ;
10          if var3[var7] = infinity:
11              break ;
12          fi ;
13          remove var7 from var5 ;
14          for each neighbor var6 of var7:
15              var8 := var3[var7] + dist_between(var7, var6) ;
16              if var8 < var3[var6]:
17                  var3[var6] := var8 ;
18                  var4[var6] := var7 ;
19              fi  ;
20          end for ;
21      end while ;
22      return var3[] ;
23  end mystery_function.

Clearly, code without meaningful variables greatly complicates reading what the algorithm is supposed to do. Even with a slightly better example, using really short variable names, can be difficult to understand.

 1  function mystery_function(G, s):
 2      for each vertex v in G:
 3          d[v] := infinity ;
 4          p[v] := undefined ;
 5      end for ;
 6      d[s] := 0 ;
 7      Q := the set of all nodes in G ;
 8      while Q is not empty:
 9          u := vertex in Q with smallest d[] ;
10          if d[u] = infinity:
11              break ;
12          fi ;
13          remove u from Q ;
14          for each neighbor v of u:
15              a := d[u] + dist_between(u, v) ;
16              if a < d[v]:
17                  d[v] := a ;
18                  p[v] := u ;
19              fi  ;
20          end for ;
21      end while ;
22      return d[] ;
23  end mystery_function.

Finally, here is the same algorithm, with the original variable names from the source-example (include reference number here).

 1  function mystery_function(Graph, source):
 2      for each vertex v in Graph:
 3          dist[v] := infinity ;
 4          previous[v] := undefined ;
 5      end for ;
 6      dist[source] := 0 ;
 7      Q := the set of all nodes in Graph ;
 8      while Q is not empty:
 9          u := vertex in Q with smallest dist[] ;
10          if dist[u] = infinity:
11              break ;
12          fi ;
13          remove u from Q ;
14          for each neighbor v of u:
15              alt := dist[u] + dist_between(u, v) ;
16              if alt < dist[v]:
17                  dist[v] := alt ;
18                  previous[v] := u ;
19              fi  ;
20          end for ;
21      end while ;
22      return dist[] ;
23  end mystery_function.

So finally, with meaningful variable names in place, it is much easier to understand the purpose of the code. In fact, many may recognize it as Dijkstra's algorithm. Comparing this well-named example to our first example shows just how important naming can be when writing code.


Notable Naming Conventions

General Conventions


Hungarian

Universally Used Variables

Special Variable Conventions


Language Specific Conventions

||This is our second subtopic||


Ruby

Ruby does not offer many suggestions for naming variables. What it does offer is how certain variables must begin. A normal variable is any concatenation of letter or number as long as a letter or underscore '_' is used at the beginning. An instance variable, which occurs in a class as multiple copies, begins with '@'. A class variable only has one copy used for all instances of a class, begins with '@@'. A global variable, which can be used from anywhere within the program, begins with '$'.

Java

C/C++/C#

.NET

Quick Access Table

Type Hungarian Ruby Java C .NET
String String String String String String
Number Number Number Number Number Number

Dos And Don'ts

When picking your variable names, here are a few Do and Don'ts to consider.

* Do use meaningful words
* Do use intention-revealing names (p 84 java book)
* Do be descriptive
* Do keep consistent in your naming convention
* Don't ever name your variables, "data" ( http://www.oreillynet.com/onlamp/blog/2004/03/the_worlds_two_worst_variable.html )
* Don't give up!

Conclusions

Variables are inevitable in programming. The naming of variables must be taken with caution so that they make sense to any who read through the code. When creating names of variables the author should take into account the language of the code, the purpose of the variable, and the type of value the variable will hold. Adhering to naming conventions is not a requirement, but they are merely guides that a programmer can use to determine the best way to name the program's variables.

References

1. a, b Author last, FI. (2010, April 26). Title of webpage. Retrieved October 15, 2010, from site name (Github): http://github.com/

Additional Resources