Are booleans as method arguments unacceptable? [cl

2019-01-16 02:48发布

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.

What's easier to understand?

file.writeData( data, true );

Or

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.

So, what's your opinion on this topic?

26条回答
Luminary・发光体
2楼-- · 2019-01-16 03:38

Remember the question Adlai Stevenson posed to ambassador Zorin at the U.N. during the cuban missile crisis?

"You are in the courtroom of world opinion right now, and you can answer yes or no. You have denied that [the missiles] exist, and I want to know whether I have understood you correctly.... I am prepared to wait for my answer until hell freezes over, if that's your decision."

If the flag you have in your method is of such a nature that you can pin it down to a binary decision, and that decision will never turn into a three-way or n-way decision, go for boolean. Indications: your flag is called isXXX.

Don't make it boolean in case of something that is a mode switch. There is always one more mode than you thought of when writing the method in the first place.

The one-more-mode dilemma has e.g. haunted Unix, where the possible permission modes a file or directory can have today result in weird double meanings of modes depending on file type, ownership etc.

查看更多
做自己的国王
3楼-- · 2019-01-16 03:38

Enums have a definite benefit, but you should't just go replacing all your booleans with enums. There are many places where true/false is actually the best way to represent what is going on.

However, using them as method arguments is a bit suspect, simply because you can't see without digging into things what they are supposed to do, as they let you see what the true/false actually means

Properties (especially with C#3 object initializers) or keyword arguments (a la ruby or python) are a much better way to go where you'd otherwise use a boolean argument.

C# example:

var worker = new BackgroundWorker { WorkerReportsProgress = true };

Ruby example

validates_presence_of :name, :allow_nil => true

Python example

connect_to_database( persistent=true )

The only thing I can think of where a boolean method argument is the right thing to do is in java, where you don't have either properties or keyword arguments. This is one of the reasons I hate java :-(

查看更多
Fickle 薄情
4楼-- · 2019-01-16 03:39

Use the one that best models your problem. In the example you give, the enum is a better choice. However, there would be other times when a boolean is better. Which makes more sense to you:

lock.setIsLocked(True);

or

enum LockState { Locked, Unlocked };
lock.setLockState(Locked);

In this case, I might choose the boolean option since I think it's quite clear and unambiguous, and I'm pretty sure my lock is not going to have more than two states. Still, the second choice is valid, but unnecessarily complicated, IMHO.

查看更多
欢心
5楼-- · 2019-01-16 03:39

Booleans values true/false only. So it is not clear what it represent. Enum can have meaningful name, e.g OVERWRITE, APPEND, etc. So enums are better.

查看更多
手持菜刀,她持情操
6楼-- · 2019-01-16 03:40

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.

查看更多
一纸荒年 Trace。
7楼-- · 2019-01-16 03:40

Enums also allow for future modifications, where you now want a third choice (or more).

查看更多
登录 后发表回答