前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >关于面向对象设计中类的方法是否应该使用boolean类型的参数

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

作者头像
Jerry Wang
发布2019-05-31 11:59:55
1.7K0
发布2019-05-31 11:59:55
举报

版权声明:本文为博主汪子熙原创文章,未经博主允许不得转载。 https://cloud.tencent.com/developer/article/1439894

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…

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年01月01日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档