Operands and options Programming in the Large Bertrand Meyer Lesson 18 Two possible kinds of argument to a feature: � Operands: values on which feature will operate. � Options: modes that govern how feature will operate. Example: printing a real number. The number is an operand; format properties (e.g. number of Object-Oriented Design principles significant digits, width) are options. Designing for reuse Examples: (Non-O-O) print ( real_value , number_of_significant_digits , zone_length , number_of_exponent_digits , ...) (O-O) my_window . display ( x_position , y_position , height , width , text , title_bar_text , color , ...) Last update: 7 June 2004 Chair of Softw are Engineering Chair of Softw are Engineering Programming in the Large, 2004 2 Recognizing options from operands The Option-Operand Principle Two criteria to recognize an option: Only operands should appear as arguments of a feature � There is a reasonable default value. Option values: � During the evolution of a class, operands will normally � Defaults (specified universally, per type, per object) remain the same, but options may be added. � To set specific values, use appropriate “setter” procedures Example: my_window . set_background_color ("blue") ... my_window . display Chair of Softw are Engineering Chair of Softw are Engineering 3 Programming in the Large, 2004 4 Programming in the Large, 2004 Operands and options Typical API in a traditional library (NAG) Useful checklist for options: nonlinear_ode ( equation_count : in INTEGER ; Option Default Set Accessed epsilon : in out DOUBLE ; func : procedure ( eq_count : INTEGER ; a : DOUBLE ; Window color White set_background_color background_color eps : DOUBLE ; b : ARRAY [ DOUBLE ] ; cm : pointer Libtype ); Hidden? No set_visible hidden left_count , coupled_count : INTEGER … ) set_hidden [And so on. Altogether 19 arguments, including: � 4 in out values; � 3 arrays, used both as input and output; � 6 functions, each with 6 or 7 arguments, of which 2 or 3 arrays!] Chair of Softw are Engineering Chair of Softw are Engineering 5 Programming in the Large, 2004 6 Programming in the Large, 2004 1
The EiffelMath routine Naming (classes, features, variables…) Traditional advice (for ordinary application programming): ... Set up the non-default values ... � Choose meaningful variable names! e . solve ... Solve the problem, recording the answer in x and y ... Chair of Softw are Engineering Chair of Softw are Engineering Programming in the Large, 2004 Programming in the Large, 2004 7 8 Naming example, original choices Naming example, revised choices Class Features Class Features ARRAY enter entry ARRAY put item STACK push top pop STACK put item remove QUEUE add oldest remove_oldest QUEUE put item remove HASH_TABLE insert value delete HASH_TABLE put item remove Chair of Softw are Engineering Chair of Softw are Engineering 9 Programming in the Large, 2004 10 Programming in the Large, 2004 Naming rules Some standard names Queries (non-boolean): -- Some rejected names: Achieve consistency by systematically using a set of count, capacity standardized names. if s . addable then item s . add ( v ) to_external , from_external end Emphasize commonality over differences. Commands: if s . deletable then s . delete ( v ) put , extend , replace , force Differences will be captured by: end wipe_out, remove , prune � Signatures (number and types of arguments and make -- For creation result). Boolean queries: � Assertions. writable , readable , extendible , prunable � Comments. is_empty , is_ full -- Usual invariants: 0 <= count ; count <= capacity is_empty = ( count = 0) is_full = ( count = capacity ) Chair of Softw are Engineering Chair of Softw are Engineering 11 Programming in the Large, 2004 12 Programming in the Large, 2004 2
Grammatical rules Feature categories Procedures (commands): verbs in infinitive form. class Examples: make , put , display . C inherit Boolean queries: adjectives … Example: full (older convention) Now recommended: is_full , is_first . feature -- Category 1 Convention: Choose form that should be false by default … Feature declarations Example: is_erroneous . feature { A , B } -- Category 2 This means that making it true is an event worth talking about … Feature declarations feature { NONE } -- Category n Other queries: nouns or adjectives. Examples: count , error _ window . … Feature declarations invariant Do not use verbs for queries, in particular functions; this goes with … Command-Query Separation Principle end Example: next_item , not get_next_item Chair of Softw are Engineering Chair of Softw are Engineering Programming in the Large, 2004 Programming in the Large, 2004 13 14 Feature categories Obsolete features and classes Standard categories (the only ones in EiffelBase): A constant problem in information technology: How do we reconcile progress with the need to protect Initialization � � Status setting the installed base? Creation � Cursor movement Access � � Element change Obsolete features and classes support smooth evolution. Measurement � � Removal Comparison � In class ARRAY : � Resizing Status report � � Transformation Basic queries enter ( i : V ; v : T ) is Basic commands obsolete "Use ` put (value, index)’" Conversion � Inapplicable � do Duplication � Implementation � put ( v , i ) Basic operations � Miscellaneous � end Transformations Internal Chair of Softw are Engineering Chair of Softw are Engineering 15 Programming in the Large, 2004 16 Programming in the Large, 2004 Obsolete classes Complementary material OOSC2: class ARRAY_LIST [ G ] � Chapter 22: How to find the classes obsolete � Chapter 23: Principles of class design "[ Use MULTI_ARRAY_LIST instead (same semantics, but new name ensures more consistent terminology). Caution: do not confuse with ARRAYED_LIST (lists implemented by one array each). ] " inherit MULTI_ARRAY_LIST [ G ] end Chair of Softw are Engineering Chair of Softw are Engineering 17 Programming in the Large, 2004 18 Programming in the Large, 2004 3
End of lecture 18 Chair of Softw are Engineering 4
Recommend
More recommend