I have been searching the web looking for a definition for declarative and imperative programming that would shed some light for me. However, the language used at some of the resources that I have found is daunting - for instance at Wikipedia. Does anyone have a real-world example that they could show me that might bring some perspective to this subject (perhaps in C#)?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
From my understanding, both terms have roots in philosophy, there are declarative and imperative kinds of knowledge. Declarative knowledge are assertions of truth, statements of fact like math axioms. It tells you something. Imperative, or procedural knowledge, tells you step by step how to arrive at something. That's what the definition of an algorithm essentially is. If you would, compare a computer programming language with the English language. Declarative sentences state something. A boring example, but here's a declarative way of displaying whether two numbers are equal to each other, in Java:
Imperative sentences in English, on the other hand, give a command or make some sort of request. Imperative programming, then, is just a list of commands (do this, do that). Here's an imperative way of displaying whether two numbers are equal to each other or not while accepting user input, in Java:
Essentially, declarative knowledge skips over certain elements to form a layer of abstraction over those elements. Declarative programming does the same.
A great C# example of declarative vs. imperative programming is LINQ.
With imperative programming, you tell the compiler what you want to happen, step by step.
For example, let's start with this collection, and choose the odd numbers:
With imperative programming, we'd step through this, and decide what we want:
Here, we're saying:
With declarative programming, on the other hand, you write code that describes what you want, but not necessarily how to get it (declare your desired results, but not the step-by-step):
Here, we're saying "Give us everything where it's odd", not "Step through the collection. Check this item, if it's odd, add it to a result collection."
In many cases, code will be a mixture of both designs, too, so it's not always black-and-white.
Stealing from Philip Roberts here:
Two examples:
1. Doubling all numbers in an array
Imperatively:
Declaratively:
2. Summing all items in a list
Imperatively
Declaratively
Note how the imperative examples involve creating a new variable, mutating it, and returning that new value (i.e., how to make something happen), whereas the declarative examples execute on a given input and return the new value based on the initial input (i.e., what we want to happen).
Calvert,C Kulkarni,D (2009). Essential LINQ. Addison Wesley. 48.
Imperative programming is telling the computer explicitly what to do, and how to do it, like specifying order and such
C#:
Declarative is when you tell the computer what to do, but not really how to do it. Datalog / Prolog is the first language that comes to mind in this regard. Basically everything is declarative. You can't really guarantee order.
C# is a much more imperative programming language, but certain C# features are more declarative, like Linq
The same thing could be written imperatively:
(example from wikipedia Linq)
The difference has mostly to do with the overall level of abstraction. With declarative, at some point, you're so far away from the individual steps that the program has a lot of latitude regarding how to get your result.
You could look at every piece of instruction as falling somewhere on a continuum:
Degree of abstraction:
Declarative Real World Example:
Imperative Real World Example: