We have many Coding styles or code specifications. 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 you want to 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 have to take a look at the document or the Function Definition of InvalidateRect. You will see that the parameter is BOOL bErase, which means "whether to repeat 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? Or something else?
Only after reading the document can we know that "false" indicates "Case Insensitive replacement ".
In fact, if you use enumeration variables/constants instead of bool variables, you will make your code easier to read, such:
Copy codeThe Code is as follows: widget-> repaint (PAINT: immediate );
Widget-> repaint (PAINT: deffer );
InvalidateRect (hwnd, lpRect ,! RepantBackground );
Str. replace ("% USER %", user, Qt: CaseInsensitive); // If Qt 4 does not agree with this, let's look at some other examples, you can guess 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 automatically ". TNND!
This is not the worst case. Let's take a look at the double denial below.Copy codeThe Code is as follows: component. setDisabled (false );
Filter. setCaseInsensitive (false). 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 the same code as fans.
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 ".