Coding style operator precedence
Hey all,
Could someone share what the coding style preference is where operator precedence is concerned?
I notice in the staging drivers that there are many instances where there are parenthesis in statements where operator precedence should be (in my opinion) obvious. Yet, there doesn't seem to be a patch to amend it. For example:
In vc04_services/interface/vchiq_arm/vchiq_core.c, line 2136, there is
``` slot_zero->master.slot_last = first_data_slot + (num_slots / 2) - 1; ``` It could just have been ``` slot_zero->master.slot_last = first_data_slot + num_slots / 2 - 1; ```
yet I do not see anyone patching it. Nothing is mentioned in the kernel docs about operator precedence. Checkpatch.pl doesn't highlight it as a coding style either.
What is the concensus regarding operator precedence in kernel coding style? The most relevant email thread I found suggests some feel the parenthesis shouldnt be there, while others would leave it there for readability.
https://lore.kernel.org/linux-staging/[email protected]/
Should I remove the parenthesis because it is technically unnecessary, or should I leave it in for readability? Or should I just make the patch request and let the maintainer decide?
Thank you.
Cheers,
Solomon
_______________________________________________
Kernelnewbies mailing list
[email protected]
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
Quoted 18 lines · Solomon Tan
Hey all,
Could someone share what the coding style preference is where operator precedence is concerned?
I notice in the staging drivers that there are many instances where there are parenthesis in statements where operator precedence should be (in my opinion) obvious. Yet, there doesn't seem to be a patch to amend it. For example:
In
vc04_services/interface/vchiq_arm/vchiq_core.c, line 2136, there is``` slot_zero->master.slot_last = first_data_slot + (num_slots / 2) - 1; ``` It could just have been ``` slot_zero->master.slot_last = first_data_slot + num_slots / 2 - 1; ```
Quoted 3 lines
yet I do not see anyone patching it. Nothing is mentioned in the kernel docs about operator precedence. Checkpatch.pl doesn't highlight it as a coding style either.
Because as-is, it is fine.
Quoted 5 lines
What is the concensus regarding operator precedence in kernel coding style? The most relevant email thread I found suggests some feel the parenthesis shouldnt be there, while others would leave it there for readability.
https://lore.kernel.org/linux-staging/[email protected]/
Make it obvious when people read the code as we do not always remember the precedence order. We write code for people first, compilers second.
thanks,
greg k-h
_______________________________________________
Kernelnewbies mailing list
[email protected]
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
Quoted 4 lines · Greg KH
On Fri, Apr 22, 2022 at 10:08:54PM +0800, Solomon Tan wrote:
Make it obvious when people read the code as we do not always remember the precedence order. We write code for people first, compilers second.
Understood. Thanks for the advice, Greg. :)
Quoted 4 lines
thanks,
greg k-h
Cheers,
Solomon
_______________________________________________
Kernelnewbies mailing list
[email protected]
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies