When you copy and paste code from Stack Overflow, it is best practice to provide a link to the place where you got that code. This is for two reasons:
- The person has helped you greatly by giving you some valuable code. You should reward them by giving the programmer a comment in a codebase that they are never ever going to see.
- It's useful to look back at the answer in case you need to debug the code later. This is very useful if the author of the code in question also wrote a detailed explanation of his code.
Sometimes, during the course of a project, the original SO code will be heavily modified by you. In that case, it makes no sense to keep the attribution, since you have thoroughly mutilated the work of the original author. There is no reason to praise the author anymore, and the author's detailed explanations may prove useless thanks to your own modifications. Remove the comment from your code ASAP.
Use your professional judgment when deciding when to give attribution and when you should remove it.
Note while that attribution is generally not required for 'code snippets', they are usually required if you are complying with the terms of a license (either the default CC-BY-SA 3.0 or a permissive license).