正在加载图片...
$26.2 CHOOSING THE RIGHT NAMES 881 Letter case Letter case is not significant in our notation,as it is too dangerous to let two almost identical identifiers denote different things.But strongly recommended guidelines help make class texts consistent and readable: Class names appear in all upper case:POINT,LINKED_LIST,PRICING_MODEL. Formal generic parameters too,usually with just one letter:G. Names of non-constant attributes,routines other than once functions,local entities and routine arguments appear in all lower case:balance,deposit,succ,i. Constant attributes have their first letter in upper case and the rest in lower case: Pi:INTEGER is 3.1415926524:Welcome message:STRING is "Welcome!".This applies to unique values,which are constant integers. The example of i The same convention applies to once functions,the equivalent of constants for non- was on page 648. basic types:Error window,lo.Our first example,the complex number i,remained in lower case for compatibility with mathematical conventions. This takes care of developer-chosen names.For reserved words,we distinguish two categories.Keywords such as do and class play a strictly syntactic role;they are written in lower case,and will appear in boldface (see below)in printed texts.A few reserved words are not keywords because they carry an associated semantics;written with an initial upper case since they are similar to constants,they include Current,Result,Precursor, True and False. Grammatical categories Precise rules also govern the grammatical category of the words from which identifiers are derived.In some languages,these rules can be applied without any hesitation;in English, as noted in an earlier chapter,they will leave more flexibility. See the Class Name The rule for class names has already been given:you should always use a noun,as rule on page 727. in ACCOUNT,possibly qualified as in LONG TERM SAVINGS ACCOUNT,except for the case of deferred classes describing a structural property,which may use an adjective as in NUMERIC or REDEEMABLE. Routine names should faithfully reflect the Command-Query separation principle: Procedures (commands)should be verbs in the infinitive or imperative,possibly with complements:make,move,deposit,set color. Attributes and functions (queries)should never be imperative or infinitive verbs; never call a query get value,but just value.Non-boolean query names should be nouns,such as number,possibly qualified as in last month balance.Boolean queries should use adjectives,as in fill.In English,because of possible confusions between adjectives and verbs(empry,for example,could mean "is this empty?"or§26.2 CHOOSING THE RIGHT NAMES 881 Letter case Letter case is not significant in our notation, as it is too dangerous to let two almost identical identifiers denote different things. But strongly recommended guidelines help make class texts consistent and readable: • Class names appear in all upper case: POINT, LINKED_LIST, PRICING_MODEL. Formal generic parameters too, usually with just one letter: G. • Names of non-constant attributes, routines other than once functions, local entities and routine arguments appear in all lower case: balance, deposit, succ, i. • Constant attributes have their first letter in upper case and the rest in lower case: Pi: INTEGER is 3.1415926524; Welcome_message: STRING is "Welcome!". This applies to unique values, which are constant integers. • The same convention applies to once functions, the equivalent of constants for non￾basic types: Error_window, Io. Our first example, the complex number i, remained in lower case for compatibility with mathematical conventions. This takes care of developer-chosen names. For reserved words, we distinguish two categories. Keywords such as do and class play a strictly syntactic role; they are written in lower case, and will appear in boldface (see below) in printed texts. A few reserved words are not keywords because they carry an associated semantics; written with an initial upper case since they are similar to constants, they include Current, Result, Precursor, True and False. Grammatical categories Precise rules also govern the grammatical category of the words from which identifiers are derived. In some languages, these rules can be applied without any hesitation; in English, as noted in an earlier chapter, they will leave more flexibility. The rule for class names has already been given: you should always use a noun, as in ACCOUNT, possibly qualified as in LONG_TERM_SAVINGS_ACCOUNT, except for the case of deferred classes describing a structural property, which may use an adjective as in NUMERIC or REDEEMABLE. Routine names should faithfully reflect the Command-Query separation principle: • Procedures (commands) should be verbs in the infinitive or imperative, possibly with complements: make, move, deposit, set_color. • Attributes and functions (queries) should never be imperative or infinitive verbs; never call a query get_value, but just value. Non-boolean query names should be nouns, such as number, possibly qualified as in last_month_balance. Boolean queries should use adjectives, as in full. In English, because of possible confusions between adjectives and verbs (empty, for example, could mean “is this empty?” or The example of i was on page 648. See the Class Name rule on page 727
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有