160 likes | 329 Views
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
E N D
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 LoD Definition OF-LoD: A OF-LoD 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-LoD. • 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 method-calls 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-LoD.
Class Form of LoD Definition CF-LoD: A CF-LoD 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-LoD.
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: aFoo.getPart().getPart(). getBar().getBp().test(); Should not be allowed in a static method. Next follows a generalization of LoD 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.