Bool is misinterpreted as a function parameter

  • 2020-04-01 01:09:15
  • OfStack

We have a lot of Coding styles or code specifications. However, it is often forgotten that we often use bool arguments in the arguments of functions, which greatly reduces the readability of the code. Don't believe it? Let's look at the following code first.
When you read the following code, what do you think it means?
Widgets - > Repaint (false); No repaint? Or something else?
After reading the documentation, we realize that the parameter is immediate, that is, false means not redrawn immediately, and the true code is redrawn immediately.
Windows API also has such a function: InvalidateRect, when you see the following code, what do you think it means?
The InvalidateRect (HWND, lpRect, false); Instead of saying how bad the function name is, what about the false parameter? "Invalidate" means "to invalidate". What does "false" mean? Double negation? Is it positive?
If you see code like this, it's pretty confusing. So, if you look at the document, or the function definition for InvalidateRect, you'll see that the argument is BOOL bErase, which means, "do I need to redraw the background?"
There are a lot of things like this. Take a look at the following code to replace "%USER%" in STR with a real USER name:
STR. Replace (" % USER % ", the USER, false); // Qt 3TNND, what does that false mean? No substitution? Or something else?
Only after reading the documentation did I realize that false stands for "case insensitive substitution."
In fact, if you use enumeration variables/constants instead of bool variables, you will make your code more readable, such as:
 
widget->repaint(PAINT::immediate); 
widget->repaint(PAINT::deffer); 
InvalidateRect(hwnd, lpRect, !RepantBackground); 
str.replace("%USER%", user, Qt::CaseInsensitive); //If Qt 4 doesn't take this seriously, let's take a look at some other examples and take a guess at the following code:
component.setCentered(true, false); 

What the hell is this?
You read the document and it turns out that this is setCentered(centered, autoUpdate);
New Textbox(300, 100, false, true); What is this?
After looking at the document, you see that you are creating a text box with a third argument: "do you want to scroll?" and a fourth argument: "do you want to wrap?" TNND!
This is not the worst case. Look at the double negative below.
 
component.setDisabled(false); 
filter.setCaseInsensitive(false) One more, if you read the code below, you'll 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 never use bool arguments as function arguments again. Except for two reasons:
You are 100% sure that there will be no reading issues, such as setVisible (bool) in Java.
You're 100% sure that you want to write code like this.
If you want to Design a good API, it is highly recommended that you read Nokia's Qt API Design Principles, and this article is the "Boolean Trap" of it.

Related articles: