CodingGuidelines: !! is not all that exotic
PATCH 1/1 CodingGuidelines: !! is not all that exotic
Remove the clause against "clever tricks" that cites !!value that is a standard technique to normalize a value to 0/1 boolean. We could resurrect it if we can come up with a truly exotic example that we would want to avoid, but this is not a good enough justification for this rule.
---
* I think we saw a good example that !! was used in a recent review thread, and the idea to remove this rule was floated back then, but without following through.
Documentation/CodingGuidelines | 4 ---- 1 file changed, 4 deletions(-)
Documentation/CodingGuidelines +0 -4
@@ -522,10 +522,6 @@ For C programs: that_is_used_in + a_very_long_expression) { ... - - Some clever tricks, like using the !! operator with arithmetic - constructs, can be extremely confusing to others. Avoid them, - unless there is a compelling reason to use them. - - Use the API. No, really. We have a strbuf (variable length string), several arrays with the ALLOC_GROW() macro, a string_list for sorted string lists, a hash map (mapping struct
Hi Junio
Quoted 11 lines · Junio C Hamano
Remove the clause against "clever tricks" that cites !!value that is a standard technique to normalize a value to 0/1 boolean. We could resurrect it if we can come up with a truly exotic example that we would want to avoid, but this is not a good enough justification for this rule.
Signed-off-by Junio C Hamano <[email protected]>---
* I think we saw a good example that !! was used in a recent review thread, and the idea to remove this rule was floated back then, but without following through.
Yes, its come up a couple of times recently, thanks for putting a patch together.
Phillip
Quoted 18 lines
Documentation/CodingGuidelines | 4 ---- 1 file changed, 4 deletions(-)Documentation/CodingGuidelines +0
-4@@ -522,10 +522,6 @@ For C programs: that_is_used_in + a_very_long_expression) { - - Some clever tricks, like using the !! operator with arithmetic - constructs, can be extremely confusing to others. Avoid them, - unless there is a compelling reason to use them. - - Use the API. No, really. We have a strbuf (variable length string), several arrays with the ALLOC_GROW() macro, a string_list for sorted string lists, a hash map (mapping struct
Quoted 20 lines · Junio C Hamano
Remove the clause against "clever tricks" that cites !!value that is a standard technique to normalize a value to 0/1 boolean. We could resurrect it if we can come up with a truly exotic example that we would want to avoid, but this is not a good enough justification for this rule.
Signed-off-by Junio C Hamano <[email protected]>---
* I think we saw a good example that !! was used in a recent review thread, and the idea to remove this rule was floated back then, but without following through.Documentation/CodingGuidelines | 4 ---- 1 file changed, 4 deletions(-)Documentation/CodingGuidelines +0
-0
Documentation/CodingGuidelines For C programs: · line 527
Quoted 10 lines
Documentation/CodingGuidelines +0
-4@@ -522,10 +522,6 @@ For C programs: that_is_used_in + a_very_long_expression) { - - Some clever tricks, like using the !! operator with arithmetic - constructs, can be extremely confusing to others. Avoid them, - unless there is a compelling reason to use them. - - Use the API. No, really. We have a strbuf (variable length string), several arrays with the ALLOC_GROW() macro, a string_list for sorted string lists, a hash map (mapping struct
I wondered why this was added in the first place. But this dates back to 6d0618a8 (Add Documentation/CodingGuidelines, 2007-11-08). So it was there from the start. And you objected to it on the first version:
https://lore.kernel.org/git/[email protected]/
JC> I actually think (!!var) idiom is already established in our JC> codebase.
Andreas Ericsson replied and emphasized *arithmetic expressions*, which I guess means that it is not just a single expression normalized to a boolean.
AE> Not very widely for arithmetic expressions though. I believe this AE> alludes to the (!!a + !!b + !!c) idiom used earlier, where it's not AE> exactly clear *why* a, b and c can be > 1 if != 0 is the only thing AE> we care about.