3 ms·
Personally, I find: x += 2 Easier to read then: x = x + 2 For many reasons.. but mainly: 1) The first statement basically says: Add 2 to x, while th
by d0m 15y ago
Personally, I find:
x += 2
Easier to read then:
x = x + 2
For many reasons.. but mainly:
1) The first statement basically says: Add 2 to x, while the second says: Take x, add 2 and add that to x.. Which is one more step.
2) When a variable is repeated multiple time, I always read the line multiple time to make sure there is no error. For instance, in english "On the screen, there there is a xyz". I'll stop and read that sentence a couple of time to try to understand it.. and then realize it's an error. Likewise, when I see x = x + 2, I think "Is it a mistake?" I.e. did the author wanted to write: y = x + 2.. if not, why does he repeat himself?
However, I'm interested in knowing in what you find it more readable :) ?
- mixmastamyk 15y agoIt's merely more familiar to the average person. I'm not against the first form and use it a lot. See my comment to llamda below for more details.
- mixmastamyk 15y agoLooking a bit closer: 1) "Take x, add 2 and add that to x" the second add should be "set x to that". "Which is one more step." It is the same amount of steps, just a difference in how it is represented to the end user. 2) Hmm, never thought about that type of connection before. In my experience I had many years of "x = x + 1" in both Algebra class and when first exposed to Basic programing. I didn't encounter C (first exposure to extended operators) for at least another decade. Whenever I see such an assignment statement I visualize this in my head... x = x + 1 x = 5 + 1 x = --> 6 <-- (woosh) x <---6 (woosh) (6) (thud)