From 00717e3cf6c6edfcbf180f1f1134a1ad5f877751 Mon Sep 17 00:00:00 2001 From: Amarpreet Singh Date: Thu, 6 Jun 2024 09:07:30 -0400 Subject: [PATCH] Update the coding standard Issue: https://github.com/eclipse-openj9/openj9/issues/19261 Co-authored-by: Jason Feng Co-authored-by: Keith W. Campbell Signed-off-by: Amarpreet Singh --- doc/CodingStandard.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/doc/CodingStandard.md b/doc/CodingStandard.md index fd9a7899ed6..55a0dcbed23 100644 --- a/doc/CodingStandard.md +++ b/doc/CodingStandard.md @@ -412,6 +412,21 @@ if (NULL != myPointer) { } ``` +* When comparing a value with two other values with <, <=, >, or >= utilize the following format: + +```c +if ((0 <= logLevelValue) && (logLevelValue <= 4)) { +``` + +Correct +```c +if ((0 <= logLevelValue) && (logLevelValue <= 4)) { +``` + +Wrong +```c +if ((logLevelValue >= 0) && (logLevelValue <= 4)) { +``` #### Rationale * Keeping rvalues on the left can prevent accidental assignment (= vs ==). @@ -421,6 +436,7 @@ if (NULL != myPointer) { * The ") {" sequence provides a clear visual indication that the condition has ended and the block has begun. * C order of operation rules are complicated; avoid confusion and mistakes by using parentheses to make the order explicit. * Combining assignment and tests violates the "Try to do only one thing on each line of code" principle. +* It seems that when comparing a value with two other values as ((min <= value) && (value <= max)), it is clearer. ### Parameter passing