DEV Community

Cover image for Why there is no NullPointerException in Declarative Programming
Can Mingir for Nucleoid

Posted on • Updated on

Why there is no NullPointerException in Declarative Programming

It is better to start with agreeing on what declarative programming is because no argue that declarative programming is an abstract term with many definitions, and for this particular case, we are going to focus on the control flow.

We can easily roll out one, if the control flow is managed by programmers, it is imperative (procedural) programming. Here is the quick example for that:

account.balance = 1000;

if( account.balance > 500 ) {
  account.status = "OK"
} else {
  account.status = "LOW"
}
Enter fullscreen mode Exit fullscreen mode

It is typical if/else statement, but the problem is when account.balance changed, the same if/else statement has to rerun.

account.balance = 1000;

if( account.balance > 500 ) {
  account.status = "OK"
} else {
  account.status = "LOW"
}

account.balance = 400;

if( account.balance > 500 ) {
  account.status = "OK"
} else {
  account.status = "LOW"
}
Enter fullscreen mode Exit fullscreen mode

If/else statement might be a part of function, but it is going to look like this when expanding of code lines into a call stack.

However, in declarative programming, it is enough to define if/else statement once, and the declarative runtime will correct variables accordingly, means the runtime will provide logical integrity:

> if( account.balance > 500 ) {
    account.status = "OK"
  } else {
    account.status = "LOW"
  }
> account.balance = 1000
> account.status
"OK"
> account.balance = 400
> account.status
"LOW"
Enter fullscreen mode Exit fullscreen mode

The runtime automatically updates account.status when account.balance changed.

Here is the problem starts,

if( account.payment.address.state == "NY" ) {
  ..
}
Enter fullscreen mode Exit fullscreen mode

When there is a property like account.payment.address.state without actually defining each object, the runtime will throw some exception like NullPointerException because the burden is on a programmer to verify each object, and this execution has to return something not programming language specific like InvalidPaymentException, 400 Bad Request etc., but again not NullPointerException.

The declarative runtime cannot just throw technical exception because those don't have any meaning to end users. Instead, it has to follow formal logic, which most declarative programs do, so that it has to wait until all variables are defined, then run the if/else statement.

> if( account.payment.address.state == "NY" ) {
    account.payment.tax = 8.82
  }
> account.payment = new Payment()
> account.payment.address = new Address()
> account.payment.address.state = "NY"
> account.payment.tax
8.82
Enter fullscreen mode Exit fullscreen mode

In conclusion, the declarative runtime is responsible to manage control flow in formal logic with including to provide logical integrity and hiding all technical details. This concept reduces complexity of code lines and helps developers to spends more time on functionalities.

P.S. It is worth to mention, word of NullPointerException in here is being used as phrase rather than specific problem to Java.


Alt Text

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

Latest comments (0)