data-driven-docs

Selft training repo


Project maintained by ggranados Hosted on GitHub Pages — Theme by mattgraham

Declarative Programming


Table of Contents

What’s Declarative Programming

Declarative programming is a programming paradigm that focuses on describing what should be accomplished rather than how it should be accomplished. In declarative programming, you define the desired result or outcome, and the underlying implementation details are abstracted away.

Characteristics

Languages

Declarative programming can lead to code that is easier to reason about, maintain, and extend, especially in complex and data-intensive applications. However, it may not be suitable for all types of problems and may require a programming environment or language that supports the declarative paradigm.

Example

import java.util.stream.IntStream;

public class DeclarativeExample {
    public static void main(String[] args) {
        int sum = IntStream.rangeClosed(1, 10).sum();
        System.out.println("Sum: " + sum);
    }
}

In this example, we’re using Java’s Stream API to perform the summation declaratively. The IntStream.rangeClosed(1, 10) generates a stream of integers from 1 to 10 (inclusive), and the sum() operation calculates the sum of those numbers. This code expresses the desired outcome (calculating the sum) without specifying the step-by-step process of how to achieve it, which is characteristic of declarative programming.

Back to top

Declarative programming vs. imperative programming

Declarative programming is a high-level programming concept, which is the opposite of imperative programming

Declarative programming relies on underlying components of a given language to carry out the necessary steps to reach the stated outcome. In declarative programming, typical programming constructs such as loops and if/then conditions do not exist, because they are instructional


Ref.


Get Started