Describe how the use of named constants can provide advantages over the use of literal values.

What will be an ideal response?


There are several good reasons to use the named constant rather than the literal one:
- The number 20 is more easily recognized as the number of departments if it is associated with an identifier. Using named constants makes your programs easier to read and understand.?- If the number of departments in your organization changes, you would change the value of NUMBER_OF_DEPTS at one location within your program-where the constant is defined-rather than searching for every use of 20 to change it to a different number. Being able to make the change at one location saves you time, and prevents you from missing a reference to the number of departments.?- Even if you are willing to search for every instance of 20 in a program to change it to the new department number value, you might inadvertently change the value of one instance of 20 that is being used for something else, such as a payroll deduction value.?- Using named constants reduces typographical errors. For example, if you must include 20 at several places within a program, you might inadvertently type 10 or 200 for one of the instances, and the compiler will not recognize the mistake. However, if you use the identifier NUMBER_OF_DEPTS, the compiler will ensure that you spell it correctly.?- When you use a named constant in an expression, it stands out as different from a variable. For example, in the following arithmetic statement, it is easy to see which elements are variable and which are constant because the constants have been named conventionally using all uppercase letters and underscores to separate words:
double payAmount = hoursWorked * STD_PAY_RATE - numDependents * DEDUCTION;?Although many programmers use named constants to stand for most of the constant values in their programs, many make an exception when using 0 or 1.

Computer Science & Information Technology

You might also like to view...

Centrex service provides

a. a local switch on the customer-premises b. similar features to a PBX, but from the central office switch c. no advanced features d. a customer-owned telecommunications solution

Computer Science & Information Technology

Before disposing of computer equipment, the ________ must be professionally cleared or damaged

Fill in the blank(s) with correct word

Computer Science & Information Technology

The ____ character is used to display a fixed floating-point integer.

A. d B. f C. o D. x

Computer Science & Information Technology

If the database structure changes, the user cannot access and use the view.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology