SAP / styleguides

This repository provides SAP style guides for coding and coding-related topics.
Other
1.68k stars 445 forks source link

Chained Equal Signs #209

Closed lucasborin closed 3 years ago

lucasborin commented 3 years ago

Thread started here: https://github.com/SAP/code-pal-for-abap/issues/398

"Given
DATA a TYPE string VALUE 'A'.
DATA b TYPE string VALUE 'B'.
DATA c TYPE abap_bool.

"When
c = a = b.

"Then
ASSERT c = abap_false.

As represented in the above code, a user can confuse the chained equal signs. Is it assigning the values? Is it comparing a and b?

Should I enhance the style guide by asking people to avoid chained equals signs when the target attribute/variable is a boolean? Please, feel free to share your thoughts.

Ps: It is an assignment. Therefore, c (which is char1 technically) will receive b's value.

nomssi commented 3 years ago

ABAP's missing boolean type means type coercion is involved in any assignment to a pseudo-boolean variable, which can be erroneous without a syntax error. Pining it on Chained Assignments is IMO a misnomer. as the issue also appear if we just write

      c = a.

in the code above. It is boolean assignments without the xsdbool( ) or boolc( ) functions that should be scrutinized/discouraged.

My use case for Chained Assignments is saving a temp while traversing the nodes of a linked list.

larshp commented 3 years ago

In my opinion: avoid using chained assignment in all cases, its rarely used in actual code, and easy to work around