Lably
Skip to content

Reactive programming

Programming paradigm based on asynchronous data streams

  • 15 min read
  • 6 chapters

Related on Lably

Through Computing, Stream (computing) and Array (data type)

For example, in imperative programming, a := b + c would mean that a is being assigned the result of b + c at the time the expression is evaluated, and later, the values of b and c can be changed with no effect on the value of a. In contrast, in reactive programming, the value of a is automatically updated whenever the values of b or c change, without the program having to explicitly re-state the statement a := b + c to re-assign the value of a.

<span id="mwKg"></span><span class="kt" id="mwKw">int</span><span class="w" id="mwLA"> </span><span class="n" id="mwLQ">b</span><span class="w" id="mwLg"> </span><span class="o" id="mwLw">=</span><span class="w" id="mwMA"> </span><span class="mi" id="mwMQ">1</span><span class="p" id="mwMg">;</span> <span class="kt" id="mwMw">int</span><span class="w" id="mwNA"> </span><span class="n" id="mwNQ">c</span><span class="w" id="mwNg"> </span><span class="o" id="mwNw">=</span><span class="w" id="mwOA"> </span><span class="mi" id="mwOQ">2</span><span class="p" id="mwOg">;</span> <span class="kt" id="mwOw">int</span><span class="w" id="mwPA"> </span><span class="n" id="mwPQ">a</span><span class="w" id="mwPg"> </span><span class="o" id="mwPw">=</span><span class="w" id="mwQA"> </span><span class="n" id="mwQQ">b</span><span class="w" id="mwQg"> </span><span class="o" id="mwQw">+</span><span class="w" id="mwRA"> </span><span class="n" id="mwRQ">c</span><span class="p" id="mwRg">;</span> <span class="n" id="mwRw">b</span><span class="w" id="mwSA"> </span><span class="o" id="mwSQ">=</span><span class="w" id="mwSg"> </span><span class="mi" id="mwSw">10</span><span class="p" id="mwTA">;</span> <span class="n" id="mwTQ">System</span><span class="p" id="mwTg">.</span><span class="na" id="mwTw">out</span><span class="p" id="mwUA">.</span><span class="na" id="mwUQ">println</span><span class="p" id="mwUg">(</span><span class="n" id="mwUw">a</span><span class="p" id="mwVA">);</span><span class="w" id="mwVQ"> </span><span class="c1" id="mwVg">// 3 (not 12 because "=" is not a reactive assignment operator)</span> <span class="c1" id="mwVw">// now imagine a special operator "$=" exists that changes the value of a variable (executes code on the right side of the operator and assigns result to left side variable) whenever explicitly initialized, and when referenced variables (on the right side of the operator) are changed</span> <span class="kt" id="mwWA">int</span><span class="w" id="mwWQ"> </span><span class="n" id="mwWg">b</span><span class="w" id="mwWw"> </span><span class="o" id="mwXA">=</span><span class="w" id="mwXQ"> </span><span class="mi" id="mwXg">1</span><span class="p" id="mwXw">;</span> <span class="kt" id="mwYA">int</span><span class="w" id="mwYQ"> </span><span class="n" id="mwYg">c</span><span class="w" id="mwYw"> </span><span class="o" id="mwZA">=</span><span class="w" id="mwZQ"> </span><span class="mi" id="mwZg">2</span><span class="p" id="mwZw">;</span> <span class="kt" id="mwaA">int</span><span class="w" id="mwaQ"> </span><span class="n" id="mwag">a</span><span class="w" id="mwaw"> </span><span class="n" id="mwbA">$</span><span class="o" id="mwbQ">=</span><span class="w" id="mwbg"> </span><span class="n" id="mwbw">b</span><span class="w" id="mwcA"> </span><span class="o" id="mwcQ">+</span><span class="w" id="mwcg"> </span><span class="n" id="mwcw">c</span><span class="p" id="mwdA">;</span> <span class="n" id="mwdQ">b</span><span class="w" id="mwdg"> </span><span class="o" id="mwdw">=</span><span class="w" id="mweA"> </span><span class="mi" id="mweQ">10</span><span class="p" id="mweg">;</span> <span class="n" id="mwew">System</span><span class="p" id="mwfA">.</span><span class="na" id="mwfQ">out</span><span class="p" id="mwfg">.</span><span class="na" id="mwfw">println</span><span class="p" id="mwgA">(</span><span class="n" id="mwgQ">a</span><span class="p" id="mwgg">);</span><span class="w" id="mwgw"> </span><span class="c1" id="mwhA">// 12</span>

Another example is a hardware description language such as Verilog, where reactive programming enables changes to be modeled as they propagate through circuits.

Reactive programming has been proposed as a way to simplify creating interactive user interfaces and near-real-time computing animation.

For example, in a model–view–controller (MVC) architecture, reactive programming can facilitate changes in an underlying model being reflected automatically in an associated view.

Approaches to creating reactive programming languages

Several popular approaches are employed in creating reactive programming languages. One approach is the specification of dedicated languages that are specific to various domain constraints. Such constraints usually are characterized by real-time, embedded computing or hardware description. Another approach involves the specification of general-purpose languages that include support for reactivity. Other approaches are articulated in the definition, and use of programming libraries, or embedded domain-specific languages, that enable reactivity alongside or atop a programming language. Specification and use of these different approaches results in language capability trade-offs. In general, the more restricted a language is, the more its associated compilers and analysis programming tools are able to inform developers (e.g., in performing analysis for whether programs are able to execute in actual real-time). Functional trade-offs in specificity may result in deterioration of the general applicability of a language.

Sign in to keep reading

5 more chapters are free with a Lably account.

More in Computing and communication