Variables in Declarative Programming
Variables carry value or reference to object, but what they represent in imperative (procedural) and declarative programming is different. Before diving into details, the one of main differences of two programming styles: declarative programming is strict to provide logical integrity, often in formal logic, but imperative programming doesn’t have to be because its control flow is managed by its programmers.
Let’s start with a simple example:
x = 1;
y = x + 1;
JavaScript engine creates a value in memory and links to variables, but when x
is changed like x = 2
, value of y
stays as is because only memory location of x
has changed. So, logical integrity is broken. However, if we apply the same example in declarative programming:
> x = 1
> y = x + 1
> x = 2
> y
3
So y
has changed along with the x
. When y = x + 1
is defined, declarative runtime is now responsible for providing logical integrity, means correcting y
.
Declarative runtime doesn’t care technical representation (In this case, memory location) rather interested in logical meaning.
The same concept is applicable to object and its properties:
class User {}
user1 = new User()
user1.domain = "domain.com"
user1.username = "first-user"
user1.email = user1.username + "@" + user1.domain
user1.email
is initialized as "first-user@domain.com"
, but changing user1.username
doesn’t affect user1.email
same as above because it is pointed to different memory block.
In declarative programming, it is not the case, in fact, the declaration can be initialized at the beginning, even before defining properties.
> class User {}
> user1 = new User()
> user1.email = user1.username + "@" + user1.domain
> user1.domain = "domain.com"
> user1.username = "first-user"
user1.email
staysundefined
until all properties are defined.
DECLARATIVE RUNTIME = RUNTIME + DATABASE
While the declarative runtime provides logical integrity, it stores each statement as received so it doesn’t require external database.
Learn more at nucleoid.org/#persistency
Nucleoid is open source (Apache 2.0), a runtime environment that allows declarative programming written in ES6 (JavaScript) syntax. Since statements are declarative, the runtime provides logical integrity and persistency as hiding technical details.
Join project at gitlab.com/nucleoid/nucleoid