我一直在网上搜索声明式编程和命令式编程的定义,希望能给我一些启发。然而,我发现在一些资源中使用的语言令人生畏——例如在维基百科。 有没有人可以给我举一个现实世界的例子,让我对这个主题有一些了解(也许是c#)?


当前回答

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:

public static void main(String[] args)
{
    System.out.print("4 = 4.");
}

另一方面,英语中的祈使句给出命令或提出某种请求。因此,命令式编程只是一个命令列表(做这个,做那个)。下面是在Java中,在接受用户输入时显示两个数字是否相等的强制方法:

private static Scanner input;    

public static void main(String[] args) 
{
    input = new Scanner(System.in);
    System.out.println();
    System.out.print("Enter an integer value for x: ");
    int x = input.nextInt();
    System.out.print("Enter an integer value for y: ");        
    int y = input.nextInt();

    System.out.println();
    System.out.printf("%d == %d? %s\n", x, y, x == y);
}

从本质上讲,声明性知识跳过某些元素,在这些元素之上形成一个抽象层。声明式编程也是如此。

其他回答

我喜欢剑桥一门课程的解释和他们的例子:

声明性-指定要做什么,而不是如何做 例如:HTML描述的是什么应该出现在网页上,而不是它应该如何在屏幕上绘制 命令式-同时指定“什么”和“如何” int x;-什么(陈述性的) x = x + 1;——如何

在计算机科学中,声明式编程是一种编程范式,它在不描述控制流的情况下表达计算逻辑。

从http://en.wikipedia.org/wiki/Declarative_programming

简而言之,声明式语言更简单,因为它缺乏控制流(循环、if语句等)的复杂性。

一个很好的比较是ASP。网络“后台代码”模型。你们有陈述句。'ASPX '文件,然后是'ASPX. cs '代码文件。我经常发现,如果我可以在脚本的声明性部分完成我所需要的一切,那么更多的人可以理解所做的事情。

声明式编程和命令式编程的一个很好的c#例子是LINQ。

使用命令式编程,你一步一步地告诉编译器你想要发生什么。

例如,让我们从这个集合开始,并选择奇数:

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

在命令式编程中,我们将逐步执行这些步骤,并决定我们想要什么:

List<int> results = new List<int>();
foreach(var num in collection)
{
    if (num % 2 != 0)
          results.Add(num);
}

这里,我们说:

创建一个结果集合 逐步检查集合中的每个数字 检查数字,如果是奇数,就把它加到结果中

另一方面,在声明式编程中,你写的代码描述了你想要的东西,但不一定是如何得到它(声明你想要的结果,但不是一步一步的):

var results = collection.Where( num => num % 2 != 0);

这里,我们说的是“把所有奇怪的东西都给我们”,而不是“遍历集合”。选中此项,如果为奇数,则将其添加到结果集合中。”

在许多情况下,代码也将是两种设计的混合,因此并不总是黑白分明的。

再举一个手机应用开发的例子。在iOS和Android中,我们有界面构建器,在那里我们可以定义应用程序的UI。

使用这些生成器绘制的UI本质上是声明性的,我们可以在其中拖放组件。实际的绘图发生在框架和系统下面,由框架和系统执行。

但我们也可以在代码中画出整个组件,这在本质上是必要的。

此外,一些新语言,如Angular JS,专注于以声明方式设计ui,我们可能会看到许多其他语言提供同样的支持。就像Java在Java swing或Java FX中没有任何好的声明性方法来绘制本地桌面应用程序,但在不久的将来,他们可能会这样做。

Imperative programming requires developers to define step by step how code should be executed. To give directions in an imperative fashion, you say, “Go to 1st Street, turn left onto Main, drive two blocks, turn right onto Maple, and stop at the third house on the left.” The declarative version might sound something like this: “Drive to Sue’s house.” One says how to do something; the other says what needs to be done. The declarative style has two advantages over the imperative style: It does not force the traveler to memorize a long set of instructions. It allows the traveler to optimize the route when possible.

卡尔弗特,C库尔卡尼,D(2009)。LINQ至关重要。艾迪森·韦斯利48岁