Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Order of precedence #132

Open
chqrlie opened this issue Dec 13, 2024 · 2 comments
Open

Order of precedence #132

chqrlie opened this issue Dec 13, 2024 · 2 comments

Comments

@chqrlie
Copy link
Contributor

chqrlie commented Dec 13, 2024

Changing the order of precedence of the C operators is problematic:

  • it causes more confusion for little benefit.
  • it is inconsistent with the C syntax (breaks the philosophy Should be easy to learn for C programmers)
  • it is inconsistent with other languages that inherit the C operators: c++, java, javascript, csharp, kotlin... that have huge momentum, making C2 quite unfriendly to learn and port code to.
  • it is unnecessary to fix the problem.

I propose you keep the same order of precedence, but require explicit parentheses for subexpressions when mixed with other operators whose precedence is deemed counter-intuitive or at least confusing. This is what clang and gcc do with strict warning levels and does not break compatibility: code that compiles in C2 would parse to the same AST in the other languages.

a + b >> c + d would require parentheses to lift the confusion between a + (b >> c) + d and (a + b) >> (c + d).

Same for a & b == c which should always be written (a & b) == c

a || b && c would become a || (b && c) or (a || b) && c depending on the programmer's intent, which is non obvious.

But a & b & c, a && b && c... would be accepted as unambiguous.

I think the is a better approach for the philosophy Should help to avoid common mistakes.

@bvdberg
Copy link
Member

bvdberg commented Dec 18, 2024

Again, this was discussed a Lot. I think the C system is a big cause for bugs. Many programmers are unsure and just add some parenthesis to make sure. This is fine I think and also makes the code easier to read without having to memorize the whole precedence table.

Maybe we could add a warning in the compiler when this happens?

@chqrlie
Copy link
Contributor Author

chqrlie commented Dec 18, 2024

Again, this was discussed a Lot. I think the C system is a big cause for bugs. Many programmers are unsure and just add some parenthesis to make sure. This is fine I think and also makes the code easier to read without having to memorize the whole precedence table.

Yes, and mandating these parentheses seems to me a better fix than changing the precedence rules.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants