8

It took almost half an hour to get this problem solved because the OP named their functions and variables poorly. Naming proved itself so important in this case I had to rename pretty much everything when trying to solve the problem so I didn't get lost.

After getting the problem solved I had no idea if I should implement the solution to the poorly named code or to answer using the highly modified version I had made to work on. After all, the real problem does not lie on naming and, as far as I know, we should try to preserve OP's coding style as much as possible.

Stack Overflow is not Code Review, so I want to know if is it okay to rename everything for the OP, or should we always leave this job to them (or Code Review)?

2
  • 3
    @Glorfindel That's entirely unrelated. He's not suggesting editing the OP, nor is he suggesting editing someone else's answer.
    – Servy
    Sep 6, 2016 at 20:42
  • 1
    Look here please. I tried to completely "bang it in shape" to be useful for future research. Sep 6, 2016 at 20:44

2 Answers 2

12

You're certainly not prohibited from making changes to the provided code that aren't explicitly to solve the exact problem asked. As to whether or not it improves the answer is going to be dependent on context. Most importantly, the essential changes/additions to solve the specific problem being asked should be front and center, and very clear to the reader. Having additional changes that the reader can look through after seeing the solution to the core problem is fine, but if you spend the vast majority of the post showing/discussing those changes, or don't start with the essential changes, then you may well be obscuring the actual answer to such a point that the answer is not particularly helpful, and if that happens, the votes will reflect that.

2
  • 1
    While I completely agree with the point made, @Glorfindel recommended a related question which the accepted answer states: "especially when it comes to renaming variables, the resulting code is almost guaranteed not to work in the context of the OP's project." Isn't that a good reason not to change anything at all? Sep 6, 2016 at 21:00
  • 4
    @TiagoMarinho That will depend on the scope of the refactor, in most cases, you're changing the entirety of where that variable is in scope, so no. Even if you are, a change like that would be simple enough to either undo from your answer (when applying it) or to apply to the other code affected by it. All that's important is that it's clear what the core problem is, and how to fix it. Being able to copy-paste code from an answer for the reader is not only not essential, but it often detracts from the quality of an answer. What important is that the reader understand how to fix the problem.
    – Servy
    Sep 6, 2016 at 21:06
4

There is no requirement that an answer uses code from the question, and especially, that it provide copy-paste ready code. Keep suggestions about code style to comments or if you really want to add them to an answer - make a clearly marked section at the end.

You are completely free to use whatever code style you want as long as the answer clearly demonstrates what was the problem and how to solve it.

Note: if question becomes popular it may even be a good idea to change the code in the question to use a consistent coding style. For all other cases, a comment on the question about updating style is more than enough.

0

You must log in to answer this question.

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