Today's Question:  What does your personal desk look like?        GIVE A SHOUT

Do NOT use boolean variable as function parameters

  Peter        2012-04-22 02:38:45       14,573        1    

We follow lots of coding styles and coding standards when we do programming, but we may frequently forget one. The forgotten one is that do not use boolean parameter as the function parameters. The reason is it would greatly reduce the readability of the code. Don't believe it?

When you read the following code, what does this code mean?

widget->repaint(false);

Do not repaint? Or what does this mean? After looking at the document, we know that this parameter is whether to repaint immediately, i.e, false to not immediately redraw or true to immediately redraw.

The Windows API also has such a function: the InvalidateRect, when you see the following code, do you know what it does?

InvalidateRect(hwnd, lpRect, false);

We will not discuss how bad the function name is here, let's talk about that false parameter. invalidate means make something invalid. What does it mean by putting a false here? Double negative? Certainly meaning? If you see this code, you will be very confused. So you may have to read the document or the function definition of the function InvalidateRect, and you will find that the parameter is a BOOL bErase, yes, it is whether you want to redraw the background or not.

There are many other similar examples. Look at the following code, and want to replace %USER% in str with a real user name:

str.replace ("%USER%", user, false); / / Qt 3

Do not replace it? Or what else? We know the false means non-case-sensitive replacement after reading the document. 

In fact, if you are using enumeration variables or constants rather than a boolean variable, you make your code more readable. For example : 

widget-> repaint (PAINT :: immediate);
widget-> repaint (PAINT :: deffer);
InvalidateRect (hwnd, lpRect,! RepantBackground);
str.replace ("% USER%", user, Qt :: CaseInsensitive); / / Qt 4

Look at some other examples, you may wish to guess following code:

component.setCentered(true, false);

What is this ah? You will never know unless you read the documentation. It turned out to be setCentered(centered, autoUpdate);

new Textbox (300, 100, false, true);

What is this ah? From the document, it is to create a text box, the third parameter is whether you want the scroll bar and the fourth is whether you want to wrap. 

The above was not the worst one yet, look at the following double negative.

component.setDisabled(false);
filter.setCaseInsensitive(false);

One more below, if you read the following code, I believe you will be fossilized like me.

event.initKeyEvent ("keypress", true, true, null, null, false, false, false, false, 9, 0);

After reading this article, I hope you'll never use boolean values as function parameters except in below two cases: 

  • 100% sure it will not lead to reading problems, such as Java setVisible(boolean).
  • 100% sure that you want to write writing everywhere reading nowhere code

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

CODING STYLE  CODING STANDARD  BOOLEAN  FUNCTION PARAMETER  METHOD PARAMETER 

Share on Facebook  Share on Twitter  Share on Weibo  Share on Reddit 

  RELATED


  1 COMMENT


guepardox [Reply]@ 2012-04-23 18:10:24
I understand your point; but some cases requires to use a large set of parameters, instead of a struct in order to be efficient; but for those cases you could use enumerators, or static structs, for each parameter having a descriptive name; i program mostly c# and for framework methods you can always use a descriptive parameter for numeric (using enums) and boolean parameters.