Never use bool as a function parameter

Source: Internet
Author: User

We have many coding styles or code specifications. But this one may often be forgotten by us.The bool parameter is often used 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 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:

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 automatically ". Tnnd!

This is not the worst case. Let's take a look at the double denial 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, false, false, false, 9, 0); 

After reading this article, I hope you willDo not use bool as a function parameter.. Unless for two reasons:

  • You are sure 100% won't bring about reading problems, such as Java'ssetVisible (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 ".

Link: http://coolshell.cn/articles/5444.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.