是布尔方法参数不能接受? [英] Are booleans as method arguments unacceptable?

查看:119
本文介绍了是布尔方法参数不能接受?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

方法参数的矿山布尔州的一个同事是不能接受的。他们应由枚举所取代。起初,我没有看到任何利益,但他给我举个例子。

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 );

现在我知道了! ;-)

这绝对是在这里枚举作为第二个参数使code更具有可读性的例子。

Now I got it! ;-)
This is definitely an example where an enumeration as second parameter makes the code much more readable.

那么,关于这个主题是你的意见?

So, what's your opinion on this topic?

推荐答案

布尔的再present是/否的选择。如果你想重新present一个是/否,然后用一个布尔值,它应该是不言自明的。

Boolean's represent "yes/no" choices. If you want to represent a "yes/no", then use a boolean, it should be self-explanatory.

但如果它是两种选择,这两者都不显然是肯定的还是否定的,那么一个枚举有时会更具可读性。之间的选择

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆