Programming paradigm
Introduction to programming paradigm
Programming paradigm所表达的是编程的理念、思想,它指导着programming language的设计,进而影响programming language的方方面面,在programming language中,有相应的construct来实现它。在学习各种programming paradigm的时候,我觉得应该抓住各种paradigm的思想,然后再学习具体的programming language对具体programming paradigm的实现,关于这个观点,在wikipedia Generic programming#Stepanov–Musser and other generic programming paradigms中有这样的描述:
In this article we distinguish the high-level programming paradigms of generic programming, above, from the lower-level programming language genericity mechanisms used to implement them (see Programming language support for genericity).
上面这段话中的“high-level programming paradigms of generic programming”就是generic programming这种paradigm的核心思想,而“lower-level programming language genericity mechanisms used to implement them ”即为programming language对它的实现。
不同programming language对programming paradigm的实现是不同的。
wikipedia Programming paradigm
Programming paradigms are a way to classify programming languages based on their features. Languages can be classified into multiple paradigms.
NOTE:它是一种对programming language进行分类的方法
Some paradigms are concerned mainly with implications for the execution model of the language, such as allowing side effects, or whether the sequence of operations is defined by the execution model. Other paradigms are concerned mainly with the way that code is organized, such as grouping a code into units along with the state that is modified by the code. Yet others are concerned mainly with the style of syntax and grammar.
NOTE:不同的paradigm所关注的点是不同的
Common programming paradigms include:
1) imperative(命令式)
in which the programmer instructs(指示) the machine how to change its state,
- procedural which groups instructions into procedures,
NOTE: procedural语言的典型代表是C
- object-oriented which groups instructions together with the part of the state they operate on,
2) declarative(陈述式)
in which the programmer merely declares properties of the desired result, but not how to compute it
- functional in which the desired result is declared as the value of a series of function applications,
- logic in which the desired result is declared as the answer to a question about a system of facts and rules,
- mathematical in which the desired result is declared as the solution of an optimization problem
NOTE: 目前的主流programming language基本上属于上述两大类。
Symbolic techniques such as reflection, which allow the program to refer to itself, might also be considered as a programming paradigm. However, this is compatible with the major paradigms and thus is not a real paradigm in its own right.
NOTE: 关于reflection,参见
Theory\Reflection
For example, languages that fall into the imperative paradigm have two main features: they state the order in which operations occur, with constructs that explicitly control that order, and they allow side effects, in which state can be modified at one point in time, within one unit of code, and then later read at a different point in time inside a different unit of code. The communication between the units of code is not explicit. Meanwhile, in object-oriented programming, code is organized into objects that contain state that is only modified by the code that is part of the object. Most object-oriented languages are also imperative languages.
NOTE:当到了现在的这个层级,就会阅读越来越多的编程的理论,如果我没有记错的话,state**这个术语在多篇文章中出现过,在阅读Overview的时候,其中有这样的一段话“ In functional programming, programs are treated as a sequence of **stateless function evaluations.”,这突然让我想起来这是和面向对象相反的,面向对象是state的,是有side effect的;state的概念在编程语言理论是是非常重要的,下面的分析说明了state的本质:
其实state是基于memory的,即通过memory来记录state;functional programming是stateless,这是源于他不需要memory。而OOP是有state的,所以它需要memory。
In contrast, languages that fit the declarative paradigm do not state the order in which to execute operations. Instead, they supply a number of operations that are available in the system, along with the conditions under which each is allowed to execute. The implementation of the language's execution model tracks which operations are free to execute and chooses the order on its own. More at Comparison of multi-paradigm programming languages.
Overview
Just as software engineering (as a process) is defined by differing methodologies, so the programming languages (as models of computation) are defined by differing paradigms. Some languages are designed to support one paradigm (Smalltalksupports object-oriented programming, Haskell supports functional programming), while other programming languages support multiple paradigms (such as Object Pascal, C++, Java, C#, Scala, Visual Basic, Common Lisp, Scheme, Perl, PHP, Python, Ruby, Oz, and F#). For example, programs written in C++, Object Pascal or PHP can be purely procedural, purely object-oriented, or can contain elements of both or other paradigms. Software designers and programmers decide how to use those paradigm elements.
In object-oriented programming, programs are treated as a set of interacting objects. In functional programming, programs are treated as a sequence of stateless function evaluations. When programming computers or systems with many processors, in process-oriented programming, programs are treated as sets of concurrent processes acting on logically shared data structures.
NOTE:上面这段中关于functional programming 的描述中的stateless与object-oriented language的state是非常具对比价值的;
Programming paradigms can also be compared with programming models which allow invoking an execution model by using only an API. Programming models can also be classified into paradigms, based on features of the execution model.
For parallel computing, using a programming model instead of a language is common. The reason is that details of the parallel hardware leak into(泄露) the abstractions used to program the hardware. This causes the programmer to have to map patterns in the algorithm onto patterns in the execution model (which have been inserted due to leakage of hardware into the abstraction). As a consequence, no one parallel programming language maps well to all computation problems. It is thus more convenient to use a base sequential language and insert API calls to parallel execution models, via a programming model. Such parallel programming models can be classified according to abstractions that reflect the hardware, such as shared memory, distributed memory with message passing, notions of place visible in the code, and so forth. These can be considered flavors of programming paradigm that apply to only parallel languages and programming models.
NOTE:上面这段中的关于在parallel computing中使用programming model而非language的解释可以结合在*programming models解释相结合。由于the execution model of **parallel hardware* often must expose features of the hardware in order to achieve high performance, The large amount of variation in parallel hardware causes a concurrent need for a similarly large number of parallel execution models.这就导致了It is impractical to make a new language for each execution model, hence it is a common practice to invoke the behaviors of the parallel execution model via an API.
TODO
1) ybrikman Six programming paradigms that will change how you think about coding
这篇文章对几种programming paradigm有着较好的描述。