views:

362

answers:

7

I'm sitting here writing a function that I'm positive has been written before, somewhere on earth. It's just too common to have not been attempted, and I'm wondering why I can't just go to a website and search for a function that I can then copy and paste into my project in 2 seconds, instead of wasting my day reinventing the wheel.

Sure there are certain libraries you can use, but where do you find these libraries and when they are absent, is there a site like I'm describing?

Possibly a wiki of some type that contains free code that anybody can edit and improve?

Edit: I can code things fine, I just don't know HOW to do them. So for example, right now, I'm trying to localize a robot/car/point in space. I KNOW there is a way to do it, just based off of range and distance. Triangulation and Trilateration. How to code that is a different story. A site that could have psuedo code, step by step how to do that would be ridiculously helpful. It would also ensure the optimal solution since everybody can edit it. I'm also writing in Matlab, which I hate because it's quirky, adding to my desire for creating a website like I describe.

+20  A: 

StackOverflow.com. No, I'm not joking.

At its best, people come here saying "hasn't some library done X already", and very often the Collective Wisdom answers "yes". But the biggest obstacle is lack of a description language: even here, a big problem for many posters is describing the problem clearly enough for others to recognize it as something they've seen before.

And if people can't understand what you're trying to do, no search engine will.

egrunin
Excellent answer. +1
chiggsy
I had this exact problem trying to write an algorithm for determining possible winning hands in varying sized hands of cards. Like if you could make a 7 card hand, how many sets (pair, trips, X-of-a-Kinds) are possible in that. Turns out the general form of the problem is well known (called an Integer Partition) and it's even a sub-problem of larger set theory, but I didn't know how to describe the problem well enough to look it up under those terms for a long while.
CodexArcanum
A: 

Aside from sites like this (Stack overflow) I don't think there's many, maybe Codeplex, but I almost marked you -1 for assuming that code found on the internet is yours to copy.

I'd suggest reading about software licencing, I hope you'd at least comment where you got it from.

Aren
+1 for the -1 .
Timothy
+1 for the +1 on the -1.
Chris Lively
+1 for the +1 on the +1 on the -1 :)
egrunin
Your characterization is like accusing someone who is looking for an online encyclopedia of assuming that information he finds on Wikipedia can be freely copied into a school assignment.Most code that you find on the Internet *is* free to copy. Under what terms you can use/distribute that code is another matter, but that's an entirely different conversation from asking where to find the code in the first place.I hope he'd include an attribution to the source as well, and I'd hope he respect the original author's licensing terms.
David M
Come on man, I don't just copy crap I find... and nowhere did I suggest that. I'm asking if there is a site where the pretense is, that you copy it and use it.
pinnacler
I agree with @David, @pinnacler. -1 for your comment @Aren.
Casebash
A: 

I'm not sure this question is language agnostic, but I use GitHub this way ;) Other languages may have places where this is possible.

Jakub Hampl
GitHub is language-agnostic, right? It's just a shared software repository, but you can put programs in any language there, as far as I know.
David Zaslavsky
@David Github has a feature that detects certain languages in a repository and lists them as such.
Chacha102
yes, GitHub is language-agnostic.
Timothy
+1  A: 

Cutting and pasting code you find on the internet into production code would be like chewing gum found in the street. - Mike Johnson

With that in mind, try sites that host opensource projects like github, codeplex, code.google.com, etc.

Timothy
I hate to say it, but I disagree with Mike Johnson's quote up there. You use libraries all the time. You don't care how they work, you just take the authors word that they do, and you assume they work efficiently. It's called abstraction. I don't want to have to reinvent the wheel every time I need to calculate a square root. I don't want to reinvent the wheel every time I need to do something a little more complicated either.
pinnacler
There's a big difference between using a library, and cutting and pasting code.
Nick Johnson
A: 

Safari Bookshelf from O'Reilly has many, many books that contain many implementations from which to choose.

http://my.safaribooksonline.com/

I was a subscriber for a few years before coming to my current job, where we have a corporate account! It's one of the best perks, and one of the best resources I have available. I haven't bought a computer book in years.

David M
+12  A: 

Firstly, two caveats:

  1. Copy and pasting code you don't understand is a bad idea. Make sure you understand exactly what the code does before you use it.
  2. Make sure you respect the license of the code you are copying. This is important!

Those caveats aside, it's often language dependent. Languages with an open development ethos (not just an open source implementation, think Python as compared to Java) tend to have official archives of open source libraries. For example:

Furthermore, don't forget to look in your languages standard library. Some modern languages have massive standard libraries, which have often contained the functionality I am looking for:

Non-openly developed languages often have non-official community archives. For example:

A third category of sites are language agnostic. They are often best search through POG (plain old-fashioned Google). For example:

Finally, a fourth category of sites that I find increasingly useful are source-code search engines:

You may also be able to find useful source code, or at least get help writing something, through various pastebins.

  • Pastebin is language-agnostic
  • HPaste is mostly Haskell, but has a little in other languages.

Often, at the end of the day it is easiest just to google it, though.

fmark
I've made this a wiki, so feel free to add your own favorites.
fmark
Copy pasting is one thing, importing an external lib and letting it do stuff is another ;)
Lo'oris
Absolutely. Sadly, I've come across many people who don't know the difference.
fmark
+3  A: 

There is a wiki that contains free code that anybody can edit and improve:

Rosetta Code.

As a means of an overview there is the "Solutions by Programming Task" page.


From the former page:

"Rosetta Code is a programming chrestomathy site. The idea is to present solutions to the same task in as many different languages as possible, to demonstrate how languages are similar and different, and to aid a person with a grounding in one approach to a problem in learning another."

Peter Mortensen