作为方法论的布尔是不可接受的吗? [英] Are booleans as method arguments unacceptable?
问题描述
我们的同事声明,布尔值作为方法参数是不可接受的。它们应由枚举代替。起初我没有看到任何好处,但他给了我一个例子。
A colleague of mine states that booleans as method arguments are not acceptable. They shall be replaced by enumerations. At first I did not see any benefit, but he gave me an example.
更容易理解吗?
file.writeData( data, true );
或
enum WriteMode {
Append,
Overwrite
};
file.writeData( data, Append );
现在我明白了! ;-)
这绝对是一个例子,其中枚举作为第二个参数使代码更容易阅读。
Now I got it! ;-)
This is definitely an example where an enumeration as second parameter makes the code much more readable.
主题?
推荐答案
布尔表示是/否选择。如果你想表示一个是/否,然后使用布尔值,它应该是不言自明的。
Boolean's represent "yes/no" choices. If you want to represent a "yes/no", then use a boolean, it should be self-explanatory.
但是如果它是两个选项之间的选择,这显然是yes或no,那么枚举有时可以更易读。
But if it's a choice between two options, neither of which is clearly yes or no, then an enum can sometimes be more readable.
这篇关于作为方法论的布尔是不可接受的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!