Java 是正交的吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3272019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:05:44  来源:igfitidea点击:

Is Java orthogonal?

javatheorydefinitionorthogonal

提问by mbc

I am wondering if Java is orthogonal or not, and if yes, then which are its features that make it orthogonal. How can you determine if a language is orthogonal or not? For example, I found on some website that C++ is not orthogonal, but no explanations, why not. What other languages are orthogonal? Please help me, because there is almost no information on the internet about this topic.

我想知道 Java 是否是正交的,如果是,那么它的哪些特性使其正交。如何确定一种语言是否正交?比如我在某个网站上发现C++不是正交的,但是没有解释,为什么不呢。还有哪些其他语言是正交的?请帮助我,因为互联网上几乎没有关于此主题的信息。

Thanks

谢谢

回答by bakkal

The Art of UNIX Programming, Chapter 4. Modularity, Orthogonality, Page 89:

UNIX 编程艺术,第 4 章。模块化,正交性,第 89 页:

Orthogonality

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.

正交性

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

Programming Language Pragmatics, Chapter 6, Page 228:

编程语言语用学,第 6 章,第 228 页:

Orthogonalitymeans that features can be used in any combination, that the combinations all make sense, and that the meaning of a given feature is consistent, regardless of the other features with which it is combined.

正交性意味着特征可以任意组合使用,组合都有意义,并且给定特征的含义是一致的,不管它与其他特征组合如何

On Lisp, 5.2 Orthogonality:

在 Lisp 上,5.2 正交性:

An orthogonal language is one in which you can express a lot by combining a small number of operators in a lot of different ways.

正交语言是一种可以通过以多种不同方式组合少量运算符来表达很多内容的语言。



I think an orthogonal programming language would be one where each of its features have minimal or no side effects, so they can be used without thinking about how that usage will affect other features. I borrow this from the definition of an orthogonal API.

我认为正交编程语言将是一种其每个功能都具有最小或没有副作用的语言,因此可以使用它们而无需考虑该用法将如何影响其他功能。我从正交 API 的定义中借用了这一点。

In Java you'd have to evaluate for example if there is a combination of keywords/constructs that could affect each other when used simultaneously on an identifier. For example when applying publicand staticto a method, they do not interfere with each other, so these two are orthogonal (no side effects besides what the keyword is intended to do)

例如,在 Java 中,您必须评估是否存在在标识符上同时使用时可能相互影响的关键字/结构组合。例如,当应用publicstatic到一个方法时,它们不会相互干扰,所以这两个是正交的(除了关键字打算做什么之外没有副作用)

You'd have to do that to all its features to prove the orthogonality. That is one way to go about it. I do not think there exists a clear cut isor is notorthogonal in this matter either.

您必须对其所有功能都这样做以证明正交性。这是一种方法。我不认为存在一个明确的不是在这个问题上正交无论是。

回答by Elf King

Using the term orthogonal programming language is unusual. Typically, in computer science you are really talking about orthogonal instruction-sets. However, if we are to extend the meaning to the grammar of a language:

使用术语正交编程语言是不寻常的。通常,在计算机科学中,您实际上是在谈论正交指令集。但是,如果我们要将含义扩展到语言的语法:

"...meaning [the language] has a relatively small number of basic constructs and a set of rules for combining those constructs. Every construct has a type associated with it and there are no restrictions on those types...."see ALGOL

“......意味着[语言]具有相对较少的基本构造和一组用于组合这些构造的规则。每个构造都有一个与之关联的类型,并且对这些类型没有限制......”参见ALGOL

Then we can assume that if not all instructions in the language can work on all datatypes will yield non-orthogonality. This however does not mean that the converse is true, that is to say if all language instructions do work on all data types, it does not necessarily mean that the language is orthogonal.

那么我们可以假设,如果不是语言中的所有指令都适用于所有数据类型,则会产生非正交性。然而,这并不意味着反之亦然,也就是说,如果所有语言指令都适用于所有数据类型,则并不一定意味着该语言是正交的。

More formally, an orthogonal language would have exactly ONE way to do a given operation. Non-orthogonal languages would have more than one way to achieve the same effect.

更正式地说,正交语言将只有一种方法来执行给定的操作。非正交语言将有不止一种方法可以达到相同的效果。

Simplest example:

最简单的例子:

for loop; vs. while loop;

for and while are non-orthogonal.

for 和 while 是非正交的。

回答by JoseMarmolejos

Orthogonality is feature of your design independent of the language. Sure some language make it easier for you to have an orthogonal design for your system but you shouldn't focus on a specific language to keep your system's design as orthogonal as possible.

正交性是独立于语言的设计特征。当然,某些语言可以让您更轻松地为您的系统进行正交设计,但您不应该专注于特定语言来保持系统设计尽可能正交。

回答by jens

Orthogonality is not really a language feature as such, even though some languages have features that promote orthogonality (such as annotations, built-in AOP, ..). Regarding orthogonality in Java: I have written a little case study about this using log4j as example: "Orthogonality By Example" - you might find this useful.

正交性并不是真正的语言特性,即使某些语言具有促进正交性的特性(例如注释、内置 AOP 等)。关于 Java 中的正交性:我使用 log4j 编写了一个关于此的小案例研究:“ Orthogonality By Example” - 您可能会发现这很有用。

回答by Behrooz Tabesh

Lack of Orthogonality in C:

C 中缺乏正交性:

  • An array can contain any data type except void
  • Parameters are passed by value but array are passed by reference
  • 数组可以包含任何数据类型,除了 void
  • 参数按值传递,数组按引用传递

A programming language is consider orthogonalif:

在以下情况下,编程语言被认为是正交的

  1. there is a relatively small set of primitive constructs that can combine in a relatively small number of ways to build data and control structures

  2. every possible structure is legal

  1. 有一组相对较小的原始结构,可以以相对较少的方式组合来构建数据和控制结构

  2. 每个可能的结构都是合法的

For example a language with 4 primitive data types (Int, Float, Double, Char) and two
type operators (Array and Pointer) can create a relatively large number of data structures.

例如,具有 4 种原始数据类型(Int、Float、Double、Char)和两种
类型运算符(数组和指针)的语言可以创建相对大量的数据结构。

So the advantage of orthogonality is to make the language simple and regular because there are less exceptions.

所以正交性的好处是让语言变得简单和规则,因为异常少。