正在加载图片...
$26.2 CHOOSING THE RIGHT NAMES 883 For boolean queries: Standard empty Is the structure devoid of elements? names for boolean full Is there no more room in the representation to add elements?(Normally the same as count capacity.) queries has Is a certain element present?(The basic membership test.) extendible Can an element be added?(May serve as a precondition to extend.) prunable Can an element be removed?(May serve as a precondition to remove and prune.) readable Is there an accessible element?(May serve as precondition to item and remove.) writable Is it possible to change an element?(May variously serve as precondition to extend,replace,put etc.) A few name choices which may seem strange at first are justified by considerations of clarity and consistency.For example prune goes with prunable and extend with extendible;delete and add might seem more natural,but then s.deletable and s.addable would carry the wrong connotation,since the question is not whethers can be deleted or added but whether we can add elements to it or delete elements from it.The verbs prune and extend,with the associated queries,convey the intended meaning. The benefits of consistent naming The set of names sketched above is one of the elements that most visibly contribute to the distinctive style of software construction developed from the principles of this book. Is the concern for consistency going too far?One could fear that confusion could result from routines that bear the same name but internally do something different.For example item for a stack will return the top element,and for an array will return an element corresponding to the index specified by the client. With a systematic approach to O-O software construction,using static typing and Design by Contract,this fear is not justified.To learn about a feature,a client author can rely on four kinds of property,all present in the short form of the enclosing class: Fl·Its name. F2.Its signature(number and type ofarguments if a routine,type ofresult if a query). F3.Its precondition and postcondition if any. F4.Its header comment. A routine also has a body,but that is not part of what client authors are supposed to use.§26.2 CHOOSING THE RIGHT NAMES 883 For boolean queries: A few name choices which may seem strange at first are justified by considerations of clarity and consistency. For example prune goes with prunable and extend with extendible; delete and add might seem more natural, but then s ● deletable and s ● addable would carry the wrong connotation, since the question is not whether s can be deleted or added but whether we can add elements to it or delete elements from it. The verbs prune and extend, with the associated queries, convey the intended meaning. The benefits of consistent naming The set of names sketched above is one of the elements that most visibly contribute to the distinctive style of software construction developed from the principles of this book. Is the concern for consistency going too far? One could fear that confusion could result from routines that bear the same name but internally do something different. For example item for a stack will return the top element, and for an array will return an element corresponding to the index specified by the client. With a systematic approach to O-O software construction, using static typing and Design by Contract, this fear is not justified. To learn about a feature, a client author can rely on four kinds of property, all present in the short form of the enclosing class: F1 • Its name. F2 • Its signature (number and type of arguments if a routine, type of result if a query). F3 • Its precondition and postcondition if any. F4 • Its header comment. A routine also has a body, but that is not part of what client authors are supposed to use. empty Is the structure devoid of elements? full Is there no more room in the representation to add elements? (Normally the same as count = capacity.) has Is a certain element present? (The basic membership test.) extendible Can an element be added? (May serve as a precondition to extend.) prunable Can an element be removed? (May serve as a precondition to remove and prune.) readable Is there an accessible element? (May serve as precondition to item and remove.) writable Is it possible to change an element? (May variously serve as precondition to extend, replace, put etc.) Standard names for boolean queries
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有