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?

share|improve this question
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. –  Robert Harvey Sep 30 '11 at 6:32
    
I wouldn't even change x *= -1; to x = -x; –  Nick Dandoulakis Sep 30 '11 at 10:43
    
@NickDandoulakis agreed, and there's probably a benchmark somewhere that changing the sign is slower than multiplying ;) –  Yar Sep 30 '11 at 13:50
    
@Yar, hahaha I was expecting such a comment :) –  Nick Dandoulakis Sep 30 '11 at 16:30
add comment

3 Answers

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.

share|improve this answer
add comment

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

share|improve this answer
2  
And even in that case, I'd first consider posting a comment, unless the user is not very active. –  Benjol Sep 30 '11 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 Sep 10 '12 at 16:35
add comment

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.

share|improve this answer
add comment

You must log in to answer this question.

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