Proceedings of the 2007 international conference on Dynamic languages in conjunction with the 15th International Smalltalk Joint Conference 2007 - ICDL '07
Context-dependent behavior is becoming increasingly important for a wide range of application domains, from pervasive computing to common business applications. Unfortunately, mainstream programming languages do not provide mechanisms that enable software entities to adapt their behavior dynamically to the current execution context. This leads developers to adopt convoluted designs to achieve the necessary runtime flexibility. We propose a new programming technique called Context-oriented
... mming (COP) which addresses this problem. COP treats context explicitly, and provides mechanisms to dynamically adapt behavior in reaction to changes in context, even after system deployment at runtime. In this paper, we lay the foundations of COP, show how dynamic layer activation enables multi-dimensional dispatch, illustrate the application of COP by examples in several language extensions, and demonstrate that COP is largely independent of other commitments to programming style. uitous computing, software evolution, runtime adaptation, and execution context dependencies as a part of COP's application domain. At present, the lack of programming mechanisms to support the development of context-aware applications forces the design of these applications to be more complex and fragile than need be the case. This paper presents Context-oriented Programming (COP) as a new programming technique to enable context-dependent computation. We claim that Contextoriented Programming brings a similar degree of dynamicity to the notion of behavioral variations that object-oriented programming brought to ad-hoc polymorphism. In support of this claim, we argue that the dynamic representation of layers and their scoped activation and deactivation in arbitrary places of the code are the essential ingredients for COP. Notably, in this paper we discuss: • The motivation of Context-oriented Programming (COP) as a programming technique that directly supports context-dependent behavioral variations. • Layers as named first-class entities that can be referred to explicitly at runtime, and whose composition can be dynamically controlled on-demand. • An illustration of the utility of COP by several examples implemented in different COP extensions to Java, Squeak/Smalltalk and Common Lisp. Specifically, we show as the contributions of this paper that: • COP is independent from how source code is organized into textual modules. • It can be beneficial to activate/deactivate layers from anywhere in the code. • The notion of layered slots in ContextL allows a higher-order reflective programming style to integrate crosscutting concerns. PROBLEMS A context-dependent application varies its behavior according to conditions arising during execution. In order to pin down the term context, we offer the following abstract picture of context-dependence (see Figures 1a, b, and c ). An actor (as in UML) is an entity that interacts with the system, calling functions, sending messages, or employing other means of interaction in order to request the desired system behavior. A system is a computational entity that provides some desired behavior whenever requested. A system may be large or small, finely-grained or coarsely-grained. The constituents of a system may, for example, be procedures, methods, objects, components or subsystems. An environment represents anything external to the relationship and interaction between actor and system.