There was a question asking for a cross platform timer, and I had written one a while ago, so I just pasted it as an answer with a little bit information how to use that code. but that I'm wondering is it a good practice? or is there any other (presumably better) way to share that code? side note: I really don't mind sharing that code on codeproject or any other site like that, I just don't feel like creating an account there!
If it's a good answer to the question, then go right ahead. Just ensure that the code doesn't do things besides answer the question. For example, if someone was asking how to check a certain type of collision and you pasted your entire collision handling library, that wouldn't be a very good answer; you should post only the relevant part.
In the case of your example, your timer module probably had several platform-specific timer bits, wrapped in
if statements checking what platform the code is running on, right? So in this case, your answer could have been several code blocks, each one handling a different platform, and then an example of checking the current platform. Together it would be all the code you pasted anyway, but it's probably a more helpful answer since it walks them through step-by-step instead of just throwing a bunch of code at them and relying on them to either figure it out or just copy-paste it into their code without any thought as to how it works.
Example answer for inspiration
Here is code to check the time in Windows:
Here's how to do it in Mac:
And here's how to check the platform:
I implemented that in a single method that I called getCurrentTime(), and then I implemented a Timer class which used that method to track differences in time, ......
I pasted the full source of my Timer class in this pastebin. Consider it public domain. If you want to use it in your project, you will need the foo and bar libraries.
You're also welcome to put your code elsewhere and link to it, but I would still recommend you include at least the most vital parts of your code in your answer, or maybe pseudocode for what you did or how you came up with the algorithm. Links elsewhere aren't guaranteed to last as long as this site, and maybe you're familiar with the feeling of clicking a link that promised to solve all your problems and hitting a 404 -- I certainly have. It's sad. Semi-relevant XKCD.
Personally, I like to store bits of code like this in a Gist, if they don't deserve their own repository. Besides being a typical Pastebin-type website, a gist can have multiple files with filenames, you can do a
git clone on it and push changes back into the gist, download it (all files) as a tar.gz, and I trust Github to last a long time. But there are many pastebin websites out there, and other technologies that also allow you to store and share files (Dropbox public folder, Amazon S3, etc.).
See also this question from Meta SO: Best way of pasting a lot of code into a question or answer?
I've had a couple of times i have wanted to share a file from a private repo of mine and found the easiest solution is to use http://pastebin.com/.