ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

I think "=+" should be a syntax error

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • I think "=+" should be a syntax error

    Hello,

    Someone coded X =+ 1 (that's X space =+ space 1) when they meant to code X += 1.

    The compiler allowed this. It's acts like "Z-ADD 1 to X".

    I think the compiler should flag "=+" as an invalid operator.

    Change my mind.

    Mike

  • #2
    I don't think it should be an error!
    X = +1 is a valid notation (same as x = -1)... and if Blanks are included or not or on which position is up to the programmer

    You may open an IBM idea

    Comment


    • #3
      But that's the problem. The code wasn't written as "X space = space +1".

      My opinion is consecutive characters in an operator capacity should be syntax-checked.

      It would be nice if Barbara chimed in here.

      Comment


      • #4
        You're both right. Both are valid but they mean entirely different things. So what did the programmer intend? The ambiguity could be cleared up by making it a shop standard to not use these C style operators. A compiler or control option to allow or disallow would be a nice feature.

        Comment


        • #5
          What does X += 1. mean ?

          Comment


          • #6
            X += n is shorthand for writing X = X+n. n can be any number.
            Common in C, though adding 1 is very common so in C it would normally be abbreviated further to just X++

            Comment


            • #7
              Originally posted by john.sev99 View Post
              Common in C, though adding 1 is very common so in C it would normally be abbreviated further to just X++
              Not just C... Virtually every language I know of (minus stuff like CL or Cobol) uses these. C, C++, C#, Java, JavaScript, PHP, Python... The list goes on endlessly. People who resist it frustrate me... it's no different from the "ADD X Y" vs "X ADD Y Y" from fixed format. There is this horrible element within RPG programmers that seems to think anything that wasn't there 30 years ago is somehow bad -- and seems mostly to be because they don't want to learn the changes.

              With regards to the original question

              Someone coded X =+ 1 (that's X space =+ space 1) when they meant to code X += 1.
              Because you can do something like "X=1" (where there aren't any spaces) you can also do "X=-1" and "X=+1". Like it or not it's always been this way, and changing it won't happen because IBM does not believe in making changes that will break existing code unless there's a REALLY good reason (such as a security problem -- which this clearly isn't.)

              Comment


              • #8
                I'm not against change, but I also like code that I can look at and understand quickly. X++ might save a few keystrokes but does nothing to help make the code's function understandable. It doesn't help people new to those languages when confusing shortcuts are used.
                There is this horrible element within RPG programmers that seems to think anything that wasn't there 30 years ago is somehow bad -- and seems mostly to be because they don't want to learn the changes.

                Comment


                • #9
                  Originally posted by MFisher View Post
                  I'm not against change, but I also like code that I can look at and understand quickly. X++ might save a few keystrokes but does nothing to help make the code's function understandable. It doesn't help people new to those languages when confusing shortcuts are used.
                  ... and you think ADD X Y was easier to understand?

                  Comment


                  • #10
                    My point is CLARITY is better than saving a few keystrokes.

                    Comment


                    • #11
                      Originally posted by MFisher View Post
                      My point is CLARITY is better than saving a few keystrokes.
                      I've seen programs with 1000 line long SQL statements that take programmers days to decipher.

                      I've seen programs with goto logic and indicators that take days to decipher.

                      I've seen programs where code uses a lot of global variables, where people cannot figure out what is going wrong because a variable is being changed in a completely obscure place.

                      I've seen a million cases of convoluted logic that is really unclear and hard to follow.

                      Never once have I seen a programmer stumped for more than a second or two (and that's when learning the language) by X++. Sorry, but adding one to a variable is quite probably the simplest and least confusing thing that most programmers deal with.

                      Comment


                      • #12
                        I think everyone understands x += n is equivalent to x = x + n. The problem is it's prone to typos. Switching the order of + and = is a problem, rather than adding n to x it assigns n to x.

                        I avoid += and -= for this reason.

                        I agree with OPs point about flagging "x =+ n" and "x =- n" as invalid. Is it a typo, were the + and = transposed or was assignment intended?

                        "x = +n" and "x = -n" are, I think, unambiguously intended as assignments and should be valid. Then there's "x=+n" and "x=-n"... I'd say those are valid assignments too but...

                        I just avoid the whole mess and never use += and -=, not because they're new but because they're error prone. Transposition is a common typing error.

                        x++ or x-- are fine, they're unambiguous and not prone to typos. It's the += -= that are risks.​

                        Comment

                        Working...
                        X