meeting -*- outline -*- objectives: discuss the heuristics in a Reil's chapter 5, give more examples of subtyping and polymorphism, show how to use the Eclipse editor/IDE. * bad-statements-example This example relies on the BadStatements eclipse project found in a subdirectory of this directory. To import this directory into eclipse, make a new eclipse project, BadStatements, and then import everything except the topmost directory. ** context The basic idea is to make a small programming language interpreter for a language of statements. ------------------------------------------ STATEMENTS GRAMMAR ::= print(); | assign(, ); | decrement(); | isZero(); | isNonZero(); | | while do | ( ) ::= ------------------------------------------ ** the bad "design" ------------------------------------------ |----------------| | <> | | Statement | |----------------| |----------------| | run(): boolean | |----------------| / \ | |------------------| | Block | |------------------| | stmt1: Statement | | stmt2: Statement | |------------------| / \ | |------------------| | While | |------------------| / \ | |------------------| | Assign | |------------------| | location: int | | val: int | |------------------| / \ | |------------------| | Decrement | |------------------| / \ | |------------------| | IsNonZero | |------------------| / \ | |-----------------------| | | |-------------| |-------------| | IsZero | | Print | |-------------| |-------------| ------------------------------------------ Q: What heuristics does this file a former Arthur Reil's chapter 5? Fundamentally, this violates heuristics 5.1, because specialization on the inheritance is used to model things that are not behavioral subtypes. For example, an assignment statement is not a special kind of while statement. (Heuristic 5.3 "do not use protected data" can be seen by looking at the code.) This actually follows heuristic 5.4. ("Inheritance hierarchies should be deep.") However this comes close to violating Heuristic 5.5, which says that "inheritance hierarchies should be no deeper than an average person can keep in ... short-term memory" This violates heuristic 5.7: "all base classes should be abstract classes". Heuristic 5.8: "factor the commonality of data, behavior, and/or interface as high as possible in the inheritance hierarchy." can be seen to be violated by looking at the code. For example the "toString" method. Heuristic 5.10: "if two or more classes have common data and behavior (i.e., methods), then those classes should each inherit from a common class that captures those data and methods." (look at the code) Heuristic 5.17: "It should be illegal to ... NOP ... a base class method ..." (look at the code) ** looking at and fixing the code *** Store First briefly go over the Store type. ------------------------------------------ ABSTRACTION OF THE COMPUTER'S MEMORY |----------------| | Store | |----------------| |----------------| | contents(): int| | store(int, int)| |----------------| ------------------------------------------ *** plan for correction look at the constructors for the types, to get some idea of what data is not used in various subclasses Q: How could we reorganize these types to follow the heuristics regarding these classes being abstract and to not waste storage space? introduce the abstract class StatementWithLocation with subclasses Assign and all of the classes below it. do that and "pull up" common code using Eclipse make While not a subclass of Block *** more details Q: What heuristics are still violated? Not private data, make location private, introduce method. ** a better version is found in the directory Statements