Popis: |
Imperative programming has great merits. As the ubiquitous style, it is familiar, and its linear and step by step nature is favored by the human mind. Experienced programmers, however, are aware of its major flaw: it is easy for meanings to get lost in piles of code, making software hard to understand, extend, and debug. Constraint-based programming as an alternative has been observed to suffer much less from these flaws, where the "what" (the intention) is expressed rather than the "how" (the algorithm) in performing a computation. It is the job of the system to automatically achieve the intention through constraint solving. Sadly, poor performance and expressiveness has prevented this style from seeing widespread adoption. We propose a general programming model as a kind of a sweet spot between imperative and constraint-based programming. Our aim is to leverage many benefits of constraint-based programming such as understandability, behavioral modularity, extensibility, etc., in a practical way and without suffering the breakdown of the approach as with the traditional constraint-based paradigm. This model enforces a certain organization where at the top-level a program is simply composed of a set of constraints. However, the constraints aren't necessarily solved by an external entity, and the programmer uses imperative code to specify (1) how each constraint should be solved in isolation, and (2) how to combine individual solutions. We have implemented a tool called Sketchpad14 that incorporates this model in JavaScript, and built a number of realistic applications in it. In this paper we demonstrate the merits of our approach by comparing it with traditional imperative as well as constraint-based approaches. |