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

rule idea: conditional-expressions-should-be-in-brackets #1152

Open
ccecvb opened this issue Jan 13, 2025 · 5 comments
Open

rule idea: conditional-expressions-should-be-in-brackets #1152

ccecvb opened this issue Jan 13, 2025 · 5 comments

Comments

@ccecvb
Copy link

ccecvb commented Jan 13, 2025

I'd like to propose a new rule : conditional-expressions-should-be-in-brackets.

Non-compliant code

define variable xx as integer no-undo extent 20.
define variable sep as character no-undo initial ';'.
put unformatted "header" +
            if xx[1] = ? then "" else string(xx[1]) + sep +
            if xx[1] = ? then "" else string(xx[1]) + sep
   .

Compliant code

define variable xx as integer no-undo extent 20.
define variable sep as character no-undo initial ';'.
put unformatted "header" +
            (if xx[1] = ? then "" else string(xx[1])) + sep +
            (if xx[1] = ? then "" else string(xx[1])) + sep
   .
@ccecvb ccecvb changed the title rule idea: onditional-expressions-should-be-in-brackets rule idea: conditional-expressions-should-be-in-brackets Jan 13, 2025
@stefandrissen
Copy link

Compliant and incompliant are functionally different.

@gquerret
Copy link
Contributor

Compliant and incompliant are functionally different

Output is identical

@ccecvb
Copy link
Author

ccecvb commented Jan 13, 2025

@stefandrissen

Your remark kind of proves my point ;-) . It's confusing.

It's not my code, I don't know which version is functionally correct, looking at the indentation I think the developer assumes this to behave like the compliant version, even if it behaves as

Functionally equivalent compliant code

define variable xx as integer no-undo extent 20.
define variable sep as character no-undo initial ';'.
put unformatted "header" +
            (if xx[1] = ? then "" else (string(xx[1]) + sep +
            (if xx[1] = ? then "" else string(xx[1]) + sep)))
   .

@stefandrissen
Copy link

stefandrissen commented Jan 13, 2025

Assuming that the actual case was attempting to use x[1] and x[2], watch how different the results are (extended to 3 to make it even more obviously different):

var char[3] xx = ['one',?, 'three'].
var char sep = '|'.

message
    ( if xx[1] = ? then "" else string(xx[1]) ) + sep +
    ( if xx[2] = ? then "" else string(xx[2]) ) + sep +
    ( if xx[3] = ? then "" else string(xx[3]) ) + sep skip(1)

    if xx[1] = ? then "" else string(xx[1]) + sep +
    if xx[2] = ? then "" else string(xx[2]) + sep +
    if xx[3] = ? then "" else string(xx[3]) + sep
view-as alert-box.
            

The output is different.

@gquerret
Copy link
Contributor

I was initially focused on the parser execution time, and I assumed the code of the developer was right. Given how that was formatted, I assumed the code was right and tested. It clearly wasn't...

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

3 participants