正在加载图片...
644 GLOBAL OBJECTS AND CONSTANTS $18.1 Using symbolic constants A rule of software style,the Symbolic Constant principle,states that when an algorithm "Mamifest and sym refers to a certain value-a number,a character,a string...-it should almost never use bolic constants". it directly.Instead,a declaration should associate a name with the value,so that the page 884.See also “odular continu- algorithm can use the name(known as a symbolic constant)rather than the value(known ity"page 44. as a manifest constant).Two reasons justify this principle: Readability:someone who reads your software may not understand what the value 50 is doing in a certain algorithm;if instead you use the symbolic constant US states count everything is clear. Extendibility:in practice,with a few exceptions (such as the value of n,unlikely to change soon),the only constant thing about constants is change.To update the value of a constant it suffices,if you have been using symbolic constants,to change one declaration.This is much nicer than having to chase throughout the software for all the places that may have relied on the earlier value. The principle permits using manifest constants(hence the word"almost"above)for zero elements of various operations,as in a loop from i:=/until i>n...iterating over the elements of an array whose numbering follows the default convention of starting at 1. (But n should be symbolic,not manifest.) Although few software developers apply the Symbolic Constant principle as systematically as they should,the benefits of declaring a symbolic constant are well worth the small extra effort.So we need a clear and simple way of defining symbolic constants in an O-O framework. Constant attributes A symbolic constant,like everything else,will be defined in a class.We will simply treat a constant value as an attribute which happens to have a fixed value,the same for all instances of the class. For the syntax,we can reuse the keyword is which already serves to introduce routines;only here it will be followed by a value of the appropriate type,rather than by an algorithm.The following examples include one for each of the basic types /NTEGER, BOOLEAN,REAL and CHARACTER: Zero:INTEGER is 0 Ok:BOOLEAN is True Pi:REAL is3.1415926524 Backslash:CHARACTER is' Backslash is of type CHARACTER,its value a single character.Constants of string type, denoting character strings of arbitrary length,will be discussed below. As these examples illustrate,the recommended style convention for names of constant attributes is to start with a capital letter,with the rest in lower case. A descendant may not redefine the value of a constant attribute.644 GLOBAL OBJECTS AND CONSTANTS §18.1 Using symbolic constants A rule of software style, the Symbolic Constant principle, states that when an algorithm refers to a certain value — a number, a character, a string… — it should almost never use it directly. Instead, a declaration should associate a name with the value, so that the algorithm can use the name (known as a symbolic constant) rather than the value (known as a manifest constant). Two reasons justify this principle: • Readability: someone who reads your software may not understand what the value 50 is doing in a certain algorithm; if instead you use the symbolic constant US_states_ count everything is clear. • Extendibility: in practice, with a few exceptions (such as the value of π, unlikely to change soon), the only constant thing about constants is change. To update the value of a constant it suffices, if you have been using symbolic constants, to change one declaration. This is much nicer than having to chase throughout the software for all the places that may have relied on the earlier value. The principle permits using manifest constants (hence the word “almost” above) for zero elements of various operations, as in a loop from i := 1 until i > n … iterating over the elements of an array whose numbering follows the default convention of starting at 1. (But n should be symbolic, not manifest.) Although few software developers apply the Symbolic Constant principle as systematically as they should, the benefits of declaring a symbolic constant are well worth the small extra effort. So we need a clear and simple way of defining symbolic constants in an O-O framework. Constant attributes A symbolic constant, like everything else, will be defined in a class.We will simply treat a constant value as an attribute which happens to have a fixed value, the same for all instances of the class. For the syntax, we can reuse the keyword is which already serves to introduce routines; only here it will be followed by a value of the appropriate type, rather than by an algorithm. The following examples include one for each of the basic types INTEGER, BOOLEAN, REAL and CHARACTER: Zero: INTEGER is 0 Ok: BOOLEAN is True Pi: REAL is 3.1415926524 Backslash: CHARACTER is '\' Backslash is of type CHARACTER, its value a single character. Constants of string type, denoting character strings of arbitrary length, will be discussed below. As these examples illustrate, the recommended style convention for names of constant attributes is to start with a capital letter, with the rest in lower case. A descendant may not redefine the value of a constant attribute. “Manifest and sym￾bolic constants”, page 884. See also “Modular continu￾ity”, page 44
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有