coding-style.md: add various rules for writing C expressions#39
coding-style.md: add various rules for writing C expressions#39DDvO wants to merge 5 commits intoopenssl:masterfrom
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
|
As a policy change, this will require an OTC vote at least two weeks after the PR was first raised. |
zuiderkwast
left a comment
There was a problem hiding this comment.
Nice. I was going to create this PR myself.
|
Re |
t8m
left a comment
There was a problem hiding this comment.
Please drop the <br/> marks. Replace with blank lines where appropriate.
Would it be adequate to start with such an OTC vote now, or after two weeks of waiting time? |
|
It has to wait. |
policies/coding-style.md
Outdated
| if (p == NULL && !f(2 * x + y == z++)). | ||
| ``` | ||
|
|
||
| For clarity, always put parentheses when mixing `&&` and `||` operations. |
There was a problem hiding this comment.
I suspect we will want a whole lot more exceptions. C has lots of operators and almost nobody learns the precedence order. Perhaps this is a common sense rule, reduce parenthesis as far as is reasonable but no further?
E.g. a <= b == c >= d + 1 is a perfectly well defined C expression with minimal parenthesis but a lot of folks won't instantly know what it's doing.
There was a problem hiding this comment.
I've extended the text and the example this way,
and now also explicitly cover the so-called bitwise operators &, ^, and |.
There was a problem hiding this comment.
The whole approach of preventing "extra" parentheses is something I think should be removed completely - as mistakes do happen and parentheses do significantly improve readability.
There was a problem hiding this comment.
@t-j-h I wholeheartedly disagree. For me a code with needless parentheses is much less readable actually. I always have to count the parentheses and so on to understand what is going on there. The example in this PR is a perfect one for me. The condition with the extra parentheses looks like parentheses hell to me where the one without is perfectly readable.
There was a problem hiding this comment.
In most real life cases I'd agree with @t-j-h. I never remember the operators priority :(
There was a problem hiding this comment.
Well, what to consider a reasonable amount of extra parentheses is a matter of both taste and fluency with C.
This could be debated ad nauseam.
There was a problem hiding this comment.
If this ever becomes a problem in practice (in a code review) just update the coding style again when that happens?
8d7f420 to
103a28c
Compare
|
Vote for accepting the coding style changes at f2ae669 has started. |
|
|
|
Vote: [+1] |
|
|
|
Vote: [+1] |
1 similar comment
|
Vote: [+1] |
|
Vote: [+0] |
| ``` | ||
|
|
||
| For clarity, always put parentheses when mixing `&&` and `||` operations, | ||
| comparison operators like `<=` and `==`, and bitwise operators. |
There was a problem hiding this comment.
So now, (a == 1 || b == 2) is badly seen? We have written that sort of expression all over the place until now...
There was a problem hiding this comment.
This recent addition was motivated by #39 (comment).
There was a problem hiding this comment.
That comment was intended as the kind of mess C operators can create. I don't think it's a great example to make a rule from, but I can live with it. I have seen code along the lines of a < b != c < d.
I think we need some flexibility in interpretation. Minimal parenthesis is an ideal but needs to be tempered but comprehension.
There was a problem hiding this comment.
I think we need some flexibility in interpretation
I agree on principle, but have a hard time seeing the flexibility in "always"
There was a problem hiding this comment.
I think it can be added after the minor policy edits is approved.
It would probably change the meaning...
There was a problem hiding this comment.
The way I read it the sentence above is completely contradicting the immediately previous example.
There was a problem hiding this comment.
I think it can be added after the minor policy edits is approved.
It would probably change the meaning...
No, the sentence @t8m proposed to add later:
I.E., do not depend on
&&having higher precedence than||,<=having higher precedence than==, and&having higher precedence than|or^.
would just clarify the sentence before:
For clarity, always put parentheses when mixing
&&and||operations, comparison operators like<=and==,and bitwise operators.
There was a problem hiding this comment.
The way I read it the sentence above is completely contradicting the immediately previous example.
Which sentence do you mean here?
All the rules and examples about extra parentheses state exceptions on the most general rule stated upfront:
Avoid needless parentheses as far as reasonable.
In this sense, they are of course somewhat contradicting ;-)
There was a problem hiding this comment.
I've understood it (and I think it was meant to be) as mixing operators in the same category only. I.E. this does not apply to
a == b && c > d. But applies toa && b || cwhich should be written as(a && b) || ceven if the former is equivalent according to C rules.
Upon re-reading it, I can understand the sentence like that. But it's not perfectly clear. I suspect that we might have disputes later on depending on interpretation.
Ah well
|
|
|
|
| ``` | ||
|
|
||
| Do not use implicit checks for | ||
| numbers (not) being `0` or pointers (not) being `NULL`. |
There was a problem hiding this comment.
Where the number is intended to be a boolean value I think this is ok and is very common in the codebase (in particular when checking function return values)m e.g. if (!EVP_DecryptUpdate(...))
There was a problem hiding this comment.
Sure, and therefore the rule is stated just about numbers (integers etc.), not about Boolean values.
Maybe this should be made more clear.
There was a problem hiding this comment.
@mattcaswell it would have been good to receive such comments regarding clarifications earlier,
not after most people have already voted.
There was a problem hiding this comment.
To clarify this, we could add (after the given examples) for instance:
Note that Boolean values shall be used directly as usual, e.g.,
if (check(x) && !success(y))
|
Vote: [-1] I broadly support this, but there are a couple of details I'm not happy with. |
|
With the discussion, I'm changing my vote because I think we need additional clarifications: Vote: [-1] |
|
Changing my vote too for the current text. Vote: [-1] |
| For clarity, always put parentheses when mixing `&&` and `||` operations, | ||
| comparison operators like `<=` and `==`, and bitwise operators. |
There was a problem hiding this comment.
Looking at the sentence again, it wouldn't be very hard to make it clearer:
| For clarity, always put parentheses when mixing `&&` and `||` operations, | |
| comparison operators like `<=` and `==`, and bitwise operators. | |
| For clarity, always put parentheses when mixing operators of the same category, | |
| such as `&&` and `||` operations, or comparison operators like `<=` and `==`, | |
| or bitwise operators. |
There was a problem hiding this comment.
Hmm, when re-reading the above suggestion, which does include an improvement, I do not like the "category" and "such as" very much.
For instance, this could also be understood that one should put parentheses when mixing * and +, which is not intended.
So I propose a further refinement as follows:
For clarity, always put parentheses when mixing the Boolean && and || operations,
when mixing comparison operators like <= and ==, and when when mixing bitwise operators.
There was a problem hiding this comment.
For clarity, always put parentheses when mixing the Boolean
&&and||operations, when mixing comparison operators like<=and==, and when when mixing bitwise operators.
"Boolean" should be "Logical", to match C parlance.
The last "and" should probably be "or"
Drop the double "when"
With those mods, I approve
There was a problem hiding this comment.
Oh, and "operations" should be "operators"
There was a problem hiding this comment.
Yes, and I suggest including in the sentence also examples for the bitwise operators.
So:
For clarity, always put parentheses when mixing the logical && and || operators,
mixing comparison operators like <= and ==, or mixing bitwise operators like & and | .
There was a problem hiding this comment.
Any changes will require a new vote -- too many have voted to change the wording now. We don't have a process to abort a vote without it failing.
I don't think there is a need for different failure modes. If the vote fails, it fails. But if the OTC members indicate that a revised version would be accepted, then discussion can be immediately restarted on a new PR.
There was a problem hiding this comment.
@DDvO I agree with Richard: IMHO a PR which has been voted down should be closed. In cases like this one, were it just failed because changes were requested, it can immediately be replaced by a new PR from the same branch (or with some suffix like
coding-style-expressions-r2, as you prefer).
True, at least when using the same branch, a new PR is very easily set up.
Yet a pointer to the previous PR should be given such that original discussion can be found quickly.
Having multiple votes within the same PR would make the thread history too confusing. But at this moment, that's just my personal opinion Your thoughts, @openssl/otc ?
That's a very good point in favor of a new PR.
There was a problem hiding this comment.
The vote includes the hash. I don't see a big problem having a second vote here.
Another alternative would be an issue for the vote, keeping this PR open.
There was a problem hiding this comment.
The current process IMO does not allow for second vote. If anyone wants to propose a change to it no problem. I suppose we would need to change it to always use a separate issue for the votes.
There was a problem hiding this comment.
@paulidale @t8m this is actually a very good idea: we shouldn't carry out the vote in the PR, but always create a dedicated Issue for a vote, with a standardized subject line (VOTE: <subject>). In this way, we could decouple the PR from the vote. Nevertheless, we need to amend the policies to regulate things like a shortened waiting period, if desired.
|
Vote: [-1] I'm not opposed against this PR in general, but I see some need for more discussion as well. |
|
I'm going to set up a new PR with the two improvements proposed above this evening (European time). |
|
Changing my vote so I can immediately close the vote. Vote: [-1] |
There will be a review period as usual. And this time we promise to review before voting 😉. |
|
Another concern with a new PR is "does the waiting period reset"? |
There is no escape route for that. So yes. |
|
Yes it does, at least until we change the policy. (We could decide to have a shorter waiting period for revised PRs in the future.) |
|
Vote is closed. The change is rejected. |
|
Closing. Further discussion should be in the new pr once it is open. |
|
So I'm voting -1 too |
Add a first chunk of rules, taken from the list of proposed ones collected at openssl/openssl#10725,
focusing on how (not) to write C expressions.
This PR significantly generalizes the former Miscellaneous section of policies/coding-style.md.
This PR only partially fixes openssl/openssl#10725 - as @mattcaswell wrote in openssl/openssl#10725 (comment):