正在加载图片...
880 A SENSE OF STYLE $26.2 obscure rather than illuminate the text.Remember that every use of the feature will unambiguously indicate the class,as in partl.number where partl must have been declared with a certain type,PART or a descendant. For composite names,it is better to avoid the style,popularized by Smalltalk and also used in such libraries as the X Window System,of joining several words together and starting the internal ones with an upper-case letter,as in yearly Premium.Instead,separate components with underscores,as in yearly_premium.The use of internal upper-case letters is ugly;it conflicts with the conventions of ordinary language;and it leads to cryptic names,hence to possible errors (compare aLongAndRatherUnreadableldentifier with an even longer but perfectly clear choice of name). Sometimes,every instance ofa certain class contains a field representing an instance of another class.This suggests using the class name also as attribute name.You may for example have defined a class RATE and,in class ACCOUNT,need one attribute of type RATE,for which it seems natural to use the name rate-in lower case,according to the rules on letter case stated below.Although you should try to find a more specific name, you may,if this fails,just declare the feature as rate:RATE.The rules on identifier choice explicitly permit assigning the same name to a feature and a class.Avoid the style of prefixing the name with the,as in the rate,which only adds noise. Local entities and routine arguments The emphasis on clear,spelled-out names applies to features and classes.Local entities and arguments of a routine only have a local scope,so they do not need to be as evocative. Names that carry too much meaning might almost decrease the software's readability by giving undue weight to ancillary elements.So it is appropriate to declare local entities (here in routines of TWO WAY LIST in the Base libraries)as move (i:INTEGER)is --Move cursor i positions,or after if i is too large. local c:CURSOR;counter:INTEGER;p:like FIRST ELEMENT remove is --Remove current item;move cursor to right neighbor (of after if none). local succ,pred,removed:like first element If succ and pred had been features they would have been called successor and predecessor.It is also common to use the names new for a local entity representing a new object to be created by a routine,and other for an argument representing an object of the same type as the current one,as in the declaration for clone in GENERAL: frozen clone (other:GENERAL):like other is...880 A SENSE OF STYLE §26.2 obscure rather than illuminate the text. Remember that every use of the feature will unambiguously indicate the class, as in part1 ● number where part1 must have been declared with a certain type, PART or a descendant. For composite names, it is better to avoid the style, popularized by Smalltalk and also used in such libraries as the X Window System, of joining several words together and starting the internal ones with an upper-case letter, as in yearlyPremium. Instead, separate components with underscores, as in yearly_ premium. The use of internal upper-case letters is ugly; it conflicts with the conventions of ordinary language; and it leads to cryptic names, hence to possible errors (compare aLongAndRatherUnreadableIdentifier with an_even_longer_but_perfectly_clear_choice_of_name). Sometimes, every instance of a certain class contains a field representing an instance of another class. This suggests using the class name also as attribute name. You may for example have defined a class RATE and, in class ACCOUNT, need one attribute of type RATE, for which it seems natural to use the name rate — in lower case, according to the rules on letter case stated below. Although you should try to find a more specific name, you may, if this fails, just declare the feature as rate: RATE. The rules on identifier choice explicitly permit assigning the same name to a feature and a class. Avoid the style of prefixing the name with the, as in the_rate, which only adds noise. Local entities and routine arguments The emphasis on clear, spelled-out names applies to features and classes. Local entities and arguments of a routine only have a local scope, so they do not need to be as evocative. Names that carry too much meaning might almost decrease the software’s readability by giving undue weight to ancillary elements. So it is appropriate to declare local entities (here in routines of TWO_WAY_LIST in the Base libraries) as move (i: INTEGER) is -- Move cursor i positions, or after if i is too large. local c: CURSOR; counter: INTEGER; p: like FIRST_ELEMENT … remove is -- Remove current item; move cursor to right neighbor (of after if none). local succ, pred, removed: like first_element … If succ and pred had been features they would have been called successor and predecessor. It is also common to use the names new for a local entity representing a new object to be created by a routine, and other for an argument representing an object of the same type as the current one, as in the declaration for clone in GENERAL: frozen clone (other: GENERAL): like other is…
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有