We have a lot of coding Style or code specifications. But this one may often be forgotten by us, that we often use the bool parameter in the parameters of the function, which greatly reduces the readability of the code. Don't believe it? Let's take a look at the following code first.
When you read the following code, what do you think this code means?
Widget->repaint (false); don't repaint? What else does it mean?
After reading the document, we know that this parameter is immediate, that is to say, false means not to redraw immediately, true code immediately repaint.
There is also a function in the Windows API: InvalidateRect, when you see the following code, what do you think it means?
InvalidateRect (hwnd, lprect, FALSE); Let's not say invalidaterect how bad the function name is, let's say that false argument first? Invalidate means "Let xxx invalid", what does false mean? Double negation? Is that for sure?
If you see such a code, you will be quite puzzling. So, you're going to look at the document, or the InvalidateRect function definition, and you'll see that the parameter is BOOL berase, meaning: "Do you want to repaint the background?"
There are a lot of things like this, and looking at the following code, you want to replace the "%user%" in Str with the real username:
Str.replace ("%user%", USER, false); Qt 3TNND, what does that false mean? Do not replace it? What else does it mean?
Read the document to know that false represents: "Case insensitive replacement."
In fact, if you use enumerated variables/constants instead of bool variables, you make your code easier to read, such as:
Copy Code code as follows:
Widget->repaint (paint::immediate);
Widget->repaint (PAINT::d effer);
InvalidateRect (hwnd, lprect,!) Repantbackground);
Str.replace ("%user%", USER, qt::caseinsensitive); Qt 4 If this is not the case, let's take a look at some other examples, and you might as well take a guess at the following code:
Component.setcentered (True, false);
What the heck is this?
Read the document you know, this is originally setcentered (Centered, AutoUpdate);
New Textbox (+, False, true); What is this?
Read the document to know that this is to create a text box, the third parameter is: "Whether to scroll bar", the fourth is: "Whether to wrap automatically." tnnd!
This situation is not the worst, look at the following double negative.
Copy Code code as follows:
Component.setdisabled (FALSE);
Filter.setcaseinsensitive (False) Another, if you read the following code, I believe you will be like me, either petrified, or messy.
Event.initkeyevent ("KeyPress", true, true, NULL, Null,false, False, False, False, 9, 0);
After reading this article, I hope you will never take bool as a function argument again. Unless two reasons:
You're 100% sure you don't have a reading problem, like Java setvisible (BOOL).
You're 100% sure you want to write code that's crazy.
If you want to design a good API, it is highly recommended that you read the Nokia Qt "API design Principles", this article is the "Boolean Trap."