Declarative dan Imperative Programming

Posted on Updated on

Teman-teman, maaf karena pakenya inggris soalnya dapet referensi nya bahasa inggris. Takutnya kalau di translete malah kurang bener maksud yang seharusnya.

Deklarative Programming (What)

declarative programming is a programming paradigm that expresses the logic of a computation without describing its control flow.

Declarative programming is a non-imperative style of programming in which programs describe the desired results of the program, without explicitly listing command or steps that need to be carried out to achieve the results.

Functional and logical programming languages are characterized by a declarative programming style.

Sebagai contoh :

–          A program that describes what computation should be performed and not how to compute it

–          Any programming language that lacks side effects (or more specifically, is referentially transparent)

–          A language with a clear correspondence to mathematical logic.

Common declarative languages include those of regular expressions, logic programming, and functional programming.

Declarative programming is an umbrella term that includes a number of better-known programming paradigms.

1. Functional programming

functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state.

Functional programming has its roots in the lambda calculus, a formal system developed in the 1930s to investigate function definition, function application, and recursion. Many functional programming languages can be viewed as elaborations on the lambda calculus.

Functional programming, and in particular purely functional programming, attempts to minimize or eliminate side effects, and is therefore considered declarative. Most functional languages, however, do permit side effects in practice.

Functional programming also finds use in industry through domain-specific programming languages like R (statistics), Mathematica (symbolic math), J and K (financial analysis)], and XSLT (XML). Widespread declarative domain specific languages like SQL and Lex/Yacc, use some elements of functional programming, especially in eschewing mutable values. Spreadsheets can also be viewed as functional programming languages.

2. 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.

Logic programming is, in its broadest sense, the use of mathematical logic for computer programming. Logic is used as a purely declarative representation language, and a theorem-prover or model-generator is used as the problem-solver. The problem-solving task is split between the programmer, who is responsible only for ensuring the truth of programs expressed in logical form, and the theorem-prover or model-generator, which is responsible for solving problems efficiently.

However, logic programming, in the narrower sense in which it is more commonly understood, is the use of logic as both a declarative and procedural representation language. It is based upon the fact that a backwards reasoning theorem-prover applied to declarative sentences in the form of implications:

If B1 and … and Bn then H

treats the implications as goal-reduction procedures:

to show/solve H, show/solve B1 and … and Bn.

For example, it treats the implication:

If you press the alarm signal button,

then you alert the driver of the train of a possible emergency

as the procedure:

To alert the driver of the train of a possible emergency,

press the alarm signal button.

3. Constraint programming

In Constraint programming relations between variables are stated in the form of constraints, specifying the properties of a solution to be found. The set of constraints is then solved by giving a value to each variable so that the solution is consistent with the maximum number of constraints.

Constraint programming is often used as a complement to other paradigms: functional, logical or even imperative programming.

4. Domain-specific languages

Some well-known examples of declarative domain specific languages (DSLs) include regular expressions, CSS, 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, SVG or other user interface markup languages are often declarative. HTML, for example, only describes what should appear on a webpage and doesn’t specify the possible interactions with it.

Some software systems now combine traditional user interface markup languages such as HTML with declarative markup that defines what (but not how) the back-end server systems should do to support the declared interface. Such systems, typically using a domain specific XML namespace, include abstractions of SQL database syntax or parameterised calls to web services using REST and SOAP.

5. 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).

Imperative programming (How)

imperative programming is a programming paradigm that describes computation in terms of statements that change a program state. In much the same way that imperative mood in natural languages expresses commands to take action, imperative programs define sequences of commands for the computer to perform.

The term is used in opposition to declarative programming, which expresses what needs to be done, without prescribing how to do it in terms of sequences of actions to be taken. Functional and logical programming are examples of a more declarative approach.

Imperative, procedural, and declarative programming

Procedural programming is imperative programming in which the program is built from one or more procedures (also known as subroutines or functions). The terms are often used as synonyms, but the use of procedures has a dramatic effect on how imperative programs appear and how they are constructed. Heavily procedural programming, in which state changes are localized to procedures or restricted to explicit arguments and returns from procedures, is known as structured programming. From the 1960s onwards, structured programming and modular programming in general, have been promoted as techniques to improve the maintainability and overall quality of imperative programs. Object-oriented programming extends this approach.

Procedural programming could be considered as a step towards declarative programming. A programmer can often tell, simply by looking at the names, arguments and return types of procedures (and related comments), what a particular procedure is supposed to do – without necessarily looking at the detail of how the procedure achieves its result. At the same time, a complete program is still imperative since it ‘fixes’ the statements to be executed and their order of execution to a large extent.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s