1
\$\begingroup\$

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!

\$\endgroup\$
2
  • \$\begingroup\$ In my opinion, if you're pasting code and it's going to end up as a big snippet/answer - I would see if I could condense it down into psuedo-code and let the person asking the question do the actual implementation. \$\endgroup\$
    – Grey
    Oct 10 '12 at 22:19
  • \$\begingroup\$ in that case I'm not pretty sure if it was possible, since he was asking for the code itself. \$\endgroup\$
    – Ali1S232
    Oct 10 '12 at 22:23
1
\$\begingroup\$

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:

[some code]

Here's how to do it in Mac:

[some code]

And here's how to check the platform:

[some code]

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?

\$\endgroup\$
0
\$\begingroup\$

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/.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ the problem is none of those code sharing sites save your code permanently or at least I don't know any that provide the link. this means my answer might get usefulness without anything happening to this site. still it's not a very bad idea. \$\endgroup\$
    – Ali1S232
    Oct 10 '12 at 22:49
0
\$\begingroup\$

I think it is fine to share a lot of code. It is more readable to explain how the code works and share the actual code as a link unless it is very short and self commented. Pasting a lot of code in the answer makes scrolling to other answers more difficult.

\$\endgroup\$

You must log in to answer this question.

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