Chapter 5 Bend or Break Bend or Break
Chapter 5 Bend or Break
Bend or Break • Life does not stand still • We need to make every effort to write code that is as loose – as flexible – as possible • Reversability: how to make reversible decisions
Bend or Break • Decoupling and the Law of Demeter – Keep separate concepts separate • Write less: good way to stay flexible • Metaprogramming: how to move details out of the code completely • Temporal Coupling: do not depend on absolute time
Bend or Break • Just a view: decouple models from views. • Blackboards: provide a meeting place where modules exchange data.
Decoupling and the Law of Demeter • Writing shy code (see also Orthogonality and Design by Contract)
Object Form of Lo. D Definition OF-Lo. D: A OF-Lo. D join point is a method-call join point, in which the target object is either: • An instance variable of the "this" object. • Constructed by the method. • An argument of the method. • Returned by a message send to “this”.
E-X-Violation • X: a property on join points; e. g. OF-Lo. D. • Definition E-X-Violation: A method-call in P is a X-violation if there exists an execution of P where a join point corresponding to the method-call does not satisfy property X.
• A program P satisfies X if for all methodcalls in P and all executions of P, the join points corresponding to the method-calls satisfy property X. • A program P satisfies X for execution E if for all method-calls in P, the corresponding join points of E satisfy property X.
Object Form Violation OF-Lod-Violation: X = OF-Lo. D.
Class Form of Lo. D Definition CF-Lo. D: A CF-Lo. D joinpoint is a method -call join-point, in which the target object’s class is either: • The static type of an instance variable. • The static type of a newly constructed object. • The type of an argument of the method. • The return type of a method of the class.
Class Form Violation CF-Lod-Violation: X = CF-Lo. D.
Forms of Lo. D forms class (c) object (o) static(s), Prog. Anal. dynamic(d), Aspect. J useful (c/s), simple definition not useful (c/d), c/s instead useful (o/s), approximates o/d most useful (o/d), simple definition
Notes • o/s: if an illegal message sending is possible assuming each decision taken both ways and each loop is executed 0 or more times. • c/d: not interesting, because c/s. If dynamically: do o/d
Note The following: a. Foo. get. Part(). get. Bar(). get. Bp(). test(); Should not be allowed in a static method. Next follows a generalization of Lo. D to static methods (slide after next). Do you agree?
Object form for regular methods Within a method, messages can only be sent to the following objects: • 1. A parameter of the method, including the enclosing object (this or self); • 1. 1. For pragmatic reasons: a global object; • 2. An immediate part object (computed or stored): • 2. 1 An object that a method called on the enclosing object returns, including attributes of the enclosing object; • 2. 2 An element of a collection which is an attribute of the enclosing object; • 3. An object created within the method.
Object form for static methods Within a static method, messages can only be sent to the following objects: • 1. A parameter of the method, including the current class; • 1. 1. For pragmatic reasons: a global object; • 2. An immediate static part object (computed or stored): • 2. 1 An object that a static method called on the current class returns, including static attributes of the current class; • 2. 2 An element of a collection which is a static attribute of the current class; • 3. An object created within the method.
- Slides: 16