We have many Coding Styles or coding specifications. But this item may often be forgotten by us, that is, we often use bool parameters in function parameters, which will greatly reduce the readability of the code. Don’t believe it? Let's first look at the code below.
When you read the following code, what do you think this code means?
widget->repaint(false); Do you not want to repaint? Or does it mean something else?
After reading the document, we learned that this parameter is immediate, that is to say, false means not to redraw immediately, and true means the code will redraw immediately.
There is also such 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 talk about how bad the function name InvalidateRect is. Let’s talk about the false parameter first? invalidate means "invalidate XXX", what does false mean? Double negative? Does it mean affirmatively?
If you see code like this, it will be quite confusing. So, you have to look at the documentation or the function definition of InvalidateRect. You will see that the parameter is BOOL bErase, which means: "Whether you want to redraw the background."
There are many such things. Looking at the following code, I want to replace "%USER%" in str with the real user name:
str.replace("%USER%", user, false); // Qt 3TNND, what does false mean? Not replacing it? Or does it mean something else?
I found out after reading the documentation that false means: "case-insensitive replacement".
Actually, if you use enum variables/constants instead of bool variables, you will make your code more readable, like: