什么是“正交性”?

发布于 2024-08-06 08:25:01 字数 48 浏览 7 评论 0原文

在谈论编程语言时,“正交性”是什么意思?

正交性的一些例子有哪些?

What does "orthogonality" mean when talking about programming languages?

What are some examples of Orthogonality?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(18

梦屿孤独相伴 2024-08-13 08:25:02

来自 Michael C. Feathers 的书《有效处理遗留代码》:

如果您想更改代码中的现有行为,并且您必须去一个地方进行更改,那么您就拥有了正交性。

From Michael C. Feathers' book "Working Effectively With Legacy Code":

If you want to change existing behavior in your code and there is exactly one place you have to go to make that change, you've got orthogonality.

━╋う一瞬間旳綻放 2024-08-13 08:25:02

在编程语言中,如果编程语言功能没有任何限制(或例外),则该功能被称为正交。
例如,在 Pascal 中,函数不能返回结构化类型。这是对函数返回值的限制。因此我们将其视为非正交特征。 ;)

In programming languages a programming language feature is said to be orthogonal if it is bounded with no restrictions (or exceptions).
For example, in Pascal functions can't return structured types. This is a restriction on returning values from a function. Therefore we it is considered as a non-orthogonal feature. ;)

大海や 2024-08-13 08:25:02

编程中的正交性:

正交性是一个重要的概念,它解决了如何以相对较少的方式组合相对较少数量的组件以获得所需的结果。它与简单性相关;设计越正交,例外情况就越少。这使得学习、阅读和编写编程语言程序变得更加容易。正交特征的含义与上下文无关;关键参数是对称性和一致性(例如,指针是一个正交概念)。

来自维基百科

Orthogonality in Programming:

Orthogonality is an important concept, addressing how a relatively small number of components can be combined in a relatively small number of ways to get the desired results. It is associated with simplicity; the more orthogonal the design, the fewer exceptions. This makes it easier to learn, read and write programs in a programming language. The meaning of an orthogonal feature is independent of context; the key parameters are symmetry and consistency (for example, a pointer is an orthogonal concept).

from Wikipedia

聊慰 2024-08-13 08:25:02

编程语言中的正交性意味着相对较小的一组
原始构造可以通过相对较少的方式组合
构建语言的控制和数据结构。此外,每个位置
原语的合理组合是合法且有意义的。例如,考虑数据类型。假设一种语言有四种基本数据类型(整数、浮点数、
双精度和字符)和两个类型运算符(数组和指针)。如果两个
类型运算符可以应用于自身和四种基本数据类型,
可以定义大量的数据结构。
正交语言特征的含义与
它在程序中出现的上下文。 (正交这个词来自
正交向量的数学概念,正交向量相互独立
其他。)正交性源于原初关系的对称性
提夫。缺乏正交性会导致语言规则的例外。
例如,在支持指针的编程语言中,应该是
可以定义一个指针来指向该语言中定义的任何特定类型。
但是,如果不允许指针指向数组,则无法定义许多潜在有用的用户定义数据结构。
我们可以通过比较来说明正交性作为设计概念的使用
IBM 大型机汇编语言的一方面
以及VAX系列小型机。我们考虑一个简单的情况:
将驻留在内存或寄存器中的两个 32 位整数值相加
用总和替换两个值之一。 IBM 大型机有两个
用于此目的的指令,其形式

A Reg1, memory_cell
AR Reg1, Reg2

为 Reg1 和 Reg2 代表寄存器。它们的语义是

Reg1 ← contents(Reg1) + contents(memory_cell)
Reg1 ← contents(Reg1) + contents(Reg2)

32 位整数值的 VAX 加法指令

ADDL operand_1, operand_2

的语义是

operand_2 ← contents(operand_1) + contents(operand_2)

在这种情况下,任一操作数可以是寄存器或存储单元。
VAX 指令设计是正交的,因为单个指令可以
使用寄存器或存储单元作为操作数。有两种方法可以
指定可以以所有可能的方式组合的操作数。 IBM 设计
不是正交的。四种操作数组合中只有两种可能
合法,并且两者需要不同的指令, A 和 AR 。 IBM 设计
受到更多限制,因此可写性更差。例如,您不能添加
两个值并将总和存储在内存位置。此外,IBM
由于限制和额外的指导,设计变得更难学习。
正交性与简单性密切相关:正交性越高
语言设计得越好,语言规则要求的例外就越少。更少
例外意味着设计中更高程度的规律性,这使得
语言更容易学习、阅读和理解。任何学过符号的人
英语的重要部分可以证明学习它的困难
许多规则例外(例如,i 在 e 之前,除了 c 之后)。

Orthogonality in a programming language means that a relatively small set of
primitive constructs can be combined in a relatively small number of ways to
build the control and data structures of the language. Furthermore, every pos-
sible combination of primitives is legal and meaningful. For example, consider data types. Suppose a language has four primitive data types (integer, float,
double, and character) and two type operators (array and pointer). If the two
type operators can be applied to themselves and the four primitive data types,
a large number of data structures can be defined.
The meaning of an orthogonal language feature is independent of the
context of its appearance in a program. (the word orthogonal comes from the
mathematical concept of orthogonal vectors, which are independent of each
other.) Orthogonality follows from a symmetry of relationships among primi-
tives. A lack of orthogonality leads to exceptions to the rules of the language.
For example, in a programming language that supports pointers, it should be
possible to define a pointer to point to any specific type defined in the language.
However, if pointers are not allowed to point to arrays, many potentially useful user-defined data structures cannot be defined.
We can illustrate the use of orthogonality as a design concept by compar-
ing one aspect of the assembly languages of the IBM mainframe computers
and the VAX series of minicomputers. We consider a single simple situation:
adding two 32-bit integer values that reside in either memory or registers and
replacing one of the two values with the sum. The IBM mainframes have two
instructions for this purpose, which have the forms

A Reg1, memory_cell
AR Reg1, Reg2

where Reg1 and Reg2 represent registers. The semantics of these are

Reg1 ← contents(Reg1) + contents(memory_cell)
Reg1 ← contents(Reg1) + contents(Reg2)

The VAX addition instruction for 32-bit integer values is

ADDL operand_1, operand_2

whose semantics is

operand_2 ← contents(operand_1) + contents(operand_2)

In this case, either operand can be a register or a memory cell.
The VAX instruction design is orthogonal in that a single instruction can
use either registers or memory cells as the operands. There are two ways to
specify operands, which can be combined in all possible ways. The IBM design
is not orthogonal. Only two out of four operand combinations possibilities are
legal, and the two require different instructions, A and AR . The IBM design
is more restricted and therefore less writable. For example, you cannot add
two values and store the sum in a memory location. Furthermore, the IBM
design is more difficult to learn because of the restrictions and the additional instruction.
Orthogonality is closely related to simplicity: The more orthogonal the
design of a language, the fewer exceptions the language rules require. Fewer
exceptions mean a higher degree of regularity in the design, which makes the
language easier to learn, read, and understand. Anyone who has learned a sig-
nificant part of the English language can testify to the difficulty of learning its
many rule exceptions (for example, i before e except after c).

浅忆 2024-08-13 08:25:02

正交性的基本思想是概念上不相关的事物在系统中不应该相关。架构中那些确实与其他部分无关的部分,例如数据库和 UI,不应该需要一起更改。其中一个的改变不应导致另一个的改变。

The basic idea of orthogonality is that things that are not related conceptually should not be related in the system. Parts of the architecture that really have nothing to do with the other, such as the database and the UI, should not need to be changed together. A change to one should not cause a change to the other.

甜警司 2024-08-13 08:25:02

正交性是指概念上不相关的事物不应在系统中相关,因此架构中彼此无关的部分(例如数据库和 UI)不应一起更改。对系统某一部分的更改不应导致另一部分的更改。

例如,如果您更改屏幕上的几行并导致数据库模式发生更改,这称为耦合。您通常希望最大限度地减少大多数不相关的事物之间的耦合,因为它会增长,并且从长远来看,系统可能会成为维护的噩梦。

Orthogonality is the idea that things that are not related conceptually should not be related in the system so parts of the architecture that have nothing to do with each other, like the database and UI should not be changed together. A change to one part of your system should not cause the change to the other.

If for example, you change a few lines on the screen and cause a change in the database schema, this is called coupling. You usually want to minimize coupling between things that are mostly unrelated because it can grow and the system can become a nightmare to maintain in the long run.

放我走吧 2024-08-13 08:25:01

正交性 是指“更改 A 不会更改 B”的属性。正交系统的一个例子是收音机,其中改变电台不会改变音量,反之亦然。

非正交系统就像直升机,改变速度就可以改变方向。

在编程语言中,这意味着当您执行一条指令时,除了该指令之外不会发生任何事情(这对于调试非常重要)。

引用指令集时也有特定的含义。

Orthogonality is the property that means "Changing A does not change B". An example of an orthogonal system would be a radio, where changing the station does not change the volume and vice-versa.

A non-orthogonal system would be like a helicopter where changing the speed can change the direction.

In programming languages this means that when you execute an instruction, nothing but that instruction happens (which is very important for debugging).

There is also a specific meaning when referring to instruction sets.

你对谁都笑 2024-08-13 08:25:01

来自 Eric S. Raymond 的“UNIX 编程艺术”

正交性是最重要的属性之一,可以帮助使复杂的设计变得紧凑。在纯粹的正交设计中,操作没有副作用;每个操作(无论是 API 调用、宏调用还是语言操作)仅更改一件事,而不会影响其他事情。有一种且只有一种方法可以更改您所控制的任何系统的每个属性。

From Eric S. Raymond's "Art of UNIX programming"

Orthogonality is one of the most important properties that can help make even complex designs compact. In a purely orthogonal design, operations do not have side effects; each action (whether it's an API call, a macro invocation, or a language operation) changes just one thing without affecting others. There is one and only one way to change each property of whatever system you are controlling.

热鲨 2024-08-13 08:25:01

想象一下它能够改变一件事而不会对另一部分产生看不见的影响。

Think of it has being able to change one thing without having an unseen affect on another part.

白馒头 2024-08-13 08:25:01

从广义上讲,正交性是两个事物之间的关系,使得它们彼此影响最小。

该术语来自数学,如果两个向量以直角相交,则它们是正交的。

考虑一个典型的二维笛卡尔空间(带有 X/Y 轴的典型网格)。绘制两条线:x=1 和 y=1。两条线是正交的。你可以通过改变x来改变x=1,这不会对另一行产生影响,反之亦然。

在软件中,该术语适用于谈论系统中彼此独立运行的两个部分的情况。

Broadly, orthogonality is a relationship between two things such that they have minimal effect on each other.

The term comes from mathematics, where two vectors are orthogonal if they intersect at right angles.

Think about a typical 2 dimensional cartesian space (your typical grid with X/Y axes). Plot two lines: x=1 and y=1. The two lines are orthogonal. You can change x=1 by changing x, and this will have no effect on the other line, and vice versa.

In software, the term can be appropriately used in situations where you're talking about two parts of a system which behave independently of each other.

反话 2024-08-13 08:25:01

如果你有一组构造。如果一种语言允许程序员自由地混合这些结构,则该语言被称为正交。例如,在 C 中你不能返回一个数组(静态数组),在这种情况下 C 被认为是非正交的:

int[] fun(); // you can't return a static array.
// Of course you can return a pointer, but the langauge allows passing arrays.
// So, it is unorthognal in case.

If you have a set of constructs. A langauge is said to be orthogonal if it allows the programmer to mix these constructs freely. For example, in C you can't return an array(static array), C is said to be unorthognal in this case:

int[] fun(); // you can't return a static array.
// Of course you can return a pointer, but the langauge allows passing arrays.
// So, it is unorthognal in case.
随梦而飞# 2024-08-13 08:25:01

大多数答案都非常冗长,甚至晦涩难懂。要点是:如果一个工具是正交的,则可以添加、替换或删除它,以支持更好的工具,而不会搞砸其他一切。

这就像木匠拥有锤子和锯子(可用于锤击或锯切)或拥有一些新型锤子/锯子组合(旨在锯木,然后将其锤击在一起)之间的区别。两者都适用于锯切然后一起锤击,但如果您执行某些需要锯切但不需要锤击的任务,则只有正交工具才有效。同样,如果您需要拧螺丝而不是锤击,如果锯子与锤子正交(不与锤子混合),则无需扔掉锯子。

典型的例子是 unix 命令行工具:您有一个工具用于获取磁盘的内容 (dd),另一个工具用于过滤文件中的行 (grep),另一个工具用于将这些行写入文件 (cat),等等。都可以随意混合搭配。

Most of the answers are very long-winded, and even obscure. The point is: if a tool is orthogonal, it can be added, replaced, or removed, in favor of better tools, without screwing everything else up.

It's the difference between a carpenter having a hammer and a saw, which can be used for hammering or sawing, or having some new-fangled hammer/saw combo, which is designed to saw wood, then hammer it together. Either will work for sawing and then hammering together, but if you get some task that requires sawing, but not hammering, then only the orthogonal tools will work. Likewise, if you need to screw instead of hammering, you won't need to throw away your saw, if it's orthogonal (not mixed up with) your hammer.

The classic example is unix command line tools: you have one tool for getting the contents of a disk (dd), another for filtering lines from the file (grep), another for writing those lines to a file (cat), etc. These can all be mixed and matched at will.

栀梦 2024-08-13 08:25:01

在谈论有关编程语言的项目决策时,正交性可能被视为您根据过去所看到的内容来预测有关该语言的其他内容的容易程度。

例如,用一种语言您可以:

str.分割

用于分割字符串和

长度(字符串)

用于获取长度。

在更正交的语言上,您始终会使用 str.x 或 x(str)。

当你要克隆一个对象或做其他任何事情时,你会知道是否使用

克隆(obj)

obj.克隆

这是编程语言正交的要点之一。这可以避免您查阅手册或询问某人。

维基百科文章更多地讨论了复杂设计或低级语言的正交性。
正如上面有人在评论中建议的那样,Sebesta 的书清楚地讨论了正交性。

如果我只用一句话,我会说,当一种编程语言的未知部分根据您所看到的内容按预期运行时,它就是正交的。
或者……没什么惊喜。

;)

While talking about project decisions on programming languages, orthogonality may be seen as how easy is for you to predict other things about that language for what you've seen in the past.

For instance, in one language you can have:

str.split

for splitting a string and

len(str)

for getting the lenght.

On a language more orthogonal, you would always use str.x or x(str).

When you would clone an object or do anything else, you would know whether to use

clone(obj)

or

obj.clone

That's one of the main points on programming languages being orthogonal. That avoids you to consult the manual or ask someone.

The wikipedia article talks more about orthogonality on complex designs or low level languages.
As someone suggested above on a comment, the Sebesta book talks cleanly about orthogonality.

If I would use only one sentence, I would say that a programming language is orthogonal when its unknown parts act as expected based on what you've seen.
Or... no surprises.

;)

蹲在坟头点根烟 2024-08-13 08:25:01

来自 Robert W. Sebesta 的《编程语言概念》:

作为高级语言中缺乏正交性的例子,
考虑 C 中的以下规则和例外。虽然 C 有两个
各种结构化数据类型、数组和记录(结构)、记录
可以从函数返回,但数组不能。一个成员
结构可以是除 void 之外的任何数据类型或相同的结构
类型。数组元素可以是除 void 或函数之外的任何数据类型。
参数按值传递,除非它们是数组,在这种情况下
实际上,它们是通过引用传递的(因为
C 程序中不带下标的数组名被解释为
数组第一个元素的地址)

From Robert W. Sebesta's "Concepts of Programming Languages":

As examples of the lack of orthogonality in a high-level language,
consider the following rules and exceptions in C. Although C has two
kinds of structured data types, arrays and records (structs), records
can be returned from functions but arrays cannot. A member of a
structure can be any data type except void or a structure of the same
type. An array element can be any data type except void or a function.
Parameters are passed by value, unless they are arrays, in which case
they are, in effect, passed by reference (because the appearance of an
array name without a subscript in a C program is interpreted to be
the address of the array’s first element)

潦草背影 2024-08-13 08:25:01

来自wikipedia

计算机科学

正交性是一种系统设计属性,促进复杂设计的可行性和紧凑性。正交性保证修改系统组件产生的技术效果既不会产生副作用,也不会传播副作用到系统的其他组件。由组件组成的系统的紧急行为应严格由其逻辑的形式定义来控制,而不是由不良集成(即模块和接口的非正交设计)产生的副作用来控制。正交性减少了测试和开发时间,因为更容易验证既不会引起副作用也不依赖副作用的设计。

例如,汽车具有正交组件和控制(例如,加速车辆不会影响任何其他东西,除了专门涉及加速功能的组件)。另一方面,非正交设计的转向可能会影响其制动(例如电子稳定控制),或者其速度会调整其悬架。1 因此,这种用法被视为源自数学中正交的使用:可以通过将向量分别投影到一组基向量的每个成员上来将向量投影到子空间上当且仅当基向量相互正交时才添加投影。

如果任何指令可以以任何寻址模式使用任何寄存器,则称指令集是正交的。该术语是由于将指令视为向量,其组成部分是指令字段而产生的。一个字段标识要操作的寄存器,另一个字段指定寻址模式。正交指令集对寄存器和寻址模式的所有组合进行唯一编码。

from wikipedia:

Computer science

Orthogonality is a system design property facilitating feasibility and compactness of complex designs. Orthogonality guarantees that modifying the technical effect produced by a component of a system neither creates nor propagates side effects to other components of the system. The emergent behavior of a system consisting of components should be controlled strictly by formal definitions of its logic and not by side effects resulting from poor integration, i.e. non-orthogonal design of modules and interfaces. Orthogonality reduces testing and development time because it is easier to verify designs that neither cause side effects nor depend on them.

For example, a car has orthogonal components and controls (e.g. accelerating the vehicle does not influence anything else but the components involved exclusively with the acceleration function). On the other hand, a non-orthogonal design might have its steering influence its braking (e.g. electronic stability control), or its speed tweak its suspension.1 Consequently, this usage is seen to be derived from the use of orthogonal in mathematics: One may project a vector onto a subspace by projecting it onto each member of a set of basis vectors separately and adding the projections if and only if the basis vectors are mutually orthogonal.

An instruction set is said to be orthogonal if any instruction can use any register in any addressing mode. This terminology results from considering an instruction as a vector whose components are the instruction fields. One field identifies the registers to be operated upon, and another specifies the addressing mode. An orthogonal instruction set uniquely encodes all combinations of registers and addressing modes.

逐鹿 2024-08-13 08:25:01

来自维基百科

正交性是一种系统设计
财产促进可行性和
复杂设计的紧凑性。
正交性保证
修改技术效果
由系统的一个组件产生
既不创建也不传播侧面
对其他成分的影响
系统。一个人的突现行为
由组件组成的系统应
受到正式的严格控制
其逻辑的定义,而不是由
不良造成的副作用
积分,即非正交
模块和接口的设计。
正交性减少了测试和
开发时间,因为它更容易
验证设计不会导致
副作用也不依赖于它们。

例如,汽车具有正交
组件和控件(例如
车辆不加速
影响除了
专门涉及的组件
加速函数)。上
另一方面,非正交设计
可能有其转向影响其
制动(例如电子稳定
控制),或其速度调整
暂停。[1]因此,这
用法被视为源自
正交在数学中的应用:一
可以将向量投影到子空间上
通过将其投影到每个成员
分别设置基向量和
添加投影当且仅当
基向量互为
正交。

据说指令集是
正交如果任何指令都可以使用
任何寻址模式下的任何寄存器。
该术语源于
将指令视为向量
其组成部分是指令
字段。一个字段标识
要操作的寄存器,以及
另一个指定寻址模式。
独特的正交指令集
对寄存器的所有组合进行编码
和寻址模式。

用最简单的术语来说,如果改变一个事物对另一个事物没有影响,则两个事物是正交的。

From Wikipedia:

Orthogonality is a system design
property facilitating feasibility and
compactness of complex designs.
Orthogonality guarantees that
modifying the technical effect
produced by a component of a system
neither creates nor propagates side
effects to other components of the
system. The emergent behavior of a
system consisting of components should
be controlled strictly by formal
definitions of its logic and not by
side effects resulting from poor
integration, i.e. non-orthogonal
design of modules and interfaces.
Orthogonality reduces testing and
development time because it is easier
to verify designs that neither cause
side effects nor depend on them.

For example, a car has orthogonal
components and controls (e.g.
accelerating the vehicle does not
influence anything else but the
components involved exclusively with
the acceleration function). On the
other hand, a non-orthogonal design
might have its steering influence its
braking (e.g. electronic stability
control), or its speed tweak its
suspension.[1] Consequently, this
usage is seen to be derived from the
use of orthogonal in mathematics: One
may project a vector onto a subspace
by projecting it onto each member of a
set of basis vectors separately and
adding the projections if and only if
the basis vectors are mutually
orthogonal.

An instruction set is said to be
orthogonal if any instruction can use
any register in any addressing mode.
This terminology results from
considering an instruction as a vector
whose components are the instruction
fields. One field identifies the
registers to be operated upon, and
another specifies the addressing mode.
An orthogonal instruction set uniquely
encodes all combinations of registers
and addressing modes.

To put it in the simplest terms possible, two things are orthogonal if changing one has no effect upon the other.

飘落散花 2024-08-13 08:25:01

正交性是指语言由一组独立的原始结构组成的程度,这些结构可以根据需要组合起来以表达程序。
如果对如何组合功能没有限制,则功能是正交的

Example : non-orthogonality

。 PASCAL:函数不能返回结构化类型。
函数式语言是高度正交的。

Orthogonality means the degree to which language consists of a set of independent primitive constructs that can be combined as necessary to express a program.
Features are orthogonal if there are no restrictions on how they may be combined

Example : non-orthogonality

PASCAL: functions can't return structured types.
Functional Languages are highly orthogonal.

未央 2024-08-13 08:25:01

编程语言中正交性的现实生活示例

已经有很多答案在指定一些虚构示例的同时解释了正交性通常是什么。例如这个答案很好地解释了。我想提供(并收集)编程语言中正交或非正交功能的一些现实生活示例:

C++20 模块和命名空间

正交: cppreference-page 关于 c++20 中新模块系统的内容如下:

模块与命名空间

正交

在这种情况下,他们写道模块与命名空间正交因为像 import foo 这样的语句不会导入与 foo 相关的模块命名空间:

import foo;            // foo exports foo::bar()
bar ();                // Error
foo::bar ();           // Ok
using namespace foo;
bar ();                // Ok

(改编自 modules-cppcon2017 幻灯片 9)

Real life examples of orthogonality in programming languages

There are a lot of answers already that explain what orthogonality generally is while specifying some made up examples. E.g. this answer explains it well. I wanted to provide (and gather) some real life examples of orthogonal or non-orthogonal features in programming languages:

Orthogonal: C++20 Modules and Namespaces

On the cppreference-page about the new Modules system in c++20 is written:

Modules are orthogonal to namespaces

In this case they write that modules are orthogonal to namespaces because a statement like import foo will not import the module-namespace related to foo:

import foo;            // foo exports foo::bar()
bar ();                // Error
foo::bar ();           // Ok
using namespace foo;
bar ();                // Ok

(adapted from modules-cppcon2017 slide 9)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文