You should (almost) never overload a method with a boolean flag. There are a variety of reasons:
To avoid confusion, methods should do one thing and one thing only. A boolean flag is a clear indication that a method does more than one thing (from Robert C. Martin).
Reading the code for such a method makes it unclear exactly what is happening. If I am new to the code I see:
MethodA("This is a message", false);
It is not entirely clear what false means without going to look at the code. This fails the principle that the intent of a method should be clear from its name alone.
It is sometimes an unecessary binding to a binary choice when more than one choice might need to be added later. For example: let's say you have a method:
public UpdateCustomer(String name, boolean isPremiumCustomer);
If you later decide to add different categories of customer then you will have to refactor every line of code that calls this method. A better example in your situation is if you wanted to have a method such that you logged only if a debug flag was turned on.
The alternative approaches are roughly as follows:
Create a function with a different name. This is the approach you have taken and probably the one I prefer. In most cases it is enough to have a: doSomething and a doSomethingADifferentWay as in your example. If however you need a second method that takes a flag then you should consider option 2:
Create an enum that describes the options. In your case this would be something like: enum EnableLogging { ENABLE_LOGGING, DISABLE_LOGGING } Then you write your code as:
MethodA("A Message", DISABLE_LOGGING);
Which is at least explicit. (Note: This solution comes from Item 40 in Effective Java 2nd Edition by Joshua Bloch, but applies equally well to other languages).
if(logIt) // log information
// do something
if(logIt) // log other info
// do more
....etc
无论您是否想要记录,您的核心逻辑都是相同的,并且只在一个地方。
This sort of thing has been (and will be) debated endlessly, but the famous answer is "it depends".
For something like whether or not to log (meaning that there shouldn't be any side effects as far as within the application), I would absolutely not create a new method. You will have to duplicate your logic, which means maintaining it in two places in the future, for no real reason. It makes more sense to do something like...
if(logIt) // log information
// do something
if(logIt) // log other info
// do more
....etc
Your core logic is the same whether or not you want to log, and is only in one place.
发布评论
评论(4)
两者都不。您的日志记录配置应该完全独立于您的业务方法。这就是 AOP 的用途。
Neither. Your logging configuration should be completely independent of your business methods. That's what AOP is for.
您(几乎)永远不应该使用布尔标志重载方法。原因有多种:
为了避免混淆,方法应该只做一件事。布尔标志清楚地表明一个方法执行不止一件事(来自 Robert C. Martin).
阅读此类方法的代码会让人不清楚到底发生了什么。如果我是代码新手,我会看到:
MethodA("这是一条消息", false);
如果不看代码,并不完全清楚 false 的含义。这违背了方法的意图应该仅从其名称就可以清楚地看出的原则。
当稍后可能需要添加多个选择时,有时这是对二元选择的不必要的绑定。例如:假设您有一个方法:
public UpdateCustomer(String name, boolean isPremiumCustomer);
如果您稍后决定添加不同类别的客户,那么您将必须重构调用此方法的每一行代码。在您的情况下,一个更好的例子是,如果您想要一种方法,仅在调试标志打开时才进行记录。
替代方法大致如下:
创建一个具有不同名称的函数。这是您所采取的方法,也可能是我更喜欢的方法。在大多数情况下,有一个: doSomething 和 doSomethingADifferentWay 就足够了,如您的示例所示。但是,如果您需要第二种带有标志的方法,那么您应该考虑选项 2:
创建一个描述选项的枚举。在你的情况下,这将是这样的:
enum EnableLogging { ENABLE_LOGGING, DISABLE_LOGGING }
然后将代码编写为:MethodA("A Message", DISABLE_LOGGING);
这至少是明确的。 (注:此解决方案来自 Joshua Bloch 的 Effective Java 2nd Edition 中的 Item 40 ,但同样适用于其他语言)。
You should (almost) never overload a method with a boolean flag. There are a variety of reasons:
To avoid confusion, methods should do one thing and one thing only. A boolean flag is a clear indication that a method does more than one thing (from Robert C. Martin).
Reading the code for such a method makes it unclear exactly what is happening. If I am new to the code I see:
MethodA("This is a message", false);
It is not entirely clear what false means without going to look at the code. This fails the principle that the intent of a method should be clear from its name alone.
It is sometimes an unecessary binding to a binary choice when more than one choice might need to be added later. For example: let's say you have a method:
public UpdateCustomer(String name, boolean isPremiumCustomer);
If you later decide to add different categories of customer then you will have to refactor every line of code that calls this method. A better example in your situation is if you wanted to have a method such that you logged only if a debug flag was turned on.
The alternative approaches are roughly as follows:
Create a function with a different name. This is the approach you have taken and probably the one I prefer. In most cases it is enough to have a: doSomething and a doSomethingADifferentWay as in your example. If however you need a second method that takes a flag then you should consider option 2:
Create an enum that describes the options. In your case this would be something like:
enum EnableLogging { ENABLE_LOGGING, DISABLE_LOGGING }
Then you write your code as:MethodA("A Message", DISABLE_LOGGING);
Which is at least explicit. (Note: This solution comes from Item 40 in Effective Java 2nd Edition by Joshua Bloch, but applies equally well to other languages).
通常,为了避免过度思考,我会进行超载,默认为“false”
,这是我个人的偏好,我确信其他人会不同意。但它并不能涵盖所有情况。
Normally,to avoid overthinking it, I would do an overload, defaulting to 'false'
That's my personal preference, I'm sure others will disagree. It doesn't cover every situation, though.
这类事情已经(并将)无休止地争论,但著名的答案是“视情况而定”。
对于是否记录(意味着应用程序内不应该有任何副作用)之类的问题,我绝对不会创建新方法。你将不得不复制你的逻辑,这意味着将来在两个地方维护它,没有真正的原因。做类似的事情更有意义......
无论您是否想要记录,您的核心逻辑都是相同的,并且只在一个地方。
This sort of thing has been (and will be) debated endlessly, but the famous answer is "it depends".
For something like whether or not to log (meaning that there shouldn't be any side effects as far as within the application), I would absolutely not create a new method. You will have to duplicate your logic, which means maintaining it in two places in the future, for no real reason. It makes more sense to do something like...
Your core logic is the same whether or not you want to log, and is only in one place.