ALevel-CS Chapter 25
Programming paradigms
25.01 Programming paradigms
Key Terms
- Programming paradigm - A fundamental style of programming
Low-level programming paradigm
The features of low-level programming languages give us the ability to manipulate the contents of memory addresses and registers directly and exploit the architecture of a given processor.
that each different type of processor has its own programming language.
There are ‘families’ of processors that are designed with similar architectures and therefore use similar programming languages.
the Intel processor family uses the x86 instruction set.
Imperative programming paradigm
Imperative programming involves writing a program as a sequence of explicit steps that are executed by the processor.
An imperative program tells the computer how to get a desired result, in contrast to declarative programming where a program describes what the desired result should be.
There are many imperative programming languages, Pascal, C and Basic to name just a few.
Object-oriented programming paradigm
The object-oriented paradigm is based on objects interacting with one another. Many programming languages that were originally imperative have been developed further to support the objectoriented paradigm. Examples include Pascal(under the name Delphi or Object Pascal) and Visual Basic (the .NET version being the first fully object-oriented version). Newer languages, such as Python and Java, were designed to be object-oriented from the beginning.
Declarative programming paradigm
Programming languages such as Pascal, VB and Python are referred to as ‘imperative programming languages’ because the programmer writes sequences of statements that reflect how to solve the problem.
Declarative programs are expressed as formal logic and computations are deductions from the formal logic statements.
Declarative programming languages include SQL and Prolog.
命令式编程(Imperative) 和 声明式编程(Declarative)
- 命令式编程(Imperative):详细的命令机器怎么(How)去处理一件事情以达到你想要的结果(What);
- 声明式编程( Declarative):只告诉你想要的结果(What),机器自己摸索过程(How)
命令式编程更加的精细化,更严谨,程序也会一是不够的执行你的命令。然后声明式编程能在特定的更高层面代码领域是给我们带来效率的提升,程序员只需要对想要的结果(What)进行深思熟虑,程序会自动的解决过程(How)。当然代码看起来更简洁也是大大的满足了众多强迫症程序猿。
传统上主流编程语言都是偏向命令式的,因为我们的硬件运行环境都是图灵机的升级版本,软件的功能就是指导硬件执行预设的动作。甚至有一种说法,程序开发工作的本质就是命令式的,毕竟为老板的小目标(what)找到具体的技术实现方案(how)才是我们的本职工作。但是,如果仔细观察一下现代编程的日常,就会发现,绝大多数的编程工作是建立在声明式API的基础之上的。
比如说,要显示界面,我们通过字符串操作拼接出HTML问本,为了调用服务,我们拼接出URL和JSON文本,为了访问数据库,我们拼接出SQL请求。很少有人清楚,如何使用命令式的指令一步步的构造出完整的应用程序。即使是最简单的前台拖拽动作,我们所会的多半也只是调用一个Draggable组件,监听(声明)一下拖拽结束时的触发动作。在这个意义上说,我们所掌握的编程技能只是从一种声明式视图映射到另一种声明式视图之间的转换策略而已。
整个软件开发生态环境正在不断向着声明式和命令式水乳交融的方向发展。以前,为了突出声明式的部分,我们会选择模板语言,即在描述性内容中嵌入少量的命令式控制逻辑。而在今天,出现了JSX这种直接将描述性内容嵌入到命令式上下文中的技术。更进一步,类似SwiftUI这种基于通用程序语言直接实现声明式表达的技术正快步向我们走来