Skip to main content

What is declarative programming?



        Here code is written in such a way that it describes what you want to do, and not how you want to do it. It is left up to the compiler to figure out the how.
        Often defined as any style of programming that is not imperative.
        The program is built from one or more procedures
        It provides a programmer a means to define precisely each step in the performance of a task.




Declarative programming contrasts with imperative and procedural programming. Declarative programming is a non-imperative style of programming in which programs describe their desired results without explicitly listing commands or steps that must be performed. Functional and logical programming languages are characterized by a declarative programming style. In logical programming languages, programs consist of logical statements, and the program executes by searching for proofs of the statements.

Examples
        Example :-   SQL , Prolog , Alpha , Brooks , Curl , JavaFX Script


Subparadigms of Declarative programming.......

        Constraint programming    :- 
Constraint programming states relations between variables in the form of constraints that specify the properties of the target solution. The set of constraints is solved by giving a value to each variable so that the solution is consistent with the maximum number of constraints. Constraint programming often complements other paradigms: functional, logical, or even imperative programming.
        Domain-specific languages:-
Well-known examples of declarative domain-specific languages (DSLs) include the yacc parser generator input language, QML, the Make build specification language, Puppet's configuration management language, regular expressions, and a subset of SQL (SELECT queries, for example). DSLs have the advantage of being useful while not necessarily needing to be Turing-complete, which makes it easier for a language to be purely declarative.

Many markup languages such as HTML, MXML, XAML, XSLT or other user-interface markup languages are often declarative. HTML, for example, only describes what should appear on a webpage - it specifies neither control flow for rendering a page nor the page's possible interactions with a user.
        Logic programming :-
Logic programming languages such as Prolog state and query relations. The specifics of how these queries are answered is up to the implementation and its theorem prover, but typically take the form of some sort of unification. Like functional programming, many logic programming languages permit side effects, and as a result are not strictly declarative.
        Hybrid languages:-
Makefiles, for example, specify dependencies in a declarative fashion, but include an imperative list of actions to take as well. Similarly, yacc specifies a context free grammar declaratively, but includes code snippets from a host language, which is usually imperative (such as C).



Example :

List<int> collection = new List<int> {1,2,3,4,5 };
Imperative programming :-

List<int> results = new List<int>();
foreach(var num in collection)
{
    if (num % 2 != 0)
          results.Add(num);
}
Declarative programming :-      var  results = collection.Where( num=>num%2 != 0);
Does not step through the collection

Comments

Popular posts from this blog

Introduction

Groovy Declarative Style Code [1,2,3,4,5].findAll { it % 2 == 0 }.each() {value->println "${value*2}"}; There are lot of way to address this stuff in Java 8. So let’s have a look to do the same stuff in Java 8 Example:- import java.util.Arrays ; import java.util.List ;   class DeclarativeStyle {     public static void main(String []args){         List numbers = Arrays.asList(1,2,3,4, 5, 6);           numbers.stream()                          //A Fancy Collection            .filter(DeclarativeStyle::isEven)      //Filter the data            .map(DeclarativeStyle::doubleInteger) //Map new values            .forEach( System.out::println );         //Iterate & perform the operation     }       public static String doubleInteger(Integer number){         return String.valueOf(number * 2);     }       public static Boolean isEven(Integer number){         return number%2==0;     } } The first three method calls are just lazy ca