关于面向对象设计中类的方法是否应该使用boolean类型的参数

简介: 关于面向对象设计中类的方法是否应该使用boolean类型的参数

Link: http://programmers.stackexchange.com/questions/147977/is-it-wrong-to-use-a-boolean-parameter-to-determine-behavior


I have seen a practice from time to time that “feels” wrong, but I can’t quite articulate what is wrong about it. Or maybe it’s just my prejudice. Here goes:

A developer defines a method with a boolean as one of its parameters, and that method calls another, and so on, and eventually that boolean is used, solely to determine whether or not to take a certain action. This might be used, for example, to allow the action only if the user has certain rights, or perhaps if we are (or aren’t) in test mode or batch mode or live mode, or perhaps only when the system is in a certain state.

Well there is always another way to do it, whether by querying when it is time to take the action (rather than passing the parameter), or by having multiple versions of the method, or multiple implementations of the class, etc. My question isn’t so much how to improve this, but rather whether or not it really is wrong (as I suspect), and if it is, what is wrong about it.


Yes, this is likely a code smell, which would lead to unmaintainable code that is difficult to understand and that has a lower risk of being easily re-used.

As other posters have noted context is everything (don’t go in heavy-handed if it’s a one off or if the practise has been acknowledged as deliberately incurred technical debt to be re-factored later) but broadly speaking if there is a parameter passed into a function that selects specific behaviour to be executed then further stepwise refinement is required; Breaking up this function in to smaller functions will produce more highly cohesive ones.


So what is a highly cohesive function?

It’s a function that does one thing and one thing only.

The problem with a parameter passed in as you describe is that the function is doing more than two things; it may or may not check the users access rights depending on the state of the boolean parameter, then depending on that decision tree it will carry out a piece of functionality.

It would be better to separate the concerns of Access Control from the concerns of Task, Action or Command.

As you have already noted the intertwining of theses concerns seems off.

So the notion of Cohesiveness helps us identify that the function in question is not highly cohesive and that we could refactor the code to produce a set of more cohesive functions.

So the question could be restated; Given that we all agree passing behavioural selection parameters is best avoided how do we improve matters?


I stopped using this pattern a long time ago, for a very simple reason; maintenance cost. Several times I found that I had some function say frobnicate(something, forwards_flag) which was called many times in my code, and needed to locate all the places in the code where the value false was passed as the value of forwards_flag. You can’t easily search for those, so this becomes a maintenance headache. And if you need to make a bugfix at each of those sites, you may have an unfortunate problem if you miss one.

But this specific problem is easily fixed without fundamentally changing the approach:

enum FrobnicationDirection {

FrobnicateForwards,

FrobnicateBackwards;

};


void frobnicate(Object what, FrobnicationDirection direction);


With this code, one would only need to search for instances of FrobnicateBackwards. While it’s possible there is some code which assigns this to a variable so you have to follow a few threads of control, I find in practice that this is rare enough that this alternative works OK.


However, there is another problem with passing the flag in this way, at least in principle. This is that some (only some) systems having this design may be exposing too much knowledge about the implementation details of the deeply-nested parts of the code (which uses the flag) to the outer layers (which need to know which value to pass in this flag).


To use Larry Constantine’s terminology, this design may have over-strong coupling between the setter and the user of the boolean flag. Franky though it’s hard to say with any degree of certainty on this question without knowing more about the codebase.


To address the specific examples you give, I would have some degree of concern in each but mainly for reasons of risk/correctness. That is, if your system needs to pass around flags which indicate what state the system is in, you may find that you’ve got code which should have taken account of this but doesn’t check the parameter (because it was not passed to this function). So you have a bug because someone omitted to pass the parameter.

It’s also worth admitting that a system-state indicator that needs to be passed to almost every function is in fact essentially a global variable. Many of the downsides of a global variable will apply. I think in many cases it is better practice to encapsulate the knowledge of the system state (or the user’s credentials, or the system’s identity) within an object which is responsible for acting correctly on the basis of that data. Then you pass around a reference to that object as opposed to the raw data. The key concept here is encapsulation.


public void foo(boolean flag) {

doThis();

if (flag)

doThat();

}


This is really a problem because it’s a case of bad cohesion. You’re creating a dependency between methods that is not really necessary.


What you should be doing in this case is leaving doThis and doThat as separate and public methods then doing:

doThis();

doThat();

or

doThis();

That way you leave the correct decision to the caller (exactly as if you were passing a boolean parameter) without create coupling.

Of course not all boolean parameters are used in such bad way but it’s definitely a code smell and you’re right to get suspicious if you see that a lot in the source code.

This is just one example of how to solve this problem based on the examples I wrote. There are other cases where a different approach will be necessary.


There are lots of situations where if (flag) doThat() inside foo() is legitimate. Pushing the decision about invoking doThat() to every caller forces repetition that will have to be removed if you later find out for some methods, the flag behavior also needs to call doTheOther(). I’d much rather put dependencies between methods in the same class than have to go scour all of the callers later.


First off: programming is not a science so much as it is an art. So there is very rarely a “wrong” and a “right” way to program. Most coding-standards are merely “preferences” that some programmers consider useful; but ultimately they are rather arbitrary. So I would never label a choice of parameter to be “wrong” in and of itself – and certainly not something as generic and useful as a boolean parameter. The use of a boolean (or an int, for that matter) to encapsulate state is perfectly justifiable in many cases.

Coding decisions, by & large, should be based primarily on performance and maintainability. If performance isn’t at stake (and I can’t imagine how it ever could be in your examples), then your next consideration should be: how easy will this be for me (or a future redactor) to maintain? Is it intuitive and understandable? Is it isolated? Your example of chained function calls does in fact seem potentially brittle in this respect: if you decide to change your bIsUp to bIsDown, how many other places in the code will need to be changed too? Also, is your paramater list ballooning? If your function has 17 parameters, then readability is an issue, and you need to reconsider whether you are appreciating the benefits of object-oriented architecture.


Context is important. Such methods are pretty common in iOS. As just one often-used example, UINavigationController provides the method -pushViewController:animated:, and the animatedparameter is a BOOL. The method performs essentially the same function either way, but it animates the transition from one view controller to another if you pass in YES, and doesn’t if you pass NO. This seems entirely reasonable; it’d be silly to have to provide two methods in place of this one just so you could determine whether or not to use animation.

It may be easier to justify this sort of thing in Objective-C, where the method-naming syntax provides more context for each parameter than you get in languages like C and Java. Nevertheless, I’d think that a method that takes a single parameter could easily take a boolean and still make sense:

file.saveWithEncryption(false); // is there any doubt about the meaning of ‘false’ here?


n Actually I have no clue what false means in the file.saveWithEncryption example. Does it mean that it will save without encryption? If so, why in the world would the method have “with encryption” right in the name? I could understand have a method like save(boolean withEncryption), but when I seefile.save(false), it is not at all obvious at a glance that the parameter indicates that it would be with or without encryption. I think, in fact, this makes James Youngman’s first point, about using an enum. – RayMay 9 '12 at 23:20

n An alternative hypothesis is that false means, don’t overwirte any existing file of the same name. Contrived example I know, but to be sure you’d need to check the documentation (or code) for the function.– James Youngman May 9 '12 at 23:34


No problem in ABAP and Scala…


image.pngimage.png

相关文章
|
1月前
|
SQL 流计算 OceanBase
OceanBase CDC从热OB库采集过来的Tinyint(1)类型会默认转换成Boolean,请教一下,如果想转换成int类型,有什方法么?
【2月更文挑战第25天】OceanBase CDC从热OB库采集过来的Tinyint(1)类型会默认转换成Boolean,请教一下,如果想转换成int类型,有什方法么?
27 3
|
6月前
|
前端开发 JavaScript
前端基础 - JavaScript值Boolean类型的默认转换
前端基础 - JavaScript值Boolean类型的默认转换
30 0
|
21天前
|
IDE Java 开发工具
阿里巴巴不建议 boolean 类型变量用 isXXX
阿里巴巴不建议 boolean 类型变量用 isXXX
11 1
|
25天前
|
算法 测试技术 编译器
【C++ 基本类型 bool 】深入探索C++中的布尔类型Boolean(二 )
【C++ 基本类型 bool 】深入探索C++中的布尔类型Boolean
25 0
|
25天前
|
程序员 编译器 C语言
【C++ 基本类型 bool 】深入探索C++中的布尔类型Boolean(一)
【C++ 基本类型 bool 】深入探索C++中的布尔类型Boolean
37 0
|
4月前
|
IDE Java 开发工具
阿里巴巴不建议 boolean 类型变量用isXXX的理由?
平时工作中大家经常使用到boolean以及Boolean类型的数据,前者是基本数据类型,后者是包装类,为什么不推荐使用isXXX来命名呢?到底是用基本类型的数据好呢还是用包装类好呢?
|
8月前
|
JavaScript
TypeScript 与 JS 中类型首字母大小写区别(String、string、Number、number、Boolean、boolean ...)
TypeScript 与 JS 中类型首字母大小写区别(String、string、Number、number、Boolean、boolean ...)
166 0
|
JavaScript
js:常见对象的类型判断typeof和布尔值Boolean
js:常见对象的类型判断typeof和布尔值Boolean
82 0
|
存储 Java 容器
Boolean包装类型
博主今天在工作中发现了Boolean包装类型的使用,由此整理了相关内容,下面我们一起来看下吧。
121 0
Boolean包装类型
|
前端开发
swagger中响应参数为Boolean或是integer如何设置响应描述信息
项目使用swagger进行文档信息展示,现在有一个招聘者企业用户信息校验接口,响应参数只会返回true或是false,如何在接口文档中响应参数栏目中添加对返回字段的描述信息(true:校验成功;false:校验失败)?
swagger中响应参数为Boolean或是integer如何设置响应描述信息

热门文章

最新文章