我一直在网上搜索声明式编程和命令式编程的定义,希望能给我一些启发。然而,我发现在一些资源中使用的语言令人生畏——例如在维基百科。 有没有人可以给我举一个现实世界的例子,让我对这个主题有一些了解(也许是c#)?
当前回答
命令式编程 一种需要编程规程的编程语言,如C/ c++, Java, COBOL, FORTRAN, Perl和JavaScript。用这种语言编写程序的程序员必须基于数据处理和编程知识,开发出适当的操作顺序来解决问题。
声明性编程 一种不需要编写传统编程逻辑的计算机语言; 用户专注于定义输入和输出,而不是过程式编程语言(如c++或Java)中所需的程序步骤。
声明性编程的例子有CSS、HTML、XML、XSLT、RegX。
其他回答
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);
}
从本质上讲,声明性知识跳过某些元素,在这些元素之上形成一个抽象层。声明式编程也是如此。
命令式编程是显式地告诉计算机做什么,以及如何做,比如指定顺序等
C#:
for (int i = 0; i < 10; i++)
{
System.Console.WriteLine("Hello World!");
}
说明性是指你告诉计算机做什么,而不是如何做。Datalog / Prolog是在这方面首先想到的语言。基本上所有东西都是声明性的。你不能保证秩序。
c#是一种更命令式的编程语言,但某些c#特性更具有声明性,如Linq
dynamic foo = from c in someCollection
let x = someValue * 2
where c.SomeProperty < x
select new {c.SomeProperty, c.OtherProperty};
同样的东西也可以写成命令式:
dynamic foo = SomeCollection.Where
(
c => c.SomeProperty < (SomeValue * 2)
)
.Select
(
c => new {c.SomeProperty, c.OtherProperty}
)
(例子来自维基百科Linq)
这种差异主要与抽象的总体水平有关。对于声明式,在某些情况下,您离单个步骤太远了,以至于程序在如何获得结果方面有很大的自由度。
你可以把每一条指令都看作是连续体上的某个地方:
抽象程度:
Declarative <<=====|==================>> Imperative
声明性真实世界的例子:
图书管理员,请帮我借一本《白鲸记》。 (图书管理员可酌情选择最佳的申请方式)
现实世界的例子:
进入图书馆 查找图书组织系统(卡片目录-老派) 研究如何使用卡片目录(你也忘了,对吧) 弄清楚货架是如何标记和组织的。 弄清楚书架上的书是如何排列的。 从卡片目录和组织系统中交叉引用图书位置以查找所述书籍。 带书到退房系统。 借书。
声明性程序只是它的一些或多或少的“通用”命令式实现/虚拟机的数据。
pluses: specifying just a data, in some hardcoded (and checked) format, is simpler and less error-prone than specifying variant of some imperative algorithm directly. some complex specifications just cant be written directly, only in some DSL form. best and freq used in DSLs data structures is sets and tables. because you not have dependencies between elements/rows. and when you havent dependencies you have freedom to modify and ease of support. (compare for example modules with classes - with modules you happy and with classes you have fragile base class problem) all goods of declarativeness and DSL follows immediately from benefits of that data structures (tables and sets). another plus - you can change implementation of declarative language vm, if DSL is more-or-less abstract (well designed). make parallel implementation, for example. or port it to other os etc. all good specifed modular isolating interfaces or protocols gives you such freedom and easyness of support.
缺点: 你猜对了。一般的(通过DSL参数化的)命令式算法/虚拟机实现可能比特定的算法慢和/或占用内存。在某些情况下。 如果这种情况很罕见,那就忘了它吧,让它慢慢来。如果这种情况经常发生,你总是可以扩展你的DSL/vm。某个能减缓其他案件的地方,当然…
P.S.框架介于DSL和命令式之间。作为所有的折中方案……他们结合了缺点,而不是优点。它们不是那么安全,也不是那么快:)看看万事通haskell——它介于强大的简单ML和灵活的元程序Prolog之间……真是个怪物。你可以把Prolog看作一个Haskell,它只包含布尔函数/谓词。它对Haskell的灵活性是多么简单……
我将添加另一个在声明式/命令式编程讨论中很少出现的示例:用户界面!
在c#中,您可以使用各种技术构建UI。
在命令式端,你可以使用DirectX或OpenGL命令式地绘制按钮、复选框等。一行一行的(或者实际上是一个三角形一个三角形的)。如何绘制用户界面由您决定。
在声明端,有WPF。您只需编写一些XML(是的,是的,技术上是“XAML”),框架就会为您完成工作。你说用户界面是什么样的。如何做到这一点取决于系统。
总之,这是另一件值得思考的事情。仅仅因为一种语言是声明式或命令式的,并不意味着它没有另一种语言的某些特性。
此外,声明式编程的一个好处是,通过阅读代码通常更容易理解目的,而命令式编程则可以更好地控制执行。
要点是:
说明性->你想做什么
命令式->你想怎么做
推荐文章
- 实体框架核心:在上一个操作完成之前,在此上下文中开始的第二个操作
- 如何为构造函数定制Visual Studio的私有字段生成快捷方式?
- 如何使用JSON确保字符串是有效的JSON。网
- AppSettings从.config文件中获取值
- 通过HttpClient向REST API发布一个空体
- 如何检查IEnumerable是否为空或空?
- 自动化invokerrequired代码模式
- 在c#代码中设置WPF文本框的背景颜色
- 在c#中,什么是单子?
- c#和Java中的泛型有什么不同?和模板在c++ ?
- c#线程安全快速(est)计数器
- 如何将此foreach代码转换为Parallel.ForEach?
- 如何分裂()一个分隔字符串到一个列表<字符串>
- 如何转换列表<字符串>列表<int>?
- c#对象列表,我如何得到一个属性的和