在 "Clean Code," 中,可变数量的参数如何影响函数是 monad、dyad 还是 triad?
In "Clean Code," how do variable number of arguments affect whether the function is monad, dyad or triad?
我正在看书"Clean code"。它有一段关于传递给函数的变量参数以及如何命名函数,args。
Argument Lists
Sometimes we want to pass a variable number of arguments into a function. Consider, for example, the String.format method:
String.format("%s worked %.2f hours.", name, hours);
If the variable arguments are all treated identically, as they are in the example above, then they are equivalent to a single argument of type List. By that reasoning, String.format is actually dyadic. Indeed, the declaration of String.format as shown below is clearly dyadic.
public String format(String format, Object... args)
So all the same rules apply. Functions that take variable arguments can be monads, dyads, or even triads. But it would be a mistake to give them more arguments than that.
void monad(Integer... args);
void dyad(String name, Integer... args);
void triad(String name, int count, Integer... args);
monad - 带有一个 arg 的函数,dyad,dyadic - 带有 2 个 args 的函数,triad - 3 个 args。
谁能解释一下这段话?我唯一得到的是这个:
即使您有许多相同类型的参数,您也可以将它们放在一个列表中,例如 - Integer ...args 表示传递了许多 int 参数。
同时,程序员仍应遵循相同的建议 - 编写不超过 3 个参数的函数,通过显示与 Object ...args.
相同类型的参数,二元函数仍然可以有 2 个参数
我说的对吗,或者这段话还有别的意思吗?
作者只是想说在统计一个函数是monad, dyad, triad 时,像Integer...
这样的可变参数算作单个参数。原因是它实际上等同于传递 List
。 (它实际上是作为数组传递的,所以我不确定作者为什么要谈论List
)。
如果作者推荐的函数不超过 3 个参数,那么作者说 foo(Integer, String, Object...)
可以,而 foo(Integer, String, Long, Object...)
不行。
我正在看书"Clean code"。它有一段关于传递给函数的变量参数以及如何命名函数,args。
Argument Lists Sometimes we want to pass a variable number of arguments into a function. Consider, for example, the String.format method:
String.format("%s worked %.2f hours.", name, hours);
If the variable arguments are all treated identically, as they are in the example above, then they are equivalent to a single argument of type List. By that reasoning, String.format is actually dyadic. Indeed, the declaration of String.format as shown below is clearly dyadic.
public String format(String format, Object... args)
So all the same rules apply. Functions that take variable arguments can be monads, dyads, or even triads. But it would be a mistake to give them more arguments than that.
void monad(Integer... args);
void dyad(String name, Integer... args);
void triad(String name, int count, Integer... args);
monad - 带有一个 arg 的函数,dyad,dyadic - 带有 2 个 args 的函数,triad - 3 个 args。
谁能解释一下这段话?我唯一得到的是这个:
即使您有许多相同类型的参数,您也可以将它们放在一个列表中,例如 - Integer ...args 表示传递了许多 int 参数。 同时,程序员仍应遵循相同的建议 - 编写不超过 3 个参数的函数,通过显示与 Object ...args.
相同类型的参数,二元函数仍然可以有 2 个参数我说的对吗,或者这段话还有别的意思吗?
作者只是想说在统计一个函数是monad, dyad, triad 时,像Integer...
这样的可变参数算作单个参数。原因是它实际上等同于传递 List
。 (它实际上是作为数组传递的,所以我不确定作者为什么要谈论List
)。
如果作者推荐的函数不超过 3 个参数,那么作者说 foo(Integer, String, Object...)
可以,而 foo(Integer, String, Long, Object...)
不行。