Correct comment to put for boolean function arguments that are “false”?

From some open source projects, I gathered the following coding style

void someFunction(bool forget);

void ourFunction() {
  someFunction(false /* forget */);
}    

I always have doubt about what false means here. Does it mean “forget”, or does the “forget” refer to its corresponding parameter (like in the case above), and “false” is meant to negate it?

What style is used most often and what is the best way (or some of the better ways) to avoid the ambiguity?

8

In the sample code you posted, it looks like forget is a flag argument. (I cannot be certain because the function is purely hypothetical.)

Flag arguments are a code smell. They indicate that a function does more than one thing, and a good function should do only one thing.

To avoid the flag argument, break up the function into two functions that explain the difference in the function name.

Flag argument

serveIceCream(bool lowFat)

No flag argument

serveTraditionalIceCream()
serveLowFatIceCream()

edit: Ideally, you don’t need to keep around a function with the flag parameter at all. There are cases along the lines of what Fowler calls a tangled implementation where completely separating the functions creates duplicated code. However, the higher the cyclomatic complexity of the parameterized function, the stronger is the argument for getting rid of it.


This is only a hunch, but a parameter named forget sounds like feature envy. Why would a caller tell another object to forget something? There may be a bigger design issue.

3

In layman’s words:

  • false is a literal.
  • you are passing a literal false
  • you are telling someFunction to not forget
  • you are telling someFunction that the parameter forget is false
  • you are telling someFunction to remember

In my opinion it would be better if the function was like this:

void someFunction(bool remember);

the you can call it

void ourFunction() {
  someFunction(true);
} 

or keep the old name but change your wrapper function to

void ourFunctionWithRemember() {
  someFunction(false);
} 

EDIT:

As @Vorac stated, always strive to use positive words. Double negation is confusing.

3

The parameter may be well-named; it’s hard to tell without knowing the name of the function. I assume that the comment was written by the original author of the function, and it was a reminder of what passing false into someFunction means, but for anyone coming along afterward, it’s a little unclear at first glance.

Using a positive variable name (suggested in Code Complete) may be the simplest change that would make this snippet easier to read, e.g.

void someFunction(boolean remember);

then ourFunction becomes:

void ourFunction() {
    someFunction(true /* remember */);
}

However, using an enum makes the function call even easier to understand, at the expense of some supporting code:

public enum RememberFoo {
    REMEMBER,
    FORGET
}

...

void someFunction(RememberFoo remember);

...

void ourFunction() {
    someFunction(RememberFoo.REMEMBER);
}

If you’re unable to change the signature of someFunction for whatever reason, using a temporary variable makes the code easier to read as well, sort of like simplifying conditionals by introducing a variable for no other reason than to make the code easier to parse by humans.

void someFunction(boolean remember);

...

void ourFunction() {
    boolean remember = false;
    someFunction(remember);
}

2

Rename the variable so a bool value makes sense.

This a million times better than adding a comment to explain arguments to a function because the name is ambiguous.

2

Create a local boolean with a more descriptive name, and assign the value to it. That way the meaning will be more clear.

void ourFunction() {
    bool takeAction = false;  /* false means to forget */
    someFunction( takeAction );
}    

If you can’t rename the variable, then the comment should be a little more expressive:

void ourFunction() {
    /* false means that the method should forget what was requested */
    someFunction( false );
}    

2

There is a good article that mentions this exact situation as it refers to Qt-Style APIs. There, it’s called The Boolean Parameter Trap and it’s worth a read.

The gist of it is:

  1. Overloading the function so that the bool isn’t needed is better
  2. Using an enum (as Esailija suggests) is best

This is a bizarre comment.

From the compiler’s perspective, someFunction(false /* forget */); is actually someFunction(false); (the comment is stripped). So, all that line does is call someFunction with the first (and only) argument set to false.

/* forget */ is just the name of the parameter. It’s probably nothing more than a quick (and dirty) reminder, that doesn’t really need to be there. Just use a less ambiguous parameter name, and you won’t need the comment at all.

One of advices of Clean code is to minimize number of unnecessary comments1 (because they tend to rot), and to name functions and methods properly.

Following that, I would just remove the comment. After all, modern IDEs (like eclipse) pop a box with code when you put a mouse over the function. Seeing the code should clear the ambiguity.


1 Commenting some complex code is ok.

2

To belabor the obvious, comments can lie. Thus is always better to make your code self-documenting without resorting to comments to explain, because some person (perhaps you) will change true to false and not update the comment.

If you can’t change the API, then I resort to 2 options

  • Change the comment so that it always true, regardless of the code. If you only call this once, this is a good solution, since it keeps the documention local.
     someFunction( false /* true=forget, false=remember */);`
  • Use #defines, especially if you call it more than once.
     #define FORGET   true
     #define REMEMBER false
     someFunction(REMEMBER);

I like the answer about making the comment always true, but while good I think it misses the fundamental problem with this code — it is being called with a literal.

You should avoid using literals when calling methods. Local variables, optional parameters, named parameters, enums — how you best avoid them will depend upon the language and whats available, but do try to avoid them. Literals have values, but they don’t have meaning.

In C#, I used named parameters to make this clearer

someFunction(forget: false);

Or enum:

enum Memory { Remember, Forget };

someFunction(Memory.Forget);

Or overloads:

someFunctionForget();

Or polymorphism`:

var foo = new Elephantine();

foo.someFunction();

The naming should always resolve ambiguity for booleans. I always name boolean something like ‘isThis’ or ‘shouldDoThat’, for example:

void printTree(Tree tree, bool shouldPrintPretty){ ... }

and so on. But when you are referencing someone else’s code, its best to just leave comment when passing values.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Correct comment to put for boolean function arguments that are “false”?

From some open source projects, I gathered the following coding style

void someFunction(bool forget);

void ourFunction() {
  someFunction(false /* forget */);
}    

I always have doubt about what false means here. Does it mean “forget”, or does the “forget” refer to its corresponding parameter (like in the case above), and “false” is meant to negate it?

What style is used most often and what is the best way (or some of the better ways) to avoid the ambiguity?

8

In the sample code you posted, it looks like forget is a flag argument. (I cannot be certain because the function is purely hypothetical.)

Flag arguments are a code smell. They indicate that a function does more than one thing, and a good function should do only one thing.

To avoid the flag argument, break up the function into two functions that explain the difference in the function name.

Flag argument

serveIceCream(bool lowFat)

No flag argument

serveTraditionalIceCream()
serveLowFatIceCream()

edit: Ideally, you don’t need to keep around a function with the flag parameter at all. There are cases along the lines of what Fowler calls a tangled implementation where completely separating the functions creates duplicated code. However, the higher the cyclomatic complexity of the parameterized function, the stronger is the argument for getting rid of it.


This is only a hunch, but a parameter named forget sounds like feature envy. Why would a caller tell another object to forget something? There may be a bigger design issue.

3

In layman’s words:

  • false is a literal.
  • you are passing a literal false
  • you are telling someFunction to not forget
  • you are telling someFunction that the parameter forget is false
  • you are telling someFunction to remember

In my opinion it would be better if the function was like this:

void someFunction(bool remember);

the you can call it

void ourFunction() {
  someFunction(true);
} 

or keep the old name but change your wrapper function to

void ourFunctionWithRemember() {
  someFunction(false);
} 

EDIT:

As @Vorac stated, always strive to use positive words. Double negation is confusing.

3

The parameter may be well-named; it’s hard to tell without knowing the name of the function. I assume that the comment was written by the original author of the function, and it was a reminder of what passing false into someFunction means, but for anyone coming along afterward, it’s a little unclear at first glance.

Using a positive variable name (suggested in Code Complete) may be the simplest change that would make this snippet easier to read, e.g.

void someFunction(boolean remember);

then ourFunction becomes:

void ourFunction() {
    someFunction(true /* remember */);
}

However, using an enum makes the function call even easier to understand, at the expense of some supporting code:

public enum RememberFoo {
    REMEMBER,
    FORGET
}

...

void someFunction(RememberFoo remember);

...

void ourFunction() {
    someFunction(RememberFoo.REMEMBER);
}

If you’re unable to change the signature of someFunction for whatever reason, using a temporary variable makes the code easier to read as well, sort of like simplifying conditionals by introducing a variable for no other reason than to make the code easier to parse by humans.

void someFunction(boolean remember);

...

void ourFunction() {
    boolean remember = false;
    someFunction(remember);
}

2

Rename the variable so a bool value makes sense.

This a million times better than adding a comment to explain arguments to a function because the name is ambiguous.

2

Create a local boolean with a more descriptive name, and assign the value to it. That way the meaning will be more clear.

void ourFunction() {
    bool takeAction = false;  /* false means to forget */
    someFunction( takeAction );
}    

If you can’t rename the variable, then the comment should be a little more expressive:

void ourFunction() {
    /* false means that the method should forget what was requested */
    someFunction( false );
}    

2

There is a good article that mentions this exact situation as it refers to Qt-Style APIs. There, it’s called The Boolean Parameter Trap and it’s worth a read.

The gist of it is:

  1. Overloading the function so that the bool isn’t needed is better
  2. Using an enum (as Esailija suggests) is best

This is a bizarre comment.

From the compiler’s perspective, someFunction(false /* forget */); is actually someFunction(false); (the comment is stripped). So, all that line does is call someFunction with the first (and only) argument set to false.

/* forget */ is just the name of the parameter. It’s probably nothing more than a quick (and dirty) reminder, that doesn’t really need to be there. Just use a less ambiguous parameter name, and you won’t need the comment at all.

One of advices of Clean code is to minimize number of unnecessary comments1 (because they tend to rot), and to name functions and methods properly.

Following that, I would just remove the comment. After all, modern IDEs (like eclipse) pop a box with code when you put a mouse over the function. Seeing the code should clear the ambiguity.


1 Commenting some complex code is ok.

2

To belabor the obvious, comments can lie. Thus is always better to make your code self-documenting without resorting to comments to explain, because some person (perhaps you) will change true to false and not update the comment.

If you can’t change the API, then I resort to 2 options

  • Change the comment so that it always true, regardless of the code. If you only call this once, this is a good solution, since it keeps the documention local.
     someFunction( false /* true=forget, false=remember */);`
  • Use #defines, especially if you call it more than once.
     #define FORGET   true
     #define REMEMBER false
     someFunction(REMEMBER);

I like the answer about making the comment always true, but while good I think it misses the fundamental problem with this code — it is being called with a literal.

You should avoid using literals when calling methods. Local variables, optional parameters, named parameters, enums — how you best avoid them will depend upon the language and whats available, but do try to avoid them. Literals have values, but they don’t have meaning.

In C#, I used named parameters to make this clearer

someFunction(forget: false);

Or enum:

enum Memory { Remember, Forget };

someFunction(Memory.Forget);

Or overloads:

someFunctionForget();

Or polymorphism`:

var foo = new Elephantine();

foo.someFunction();

The naming should always resolve ambiguity for booleans. I always name boolean something like ‘isThis’ or ‘shouldDoThat’, for example:

void printTree(Tree tree, bool shouldPrintPretty){ ... }

and so on. But when you are referencing someone else’s code, its best to just leave comment when passing values.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Correct comment to put for boolean function arguments that are “false”?

From some open source projects, I gathered the following coding style

void someFunction(bool forget);

void ourFunction() {
  someFunction(false /* forget */);
}    

I always have doubt about what false means here. Does it mean “forget”, or does the “forget” refer to its corresponding parameter (like in the case above), and “false” is meant to negate it?

What style is used most often and what is the best way (or some of the better ways) to avoid the ambiguity?

8

In the sample code you posted, it looks like forget is a flag argument. (I cannot be certain because the function is purely hypothetical.)

Flag arguments are a code smell. They indicate that a function does more than one thing, and a good function should do only one thing.

To avoid the flag argument, break up the function into two functions that explain the difference in the function name.

Flag argument

serveIceCream(bool lowFat)

No flag argument

serveTraditionalIceCream()
serveLowFatIceCream()

edit: Ideally, you don’t need to keep around a function with the flag parameter at all. There are cases along the lines of what Fowler calls a tangled implementation where completely separating the functions creates duplicated code. However, the higher the cyclomatic complexity of the parameterized function, the stronger is the argument for getting rid of it.


This is only a hunch, but a parameter named forget sounds like feature envy. Why would a caller tell another object to forget something? There may be a bigger design issue.

3

In layman’s words:

  • false is a literal.
  • you are passing a literal false
  • you are telling someFunction to not forget
  • you are telling someFunction that the parameter forget is false
  • you are telling someFunction to remember

In my opinion it would be better if the function was like this:

void someFunction(bool remember);

the you can call it

void ourFunction() {
  someFunction(true);
} 

or keep the old name but change your wrapper function to

void ourFunctionWithRemember() {
  someFunction(false);
} 

EDIT:

As @Vorac stated, always strive to use positive words. Double negation is confusing.

3

The parameter may be well-named; it’s hard to tell without knowing the name of the function. I assume that the comment was written by the original author of the function, and it was a reminder of what passing false into someFunction means, but for anyone coming along afterward, it’s a little unclear at first glance.

Using a positive variable name (suggested in Code Complete) may be the simplest change that would make this snippet easier to read, e.g.

void someFunction(boolean remember);

then ourFunction becomes:

void ourFunction() {
    someFunction(true /* remember */);
}

However, using an enum makes the function call even easier to understand, at the expense of some supporting code:

public enum RememberFoo {
    REMEMBER,
    FORGET
}

...

void someFunction(RememberFoo remember);

...

void ourFunction() {
    someFunction(RememberFoo.REMEMBER);
}

If you’re unable to change the signature of someFunction for whatever reason, using a temporary variable makes the code easier to read as well, sort of like simplifying conditionals by introducing a variable for no other reason than to make the code easier to parse by humans.

void someFunction(boolean remember);

...

void ourFunction() {
    boolean remember = false;
    someFunction(remember);
}

2

Rename the variable so a bool value makes sense.

This a million times better than adding a comment to explain arguments to a function because the name is ambiguous.

2

Create a local boolean with a more descriptive name, and assign the value to it. That way the meaning will be more clear.

void ourFunction() {
    bool takeAction = false;  /* false means to forget */
    someFunction( takeAction );
}    

If you can’t rename the variable, then the comment should be a little more expressive:

void ourFunction() {
    /* false means that the method should forget what was requested */
    someFunction( false );
}    

2

There is a good article that mentions this exact situation as it refers to Qt-Style APIs. There, it’s called The Boolean Parameter Trap and it’s worth a read.

The gist of it is:

  1. Overloading the function so that the bool isn’t needed is better
  2. Using an enum (as Esailija suggests) is best

This is a bizarre comment.

From the compiler’s perspective, someFunction(false /* forget */); is actually someFunction(false); (the comment is stripped). So, all that line does is call someFunction with the first (and only) argument set to false.

/* forget */ is just the name of the parameter. It’s probably nothing more than a quick (and dirty) reminder, that doesn’t really need to be there. Just use a less ambiguous parameter name, and you won’t need the comment at all.

One of advices of Clean code is to minimize number of unnecessary comments1 (because they tend to rot), and to name functions and methods properly.

Following that, I would just remove the comment. After all, modern IDEs (like eclipse) pop a box with code when you put a mouse over the function. Seeing the code should clear the ambiguity.


1 Commenting some complex code is ok.

2

To belabor the obvious, comments can lie. Thus is always better to make your code self-documenting without resorting to comments to explain, because some person (perhaps you) will change true to false and not update the comment.

If you can’t change the API, then I resort to 2 options

  • Change the comment so that it always true, regardless of the code. If you only call this once, this is a good solution, since it keeps the documention local.
     someFunction( false /* true=forget, false=remember */);`
  • Use #defines, especially if you call it more than once.
     #define FORGET   true
     #define REMEMBER false
     someFunction(REMEMBER);

I like the answer about making the comment always true, but while good I think it misses the fundamental problem with this code — it is being called with a literal.

You should avoid using literals when calling methods. Local variables, optional parameters, named parameters, enums — how you best avoid them will depend upon the language and whats available, but do try to avoid them. Literals have values, but they don’t have meaning.

In C#, I used named parameters to make this clearer

someFunction(forget: false);

Or enum:

enum Memory { Remember, Forget };

someFunction(Memory.Forget);

Or overloads:

someFunctionForget();

Or polymorphism`:

var foo = new Elephantine();

foo.someFunction();

The naming should always resolve ambiguity for booleans. I always name boolean something like ‘isThis’ or ‘shouldDoThat’, for example:

void printTree(Tree tree, bool shouldPrintPretty){ ... }

and so on. But when you are referencing someone else’s code, its best to just leave comment when passing values.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Correct comment to put for boolean function arguments that are “false”?

From some open source projects, I gathered the following coding style

void someFunction(bool forget);

void ourFunction() {
  someFunction(false /* forget */);
}    

I always have doubt about what false means here. Does it mean “forget”, or does the “forget” refer to its corresponding parameter (like in the case above), and “false” is meant to negate it?

What style is used most often and what is the best way (or some of the better ways) to avoid the ambiguity?

8

In the sample code you posted, it looks like forget is a flag argument. (I cannot be certain because the function is purely hypothetical.)

Flag arguments are a code smell. They indicate that a function does more than one thing, and a good function should do only one thing.

To avoid the flag argument, break up the function into two functions that explain the difference in the function name.

Flag argument

serveIceCream(bool lowFat)

No flag argument

serveTraditionalIceCream()
serveLowFatIceCream()

edit: Ideally, you don’t need to keep around a function with the flag parameter at all. There are cases along the lines of what Fowler calls a tangled implementation where completely separating the functions creates duplicated code. However, the higher the cyclomatic complexity of the parameterized function, the stronger is the argument for getting rid of it.


This is only a hunch, but a parameter named forget sounds like feature envy. Why would a caller tell another object to forget something? There may be a bigger design issue.

3

In layman’s words:

  • false is a literal.
  • you are passing a literal false
  • you are telling someFunction to not forget
  • you are telling someFunction that the parameter forget is false
  • you are telling someFunction to remember

In my opinion it would be better if the function was like this:

void someFunction(bool remember);

the you can call it

void ourFunction() {
  someFunction(true);
} 

or keep the old name but change your wrapper function to

void ourFunctionWithRemember() {
  someFunction(false);
} 

EDIT:

As @Vorac stated, always strive to use positive words. Double negation is confusing.

3

The parameter may be well-named; it’s hard to tell without knowing the name of the function. I assume that the comment was written by the original author of the function, and it was a reminder of what passing false into someFunction means, but for anyone coming along afterward, it’s a little unclear at first glance.

Using a positive variable name (suggested in Code Complete) may be the simplest change that would make this snippet easier to read, e.g.

void someFunction(boolean remember);

then ourFunction becomes:

void ourFunction() {
    someFunction(true /* remember */);
}

However, using an enum makes the function call even easier to understand, at the expense of some supporting code:

public enum RememberFoo {
    REMEMBER,
    FORGET
}

...

void someFunction(RememberFoo remember);

...

void ourFunction() {
    someFunction(RememberFoo.REMEMBER);
}

If you’re unable to change the signature of someFunction for whatever reason, using a temporary variable makes the code easier to read as well, sort of like simplifying conditionals by introducing a variable for no other reason than to make the code easier to parse by humans.

void someFunction(boolean remember);

...

void ourFunction() {
    boolean remember = false;
    someFunction(remember);
}

2

Rename the variable so a bool value makes sense.

This a million times better than adding a comment to explain arguments to a function because the name is ambiguous.

2

Create a local boolean with a more descriptive name, and assign the value to it. That way the meaning will be more clear.

void ourFunction() {
    bool takeAction = false;  /* false means to forget */
    someFunction( takeAction );
}    

If you can’t rename the variable, then the comment should be a little more expressive:

void ourFunction() {
    /* false means that the method should forget what was requested */
    someFunction( false );
}    

2

There is a good article that mentions this exact situation as it refers to Qt-Style APIs. There, it’s called The Boolean Parameter Trap and it’s worth a read.

The gist of it is:

  1. Overloading the function so that the bool isn’t needed is better
  2. Using an enum (as Esailija suggests) is best

This is a bizarre comment.

From the compiler’s perspective, someFunction(false /* forget */); is actually someFunction(false); (the comment is stripped). So, all that line does is call someFunction with the first (and only) argument set to false.

/* forget */ is just the name of the parameter. It’s probably nothing more than a quick (and dirty) reminder, that doesn’t really need to be there. Just use a less ambiguous parameter name, and you won’t need the comment at all.

One of advices of Clean code is to minimize number of unnecessary comments1 (because they tend to rot), and to name functions and methods properly.

Following that, I would just remove the comment. After all, modern IDEs (like eclipse) pop a box with code when you put a mouse over the function. Seeing the code should clear the ambiguity.


1 Commenting some complex code is ok.

2

To belabor the obvious, comments can lie. Thus is always better to make your code self-documenting without resorting to comments to explain, because some person (perhaps you) will change true to false and not update the comment.

If you can’t change the API, then I resort to 2 options

  • Change the comment so that it always true, regardless of the code. If you only call this once, this is a good solution, since it keeps the documention local.
     someFunction( false /* true=forget, false=remember */);`
  • Use #defines, especially if you call it more than once.
     #define FORGET   true
     #define REMEMBER false
     someFunction(REMEMBER);

I like the answer about making the comment always true, but while good I think it misses the fundamental problem with this code — it is being called with a literal.

You should avoid using literals when calling methods. Local variables, optional parameters, named parameters, enums — how you best avoid them will depend upon the language and whats available, but do try to avoid them. Literals have values, but they don’t have meaning.

In C#, I used named parameters to make this clearer

someFunction(forget: false);

Or enum:

enum Memory { Remember, Forget };

someFunction(Memory.Forget);

Or overloads:

someFunctionForget();

Or polymorphism`:

var foo = new Elephantine();

foo.someFunction();

The naming should always resolve ambiguity for booleans. I always name boolean something like ‘isThis’ or ‘shouldDoThat’, for example:

void printTree(Tree tree, bool shouldPrintPretty){ ... }

and so on. But when you are referencing someone else’s code, its best to just leave comment when passing values.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Correct comment to put for boolean function arguments that are “false”?

From some open source projects, I gathered the following coding style

void someFunction(bool forget);

void ourFunction() {
  someFunction(false /* forget */);
}    

I always have doubt about what false means here. Does it mean “forget”, or does the “forget” refer to its corresponding parameter (like in the case above), and “false” is meant to negate it?

What style is used most often and what is the best way (or some of the better ways) to avoid the ambiguity?

8

In the sample code you posted, it looks like forget is a flag argument. (I cannot be certain because the function is purely hypothetical.)

Flag arguments are a code smell. They indicate that a function does more than one thing, and a good function should do only one thing.

To avoid the flag argument, break up the function into two functions that explain the difference in the function name.

Flag argument

serveIceCream(bool lowFat)

No flag argument

serveTraditionalIceCream()
serveLowFatIceCream()

edit: Ideally, you don’t need to keep around a function with the flag parameter at all. There are cases along the lines of what Fowler calls a tangled implementation where completely separating the functions creates duplicated code. However, the higher the cyclomatic complexity of the parameterized function, the stronger is the argument for getting rid of it.


This is only a hunch, but a parameter named forget sounds like feature envy. Why would a caller tell another object to forget something? There may be a bigger design issue.

3

In layman’s words:

  • false is a literal.
  • you are passing a literal false
  • you are telling someFunction to not forget
  • you are telling someFunction that the parameter forget is false
  • you are telling someFunction to remember

In my opinion it would be better if the function was like this:

void someFunction(bool remember);

the you can call it

void ourFunction() {
  someFunction(true);
} 

or keep the old name but change your wrapper function to

void ourFunctionWithRemember() {
  someFunction(false);
} 

EDIT:

As @Vorac stated, always strive to use positive words. Double negation is confusing.

3

The parameter may be well-named; it’s hard to tell without knowing the name of the function. I assume that the comment was written by the original author of the function, and it was a reminder of what passing false into someFunction means, but for anyone coming along afterward, it’s a little unclear at first glance.

Using a positive variable name (suggested in Code Complete) may be the simplest change that would make this snippet easier to read, e.g.

void someFunction(boolean remember);

then ourFunction becomes:

void ourFunction() {
    someFunction(true /* remember */);
}

However, using an enum makes the function call even easier to understand, at the expense of some supporting code:

public enum RememberFoo {
    REMEMBER,
    FORGET
}

...

void someFunction(RememberFoo remember);

...

void ourFunction() {
    someFunction(RememberFoo.REMEMBER);
}

If you’re unable to change the signature of someFunction for whatever reason, using a temporary variable makes the code easier to read as well, sort of like simplifying conditionals by introducing a variable for no other reason than to make the code easier to parse by humans.

void someFunction(boolean remember);

...

void ourFunction() {
    boolean remember = false;
    someFunction(remember);
}

2

Rename the variable so a bool value makes sense.

This a million times better than adding a comment to explain arguments to a function because the name is ambiguous.

2

Create a local boolean with a more descriptive name, and assign the value to it. That way the meaning will be more clear.

void ourFunction() {
    bool takeAction = false;  /* false means to forget */
    someFunction( takeAction );
}    

If you can’t rename the variable, then the comment should be a little more expressive:

void ourFunction() {
    /* false means that the method should forget what was requested */
    someFunction( false );
}    

2

There is a good article that mentions this exact situation as it refers to Qt-Style APIs. There, it’s called The Boolean Parameter Trap and it’s worth a read.

The gist of it is:

  1. Overloading the function so that the bool isn’t needed is better
  2. Using an enum (as Esailija suggests) is best

This is a bizarre comment.

From the compiler’s perspective, someFunction(false /* forget */); is actually someFunction(false); (the comment is stripped). So, all that line does is call someFunction with the first (and only) argument set to false.

/* forget */ is just the name of the parameter. It’s probably nothing more than a quick (and dirty) reminder, that doesn’t really need to be there. Just use a less ambiguous parameter name, and you won’t need the comment at all.

One of advices of Clean code is to minimize number of unnecessary comments1 (because they tend to rot), and to name functions and methods properly.

Following that, I would just remove the comment. After all, modern IDEs (like eclipse) pop a box with code when you put a mouse over the function. Seeing the code should clear the ambiguity.


1 Commenting some complex code is ok.

2

To belabor the obvious, comments can lie. Thus is always better to make your code self-documenting without resorting to comments to explain, because some person (perhaps you) will change true to false and not update the comment.

If you can’t change the API, then I resort to 2 options

  • Change the comment so that it always true, regardless of the code. If you only call this once, this is a good solution, since it keeps the documention local.
     someFunction( false /* true=forget, false=remember */);`
  • Use #defines, especially if you call it more than once.
     #define FORGET   true
     #define REMEMBER false
     someFunction(REMEMBER);

I like the answer about making the comment always true, but while good I think it misses the fundamental problem with this code — it is being called with a literal.

You should avoid using literals when calling methods. Local variables, optional parameters, named parameters, enums — how you best avoid them will depend upon the language and whats available, but do try to avoid them. Literals have values, but they don’t have meaning.

In C#, I used named parameters to make this clearer

someFunction(forget: false);

Or enum:

enum Memory { Remember, Forget };

someFunction(Memory.Forget);

Or overloads:

someFunctionForget();

Or polymorphism`:

var foo = new Elephantine();

foo.someFunction();

The naming should always resolve ambiguity for booleans. I always name boolean something like ‘isThis’ or ‘shouldDoThat’, for example:

void printTree(Tree tree, bool shouldPrintPretty){ ... }

and so on. But when you are referencing someone else’s code, its best to just leave comment when passing values.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật