views:

174

answers:

7

How do you find code that was intended to be reused if you do not know it exists? In a large code base with many library functions how do you know if the code you intend to write has already been written?

+7  A: 

I read the documentation. If there isn't any documentation, it's not reusable.

Reuse is not something that comes for free. For example, take a look at the C++ Stansdard library std::string class - do you think that you would be able to re-use this in your code if a lot of people had not gone to great to lengths to document how it works? You need to put the same kind of effort in to make your own code reusable.

anon
A: 

Check for documentation, if none exists, see if any of the code is easily separable. For example some of the code could be compiled into a stand alone library.

Besides those two options it comes down to good old fashion searching.

Präriewolf
A: 

If there is documentation of the code base - look there.

If there is no documentation and you still have to find your way around a source browsing tool or a powerful text editor with source code analysis and navigation like SlickEdit is helpful. I would personally just ctag the source and use vim to navigate around, but this is a bit old-fashioned. Good source code navigation (finding callers for a function, displaying call graphs, etc) is very useful in finding your way in undocumented code.

And don't forget to document the findings, so the next guy after you does not have the same problem.

KIV
+1  A: 

Use a code browsing tool of your choice. Visual Studio, Eclipse and many others scan the source base and offer browsing capabilities. That allows you to follow interesting references to other classes or functions.

lothar
A: 

To make code properly reusable the granularity of reuse should be the same as the granularity of release. You should therefore be able to see from your code control system what components are being released and you should look to understand what each component does.

If code is intended for reuse and is not separately released then proceed with caution.

Howard May
+2  A: 

A great way to make code discoverable is to apply a clear, consistent naming system. For example, if I'm looking for a function which reverses the contents of a string, I'm likely to look for "ReverseString" (or "String.Reverse" in more OO systems). If someone has written such a function but called it "RevString" or "FastStrRev" or "gnirts" or whatever, I'm less likely to find it. Although that last one would be pretty funny.

As an aside, you should always be looking for opportunities to make code reusable (at least within a system). If you see something which nearly does what you need it to do, but not quite, refactor it so that all cases are catered for with the minimum of duplication. This helps prevent codesplosion and increases your StackOverflow rep.

Jim Arnold
A: 

Ask somebody else in the team - they might be able to tell you off the top of their head. Failing that, search the code or file names for something similar to what you're doing.
Then copy and paste the code :-)

I find that duplication of even simple functions is bad so any re-use you can make is good.

markh44