什么是选项-操作数分离?

What is option–operand separation?

我最近看到,option-operand separation是Eiffel语言引入的一个原则(我没用过Eiffel)。

来自Wikipedia article

[Option–operand separation] states that an operation's arguments should contain only operands — understood as information necessary to its operation — and not options — understood as auxiliary information. Options are supposed to be set in separate operations.

这是否意味着一个函数应该只包含作为其功能一部分的 "essential" 个参数,并且不应该有任何改变功能的参数(而应该是一个单独的函数)?

谁能简单地解释一下,最好是用伪代码示例?

是的,这就是想法:参数不应该用于 select 特定行为。应该改用不同的方法(Eiffel 术语中的特征)。

示例。假设,有一种方法可以将二维图形移动到给定位置。可以使用极坐标或笛卡尔坐标指定位置:

move (coordinate_1, coordinate_2: REAL_64; is_polar: BOOLEAN)
    -- Move the figure to the position (coordinate_1, coordinate_2)
    -- using polar system if is_polar is True, and Cartesian system otherwise.

根据原理,最好定义两个函数:

cartesian_move (x, y: REAL_64)
    -- Move the figure to the position with Cartesian coordinates (x, y).

polar_move (rho, phi: REAL_64)
    -- Move the figure to the position with polar coordinates (rho, phi).

虽然这个原则似乎是普遍适用的,但一些面向对象的语言在某些情况下并没有提供足够的手段。明显的例子是在许多语言中具有相同名称的构造函数,因此使用选项成为唯一的选择(在这些情况下,解决方法是使用对象工厂)。