Saturday, March 1, 2014

What is the difference between declarative and imperative programming
up vote
57
down vote
favorite
36
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 any one have a real world example that they could show me that might bring some perspective to this subject... perhaps in C#.

c# programming-paradigms imperative-programming declarative-programming
share|edit
edited Jan 15 '13 at 19:10

rae1
3,98031130
asked Nov 23 '09 at 17:24

Brad
3,068124674
add comment
7 Answers
activeoldestvotes
up vote
91
down vote
accepted
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:

List collection = new List { 1, 2, 3, 4, 5 };
With imperative programming, we'd step through this, and decide what we want:

List results = new List();
foreach(var num in collection)
{
    if (num % 2 != 0)
          results.Add(num);
}
Here, we're saying:

Create a result collection
Step through each number in the collection
Check the number, if it's odd, add it to the results
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):

var results = collection.Where( num => num % 2 != 0);
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.

share|edit
edited Nov 23 '09 at 22:24

answered Nov 23 '09 at 17:29

Reed Copsey
295k24483825
6  
Your answer as far as "declarative vs imperative" programming is fine. Your code to check for oddness is deeply flawed. This code does NOT check for oddness. Can you find the bug? –  Eric Lippert Nov 23 '09 at 21:42
1  
Yeah, it only checks for positive odd numbers -- I'll fix. –  Reed Copsey Nov 23 '09 at 22:22
1  
+1. However, you first mention LINQ, but what has the examples to do with that? –  Zano Nov 23 '09 at 23:31
5  
collection.Where is using the declarative LINQ extension methods. It's not using the C# language features, but rather the declarative API. I did not want to mix messages here, which is why I avoided the language additions built on top of the declarative methods. –  Reed Copsey Nov 24 '09 at 0:14
 
It seems to me that declarative programming is nothing more than a layer of abstraction. –  Josh Bjelovuk Nov 3 '13 at 23:45
show 2 more comments
up vote
33
down vote
Declarative programming is when you say what you want, and imperative language is when you say how to get what you want.

A simple example in Python:

# Declarative
small_nums = [x for x in range(20) if x < 5]

# Imperative
small_nums = []
for i in range(20):
    if i < 5:
        small_nums.append(i)
The first example is declarative because we do not specify any "implementation details" of building the list.

To tie in a C# example, generally, using LINQ results in a declarative style, because you aren't saying how to obtain what you want; you are only saying what you want. You could say the same about SQL.

One benefit of declarative programming is that it allows the compiler to make decisions that might result in better code than what you might make by hand. Running with the SQL example, if you had a query like

SELECT score FROM games WHERE id < 100;
the SQL "compiler" can "optimize" this query because it knows that id is an indexed field -- or maybe it isn't indexed, in which case it will have to iterate over the entire data set anyway. Or maybe the SQL engine knows that this is the perfect time to utilize all 8 cores for a speedy parallel search. You, as a programmer, aren't concerned with any of those conditions, and you don't have to write your code to handle any special case in that way.

share|edit
edited Nov 23 '09 at 17:37

answered Nov 23 '09 at 17:27

Mark Rushakoff
89.8k14202266
2  
That Python example IS NOT declarative. –  Juanjo Conti Nov 23 '09 at 18:06
 
Prolog is a declarative programming language. –  Juanjo Conti Nov 23 '09 at 18:10
1  
@Juanjo: It IS decalarative. –  missingfaktor Apr 10 '10 at 5:27
 
How is the first statement in here any more declarative than the second? –  zenna May 9 '12 at 21:46
add comment
up vote
8
down vote
I'll add another example that rarely pops up in declarative/imperative programming discussion: the User Interface!

In C#, you can build an UI using various technologies.

On the imperative end, you could use DirectX or OpenGL to very imperatively draw your buttons, checkboxes, etc... line-by-line (or really, triangle by triangle). It is up to you to say how to draw the user interface.

At the declarative end, you have WPF. You basically write some XML (yeah, yeah, "XAML" technically) and the framework does the work for you. You say what the user interface looks like. It is up to the system to figure out how to do it.

Anyway, just another thing to think about. Just because one language is declarative or imperative does not mean that it doesn't have certain features of the other.

Also, one benefit of declarative programming is that purpose is usually more easily understood from reading the code whereas imperative gives you finer control over execution.

The gist of it all:

Declarative -> what you want done

Imperative -> how you want it done

share|edit
answered Nov 23 '09 at 18:41

Erich Mirabal
5,7611327

2 comments:

oakleyses said...

jordan pas cher, chanel handbags, nike outlet, michael kors pas cher, kate spade outlet, replica watches, longchamp pas cher, nike free, jordan shoes, christian louboutin shoes, nike free run, louis vuitton outlet, oakley sunglasses, ray ban sunglasses, polo ralph lauren, ugg boots, christian louboutin uk, air max, louis vuitton outlet, ugg boots, tiffany jewelry, polo ralph lauren outlet online, burberry pas cher, prada outlet, nike air max, gucci handbags, sac longchamp pas cher, ray ban sunglasses, louboutin pas cher, louis vuitton, uggs on sale, tiffany and co, oakley sunglasses, louis vuitton outlet, ray ban sunglasses, longchamp outlet, louis vuitton, longchamp outlet, replica watches, nike roshe, polo outlet, oakley sunglasses, cheap oakley sunglasses, oakley sunglasses wholesale, christian louboutin, christian louboutin outlet, tory burch outlet

oakleyses said...

lululemon canada, nike air max, burberry outlet, oakley pas cher, burberry handbags, coach outlet store online, kate spade, michael kors outlet, michael kors, nike air force, true religion jeans, true religion outlet, michael kors, polo lacoste, nike tn, new balance, abercrombie and fitch uk, michael kors outlet, uggs outlet, michael kors outlet, ralph lauren uk, michael kors outlet online, replica handbags, coach outlet, true religion outlet, coach purses, nike free uk, sac vanessa bruno, mulberry uk, michael kors outlet online, michael kors outlet online, michael kors outlet online, north face, uggs outlet, converse pas cher, hogan outlet, nike air max uk, hollister pas cher, sac hermes, nike roshe run uk, hollister uk, nike air max uk, true religion outlet, timberland pas cher, vans pas cher, ray ban pas cher, guess pas cher, ray ban uk