14

I was looking at this answer and I was working through the code and I thought, "it would be great to improve the answer with my code." But then again, the answer already works. I would just change things like this:

if (x < 0) {
    x *= -1;
}

to

x = ABS(x)

and rename some variables for clarity. But then I thought, should I make fixes if they are not 100% relevant to the central point of the question/answer? The author might get offended and in fact could argue that ABS is worse than multiplying, etc. etc.

Leave it alone, right, unless it's actually WRONG?

4
  • 5
    You will note that that I added a couple of spaces to your code example for clarity. That's about the only code improvement I'm personally comfortable with. If I have a suggestion for genuine code improvement (like your example), I will leave a comment with my suggestion, or post an answer.
    – user102937
    Commented Sep 30, 2011 at 6:32
  • I wouldn't even change x *= -1; to x = -x; Commented Sep 30, 2011 at 10:43
  • @NickDandoulakis agreed, and there's probably a benchmark somewhere that changing the sign is slower than multiplying ;) Commented Sep 30, 2011 at 13:50
  • @Yar, hahaha I was expecting such a comment :) Commented Sep 30, 2011 at 16:30

3 Answers 3

14

I suggest leaving a comment about it. The OP might have reasons for doing it a certain way, and since changing it isn't critical, leaving a comment lets the OP take a look at it and see if there was a reason for doing it the original way. If not, the OP can change it if he/she prefers your version. At the same time, your comment will help other readers even if the OP decides to not change it.

9

I would only submit changes to code if it is actually wrong; stylistic changes are not what Stack Overflow is about.

2
  • 2
    And even in that case, I'd first consider posting a comment, unless the user is not very active.
    – Benjol
    Commented Sep 30, 2011 at 5:44
  • 1
    I'm sorry Jeff. I have to respectfully disagree. It seems to me that changing the code if it's wrong also changes the answer. Isn't it better to down vote and leave a comment? When I receive comments about my own incorrect code, I change it myself. If the code was left by an inactive user, the comment will still be there.
    – Herbert
    Commented Sep 10, 2012 at 16:35
7

Yeah, that's the kind of thing I would just point out in a comment. This example code is admittedly a bit silly, but for bigger or more subjective issues, it will help both the asker and others who view the question to see the original code and the suggested alternative. If you just go in and replace stuff, a valuable learning experience may go completely overlooked. This reason on top of just politeness.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .