DEV Community

Cover image for The Immeasurable Value of Declarative Programming
Theodoros Bebekis
Theodoros Bebekis

Posted on

The Immeasurable Value of Declarative Programming

1. Two Ways to Tell the Computer What You Want

There are two ways to build software.

The first is to tell the computer how to do something, step by step.

The second is to tell it what you want to hold true, and to write a common mechanism, which you build once, that applies what you told it.

The first is called imperative. The second is called declarative.

In imperative, you write the procedure yourself, every time.

In declarative, you describe the rule once, and the procedure is executed every time by the mechanism you wrote.

We all started with the first, the imperative way. It is the natural way. You write code that says: take this, put it there, check this, save the other.

And for small programs, it works wonderfully.

The problem begins when the program grows. When you go from 5 tables to 50. From 1 form to 200. From 1 developer to a team.

Then the "how" starts to repeat itself. The same thing, written again and again, in a thousand places, with small differences that nobody remembers why they exist.

That is where declarative shows its value. And its value is not small. It is immeasurable.

2. What Does Declarative Mean in Practice?

Declarative does not mean "without code". It means "code that describes".

Instead of writing:

Take the Product, take its price, put it in the Line, take its name, put it in the Line, take its VAT rate, put it in the Line...

You write:

The Order Line has a lookup to the Product. When a Product is selected, the fields Name, Price, VAT Rate are copied.

The first version is instructions. The second is a definition.

The definition is written once. And it is executed every time, by the framework, automatically, in the same way, without errors, without omissions.

That is the difference. It is not syntactic. It is architectural.

And there is something else, which is the key: a declarative definition is not just code that runs. It is knowledge that the system can use.

The copy mechanism uses it. The UI uses it. Validation uses it. Any code in your application can use it.

Nobody needs to discover inside the code what you meant. The system knows it. Because you told it once, clearly.

3. Example 1: Snapshot Fields

Let us take the most classic example from business applications: the Order.

We have a Customer, an Item, an Order and its Lines.

When the user creates an Order, they select a Customer and an Item. At that moment, we must keep a snapshot of the state as it was then.

Why? Because tomorrow the Customer may change their company name. They may change their billing address. They may change their shipping address. The Item may change its price. It may change its description. It may change its VAT rate.

The Order, however, must not change. The Order is a historical document. It must remember how the world was at the moment it was issued.

So in the Order table we must keep:

  • The Customer's company name as it was
  • The billing address as it was
  • The shipping address as it was
  • The phone, the email, the VAT number as they were

And in the OrderLine table we must keep:

  • The Item's name as it was
  • Its price as it was
  • Its VAT rate as it was
  • Its unit of measure as it was

These are the snapshot fields.

And for anyone who has built an ERP, CRM, accounting system, warehouse system, it is an obvious need that they exist and are kept up to date.

The question is: where is this defined?

In the Imperative Approach

It is defined nowhere. Or rather, it is defined everywhere.

It is defined in the Service that creates the Order. It is defined in the Service that processes it. It is defined in the API that accepts it. It is defined in the form that shows it. It is defined in the import that brings it in from elsewhere.

At every point, someone writes by hand:

Take the name from the Item, put it in the Line. Take the price from the Item, put it in the Line.

And if tomorrow we must also keep something else, for example the Item's barcode as it was then, we must find all these places and change them one by one.

And we will certainly forget one.

In the Declarative Approach

It is defined once, in the definition of the lookup.

The definition says, in plain language:

The field ProductId of the Line is a lookup to the Product table.
When it changes, the following fields are copied:
Product.Name -> OrderLine.ProductName
Product.Price -> OrderLine.Price
Product.VatRate -> OrderLine.VatRate
Product.Barcode -> OrderLine.Barcode

And in code, the same definition may look something like this. The example is indicative, it does not belong to a specific library:

{
  "field": "ProductId",
  "lookup": {
    "source": "Product",
    "key": "Id",
    "copyOnSelection": {
      "Name": "ProductName",
      "UnitId": "UnitId",
      "VatRate": "VatRate",
      "Barcode": "Barcode"
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

That is all.

From then on, whoever creates an Order, from wherever they create it, from a form, from an API, from an import, from an automatic process, the snapshot will happen automatically, in the same way.

And if tomorrow we must add the barcode, we add it in one place: in the definition. Not in a thousand.

That is the difference between "we know it" and "we have defined it".

Copying fields is a mechanism. Which data we keep and when we change it is a business decision. A declarative definition lets us express our knowledge in a concentrated way.

4. Example 2: Adding a Field

Let us take the second example, which is perhaps even more revealing.

Suppose we have a system that works. It has 50 tables, 200 forms, 100 reports.

And the Customer comes and says: "I want to add the field 'Profession' to the Customer".

A simple request. One field.

What needs to happen in the imperative approach?

  1. Add a column to the database.
  2. Add a property to the entity / model.
  3. Add a field to the DTO.
  4. Add a mapping to the Mapper.
  5. Add a field to the entry form.
  6. Add a field to the search form.
  7. Add a field to the grid.
  8. Add a field to the filter.
  9. Add a field to validation.
  10. Add a field to the API.
  11. Add a field to the report.
  12. Add a field to the Excel export.

Twelve places for one field. And if you forget one, the system works, but something is missing. And nobody notices immediately.

And if the field is a lookup, for example "Profession" selected from a list, multiply the above by two, because you must also build the lookup, and the snapshot, and the lookup's form.

Now imagine it is not one field, but an entire table. Or ten tables. Or an entire module.

In the Declarative Approach

You add the field to the definition of the table.

Table Customer, field Profession, type string, length 100, required no, visible yes.

And that is it.

The form shows it automatically, because it is built from the definition.
The grid shows it automatically, because it is built from the definition.
The filter has it automatically.
Validation does it automatically.
The API exposes it automatically.
The export has it automatically.

You did not write the same thing twelve times. You wrote it once, and the system understood it.

The application knows what the field is. Let it use that knowledge.

That does not mean that every field is necessarily shown everywhere. It may be hidden in the grid, available only to certain user roles, excluded from the public API. But those too are pieces of information expressed declaratively, in the same definition. They are not code scattered across 12 files.

5. Example 3: The Form

Let us go one step further.

In business applications, 80% of forms are the same: a header with fields, a grid with lines, some lookups, some Insert / Edit / Delete / Save buttons.

In the imperative approach, you build the same form 200 times, with small differences. And every time you write:

Make a TextBox for the Name. Make a TextBox for the Address. Make a Lookup for the Customer. Make a Grid for the Lines.

In the declarative approach, you do not build a form. You define that the Order table has a form, and the form is built from the definition of the table.

If you want something custom, you make it custom. But the 80% that is standard, you do not rewrite it.

This has two consequences that are not immediately visible.

The first is speed. You can build an entire application in days, not months.

The second, which is more important, is consistency. All forms work the same way. The user learns once how the Lookup works, and knows how it works everywhere. The developer learns once how the framework works, and knows how it works everywhere.

In the imperative approach, every form is a small, different program. With its own bugs. With its own quirks.

And yes, in the declarative approach, a bug in the common mechanism will affect many forms. But I prefer a thousand times to fix a bug in one place, than to chase two hundred variations of the same bug in two hundred forms.

6. Why Doesn't Everyone Do It This Way?

A fair question.

The answer is that declarative is harder at the beginning. Much harder.

To say "I define the table and the form builds itself", you must first build the mechanism that builds forms from definitions. You must build the mechanism that does snapshots. You must build the mechanism that does lookups, locators, filters, validations, code providers.

You end up building a framework. And building a framework is much harder than building an application.

That is why most developers, and most teams, choose the imperative road. Because it is direct. You write code, you see results. You do not need to design anything in advance.

And for a small application, they are right. It is faster.

For a large application, that will live for 10 years, that will keep changing, that will be maintained by people who did not write it, the imperative road slowly becomes unmaintainable. Not because the developers are bad. But because the knowledge is scattered in a thousand places, and nobody knows where it all is.

Declarative is an investment. You pay at the beginning, you gain continuously.

And no, not every team needs to build its own framework. And no, not every small application justifies such an investment.

But at some point we must stop measuring only how fast we wrote the first form.

7. What Declarative Is Not

Declarative does not mean "without code". It does not mean "low-code". It does not mean "drag and drop".

Declarative means that your code describes what holds true, not how it will happen.

You can write declarative code in C#, in Java, in TypeScript, in any language. The language does not matter. The mindset does.

And declarative does not mean that you never write imperative code. Of course you do. When you have a complex calculation, a complex rule, a complex flow, you will write imperative code. And you will do well.

The difference is: where do you put the imperative?

You put it where there is real business logic. Not where there is repetition.

The snapshot is not business logic. It is a mechanism. You should not write it every time.

The form is not business logic. It is presentation. You should not rebuild it every time.

The business logic is: "If the Customer is wholesale, the discount is 10%, otherwise 0%". You write that once, in code, and it is right that it is imperative.

And let us make one more thing clear. Abstraction does not replace knowledge.

If a tool generates SQL, that does not mean you do not need to understand databases and SQL. If a framework generates forms, that does not mean you do not need to understand UI. If it copies snapshot fields, someone must have correctly decided what those snapshots mean.

Good infrastructure executes the knowledge we gave it. It does not fix our ignorance.

8. The Immeasurable Value

The value of declarative does not show in the first week. It shows in the first year. And in the fifth. And in the tenth.

It shows

  • when you have to add a field and you add it in one place, not in twelve.
  • when you have to change a snapshot rule and you change it in one place, not in fifty Services.
  • when a new developer joins the team and does not need to read 200 forms to understand how the system works. They read the definitions.
  • when the Customer asks for a change and you say "done", instead of "it will take two weeks because we have to change 30 places".

It is not about language. It is not about framework. It is not about Java or .NET, Entity Framework or Hibernate.

It is a matter of decision: Will we define once what holds true, or will we rewrite it every time?

Whoever chooses declarative, pays at the beginning. And gains forever.

Whoever chooses imperative, gains at the beginning. And pays forever.

Top comments (0)