我一直在网上搜索声明式编程和命令式编程的定义,希望能给我一些启发。然而,我发现在一些资源中使用的语言令人生畏——例如在维基百科。 有没有人可以给我举一个现实世界的例子,让我对这个主题有一些了解(也许是c#)?
当前回答
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岁
其他回答
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岁
命令式编程是显式地告诉计算机做什么,以及如何做,比如指定顺序等
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)
我喜欢剑桥一门课程的解释和他们的例子:
声明性-指定要做什么,而不是如何做 例如:HTML描述的是什么应该出现在网页上,而不是它应该如何在屏幕上绘制 命令式-同时指定“什么”和“如何” int x;-什么(陈述性的) x = x + 1;——如何
这只是一个实际的例子,说明为什么CSS是声明式的,而JavaScript是命令式的。
假设我们有这个导航栏,用户当前正在查看“探索”选项,所以它被标记为当前选中。
<ul>
<li class="selected">
<p>Explore</p>
</li>
<li>
<p>Suggestions</p>
</li>
</ul>
我们希望当前选择的选项的标题是蓝色的,我们如何实现这与CSS和JavaScript?
CSS
li.selected > p {
color: blue;
}
李在这里。Selected > p声明了我们想要属性颜色的元素的模式:蓝色;待应用。结果是“探索”用蓝色突出显示,而“建议”没有。注意,我们的代码描述了我们想要发生什么,而不是如何发生。CSS选择器引擎如何找到“探索”?我们不知道,通常也不在乎。
JavaScript
let liElements = document.getElementsByTagName("li")
for (let i = 0; i < liElements.length; i++) {
if (liElements[i].className === "selected") {
let children = liElements[i].childNodes
for (let j = 0; j < children. length; j++) {
let child = children[j]
if (child.nodeType === Node.ELEMENT_NODE && child.tagName === "P") {
child.setAttribute("style", "color: blue")
}
}
}
}
这段代码要长得多,也更难理解。除此之外,它将蓝色应用于所选选项,但当所选类被删除时,它永远不会取消应用它。蓝色只有在页面重新加载时才会重置。注意,在这段代码中,我们一步一步精确地指定了需要做什么以及如何做。
结论
每种编程范例都有其优点。
CSS(声明)
简洁的 作为程序员,我们不能控制CSS内核如何做我们需要的事情。这给了CSS核心开发人员在任何时候改变CSS选择器实现的机会。为什么CSS核心需要改变?也许,CSS开发人员找到了一种更快的方法来应用属性。
JavaScript(必须)
定制。我们控制代码如何实现目标的所有方面。 适合解决各种各样的问题
这里和其他在线帖子的答案提到了以下内容:
使用声明式编程,您编写的代码描述您想要的东西,但不一定是如何得到它 您应该更喜欢声明式编程而不是命令式编程
他们没有告诉我们如何实现这一目标。为了使部分程序更具声明性,其他部分必须提供抽象来隐藏实现细节(即命令式代码)。
例如,LINQ比循环(for, while等)更具声明性,例如,你可以使用list. where()来获得一个新的过滤列表。为了实现这一点,微软已经完成了LINQ抽象背后的所有繁重工作。
事实上,函数式编程和函数式库更具有声明性的原因之一是因为它们抽象了循环和列表创建,隐藏了所有实现细节(很可能是带有循环的命令式代码)。
在任何程序中,都将同时拥有命令式代码和声明式代码,并且应该将所有命令式代码隐藏在特定于领域的抽象后面,以便程序的其他部分可以声明性地使用它们。
最后,尽管函数式编程和LINQ可以使您的程序更具声明性,但您总是可以通过提供更多的抽象来使程序更具声明性。例如:
// JavaScript example
// Least declarative
const bestProducts = [];
for(let i = 0; i < products.length; i++) {
let product = products[i];
if (product.rating >= 5 && product.price < 100) {
bestProducts.push(product);
}
}
// More declarative
const bestProducts = products.filter(function(product) {
return product.rating >= 5 && product.price < 100;
});
// Most declarative, implementation details are hidden in a function
const bestProducts = getBestProducts();
另外,声明式编程的极端是发明新的领域特定语言(DSL):
字符串搜索:正则表达式而不是自定义命令式代码 js: JSX而不是直接的DOM操作 AWS CloudFormation:用YAML代替CLI 关系型数据库:用SQL代替旧的读写api,如ISAM或VSAM。
推荐文章
- net HttpClient。如何POST字符串值?
- 我如何使一个方法的返回类型泛型?
- 何时处理CancellationTokenSource?
- 如何获取正在执行的程序集版本?
- AutoMapper vs valueinjector
- 为什么控制台不。Writeline,控制台。在Visual Studio Express中编写工作?
- 什么是.NET程序集?
- 字符串不能识别为有效的日期时间“格式dd/MM/yyyy”
- 函数应该返回空对象还是空对象?
- 如何转换日期时间?将日期时间
- 如何在c#中连接列表?
- 在c#中引用类型变量的“ref”的用途是什么?
- 防止在ASP中缓存。NET MVC中使用属性的特定操作
- 转换为值类型'Int32'失败,因为物化值为空
- c#中有任何连接字符串解析器吗?