简体   繁体   English

声明式编程和命令式编程之间有什么区别?

[英]What's the difference between declarative and imperative programming?

I read the following paragraphs about architecture from Microsoft .Net: Architecting Applications for the Enterprise , by Dino Esposito and Andrea Saltarello: 我阅读了以下关于Microsoft .Net的架构:由Dino Esposito和Andrea Saltarello 构建企业应用程序

Whenever you take a declarative approach to coding rather than an imperative approach, you are delegating some responsibilities to some other layer of code—a layer that you don't control entirely. 无论何时采用声明性方法进行编码而不是命令式方法,您都要将一些职责委托给其他一些代码层 - 一个您无法完全控制的层。 So you're ultimately hoping that the intermediate tool will understand you correctly and won't encounter any trouble of its own along the way. 因此,您最终希望中间工具能够正确理解您,并且在此过程中不会遇到任何问题。 In software development, delegating to a third party makes development faster, but you should guarantee that users get exactly what you want them to. 在软件开发中,委托给第三方可以加快开发速度,但是您应该保证用户能够得到您想要的结果。

This doesn't mean that you shouldn't trust Visual Studio 2008 or similar wizard-based products. 这并不意味着您不应该信任Visual Studio 2008或类似的基于向导的产品。 The point is something else entirely. 这一点完全是另一回事。 In a large system, likely being developed by several teams, classic declarative programming just doesn't work. 在一个可能由多个团队开发的大型系统中,经典的声明性编程只是不起作用。 You need to have in your presentation a layer of code that decides what to display, where to read settings, and how to apply them. 您需要在演示文稿中包含一层代码,用于决定要显示的内容,在何处阅读设置以及如何应用这些设置。 Declarative programming is still a great option, but only if you write the engine and the wizards. 声明性编程仍然是一个很好的选择,但只有你编写引擎和向导。

Can someone explain to me in simple words what exactly declarative and imperative programming are? 有人可以用简单的语言向我解释究竟什么是声明性和命令式编程?

Declarative - I will describe what I want you to do, you figure it out (SQL, XSLT). 声明 - 我将描述我想要你做什么,你想出来(SQL,XSLT)。

Imperative - I will tell you exactly what to do, one step at a time (C#, Java). 势在必行 - 我会告诉你到底要做什么,一步一步(C#,Java)。

Examples. 例子。

Declarative programming: 声明性编程:

<asp:DropDownList runat="server" DataSourceID="ObjectDataSource1" />
<asp:ObjectDataSource runat="server" ID="ObjectDataSource1" ItemUpdating="..." />

Imperative programming: 命令式编程:

ObjectDataSource source = new ObjectDataSource();
source.ItemUpdating += ...;

DropDownList list = new DropDownList();
list.ID = "";
list.DataSource = source;
list.DataBind();

Declarative programming - What should be done 声明性编程 - 应该做什么

Imperative programming - How what you want should be done. 命令式编程 - 应该如何完成你想要的。

Declarative programming requires developers to say what is to be done. 声明性编程要求开发人员说明要做什么。 Imperative programming requires developers to define step by step how code should be executed. 命令式编程要求开发人员逐步定义代码的执行方式。

Example: LINQ in C# is declarative. 示例:C#中的LINQ是声明性的。

Read here for more: http://www.informit.com/articles/article.aspx?p=1330154&seqNum=4 在此阅读更多信息: http//www.informit.com/articles/article.aspx?p = 1330154&seqNum = 4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM