Why Name It?

◀ My Naming Conventions▶ Programming Misconceptions
Amazon Sometimes naming a variable is not necessary; you can just put everything into a big expression. For example, to convert Fahrenheit to Celsius, you need to subtract 32 degrees from the Fahrenheit temperature, multiply the result by 5, and then divide the result by 9.

If you want to write a program that accepts a Fahrenheit temperature from the user then outputs the corresponding Celsius temperature, you can write the following in main():
double celsius, fahrenheit;
cout << "Please enter a Fahrenheit temperature: ";
cin >> fahrenheit;
celsius = (fahrenheit-32)*5/9;
cout << "Peter, the corresponding Celsius temperature is " << celsius << endl;
However, you also can do:
double fahrenheit;
cout << "Please enter a Fahrenheit temperature: ";
cin >> fahrenheit;
cout << "Peter, the corresponding Celsius temperature is " << (fahrenheit - 32)*5/9 << endl;
This simple example illustrates an important point: Whenever you think you need to create a variable, think twice. Essentially, the questions you need to ask yourself are:

* Will you use the variable just once or over and over again?
* Will the use of the variable greatly enhance your program’s readability (i.e. no magic numbers)?
* Will the use of the variable decrease the running time of your program?

If you need to use the result of an expression many times, you may want to store it in a variable. If the use of the variable can greatly increase your program’s readability, it’s a good idea to use it.

In the above example, a person seeing your program probably understands immediately what the program does if you create a variable called celsius. A person unfamiliar with the conversion between Celsius and Fahrenheit may not know what (fahrenheit - 32)*5/9 means.

This is just a small example, but in a much larger program, you may need to deal with much more expressions. Then you will make a decision whether you should create a variable for each expression.

Lastly, you need to know if the use of the variable decreases the running time of your program. Consider the following program:
#include <iostream>
using namespace std;
#include <ctime>	/* or <time.h> */
#include <cmath>	/* or <math.h> */

int main(int argc, char **argv) {
  int i, i2, i3;
  clock_t start;
  double num, sq;	

  cout << "Please enter a positive number: ";
  cin >> num;
  start = clock();
  for(i=0; i<sqrt(num); i++)
    for(i2=0; i2<sqrt(num); i2++)
      for(i3=0; i3<sqrt(num); i3++)
        ;

  cout << "Total is " << (float)(clock() - start) / CLOCKS_PER_SEC << " seconds\n";
}
As you can see, the program uses the expression sqrt(num) in the for loops. After several sample runs with num being 10000, the average time it takes is about 1.268 seconds on my machine.

However, as you can see, we can just calculate it once and store the result in a variable. Here is a version that stores the result of sqrt(num) in a variable and uses it whenever the program needs it.
#include <iostream>
using namespace std;
#include <ctime>	/* or <time.h> */
#include <cmath>	/* or <math.h> */

int main(int argc, char **argv) {
  int i, i2, i3;
  clock_t start;
  double num, sq;	

  cout << "Please enter a positive number: ";
  cin >> num;
  sq = sqrt(num);
  start = clock();
  for(i=0; i<sq; i++)
    for(i2=0; i2<sq; i2++)
      for(i3=0; i3<sq; i3++)
        ;

  cout << "total is " << (float)(clock() - start) / CLOCKS_PER_SEC << " seconds\n";
}
This version runs 0.026 seconds on average on my machine, a dramatic improvement.The square root of a value takes relatively long time to calculate, so it is a good idea to do such computations once.

Again, this is a rather small example. In a program the result of an expression may need to be computed many times, given different values in the expression. In any case, whenever you want to use a variable, think about these questions before you make your move.

If you seize a bat and put it on the ground, it won’t be able to fly anymore. They need to climb to a high spot, then fall into flight.
◀ My Naming Conventions▶ Programming Misconceptions

fShare
Questions? Let me know!