This is an article from cool shell.ArticleThe original Article is here.
We have many coding styles orCodeSpecification. But this one may often be forgotten, that is, we often use bool parameters in function parameters, which greatly reduces the readability of the Code. Believe it? Let's take a look at the following code.
When you read the following code, what does it mean?
Widget-> repaint (false );
Do not repaint? Or something else? After reading the document, we know that this parameter is immediate. That is to say, false indicates that the image is not re-painted immediately, and the true code re-painted immediately.
There is also a function in Windows API: invalidaterect. What do you mean when you see the following code?
Invalidaterect (hwnd, lprect, false );
Let's not talk about how bad the invalidaterect function name is. Let's talk about the false parameter first? Invalidate indicates "invalid XXX". What does false mean? Double negation? Is it true? If you see such code, you will be quite confused. So you need to see the document or the Function Definition of invalidaterect. You will see that the parameter isBoolBeraseIndicates whether to reproduce the background.
There are many such things. Let's look at the following code and try to replace "% USER %" in STR with the real User Name:
Str. Replace ("% USER %", user, false); // QT 3
Tnnd, what does false mean? Don't you replace it? What else does it mean? I only know it after reading the document. If it is false, the code is case-insensitive.
In fact, if you use enumeration variables/constants instead of bool variables, you will make your code easier to read, such:
Widget-> repaint (paint: immediate); widget-> repaint (paint: deffer); invalidaterect (hwnd, lprect ,! Repantbackground); Str. Replace ("% USER %", user, QT: caseinsensitive); // QT 4
If you disagree with this, let's take a look at some other examples. You may wish to take a look at the following code:
Component. setcentered (true, false );
What is this? After reading the documentation, you will know that this was originally setcentered (centered, AutoUpdate );
New Textbox (300,100, false, true );
What is this? After reading the document, you can see that this is to create a text box, the third parameter is whether to scroll the bar, and the fourth parameter is whether to wrap the line automatically. Tnnd.
The above situation is not the worst. Let's take a look at the double negation below.
Component. setdisabled (false); filter. setcaseinsensitive (false)
Again, if you read the following code, I believe you will be the same as me, either petrochemical or messy.
Event. initkeyevent ("keypress", true, true, null, null, false, 9, 0 );
After reading this article, I hope you will never take bool as a function parameter any more. Unless for two reasons:
- You are sure 100% won't bring about reading problems, such as Java's setvisible (bool ).
- You are 100% sure you want to write code that is difficult to maintain and read.
[Update 2011/9/8] Of course, other parameters also have the same problem, for example:New Textbox (300,100, false, true );In 300 and 100, I do not know whether it is the coordinate or the length and width. However, parameters such as the general length or coordinates will not be hard code, and there will be variable names, the bool parameter is often passed to true and false. The bool parameter is more obvious.
So,ProgramDo not show magic number. True/false is also a magic number. However, I would like to tell you that from the API design perspective, you cannot force callers to replace true/false with constants, and defining them as enumeration types is the best choice.
Finally, if you want to design a good API, we strongly recommend that you read the API design principles of Nokia QT. This article is "Boolean trap ".
(Full text)