Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What should a programming language have so that it can be said to promote good reusability? Only generics and functions come to my mind when I hear the word reusability. What else make a programming language good for writing easily reusable code?

share|improve this question

closed as not constructive by FrustratedWithFormsDesigner, user16764, GlenH7, Dan Pichelman, MichaelT Jun 12 '13 at 23:07

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

This could get rather subjective... – FrustratedWithFormsDesigner Jun 12 '13 at 20:02
This is a very broad question, and the answers can vary greatly with programming paradigm. Reusability will be very different between OO, functional, and procedural languages. – Ryan Kinal Jun 12 '13 at 20:12
Voting to close to give the question an opportunity to be refined and scoped. Will happily vote to re-open upon sufficient editing. – GlenH7 Jun 12 '13 at 21:22
up vote 2 down vote accepted

The first and foremost item would be a well defined unit of reuse and philosophy behind their implementation. When you look at the most successful reuse implementations you essentially have components or plugins. What makes those work are:

  • A well defined interface separating implementation and use
  • Well defined contracts for that interface
  • Consistent packaging (i.e. a way for the consumer of the component to discover what is available)

C# and Java provide the programming concept of an interface for just this purpose. Interfaces don't require all implementations to share an object hierarchy which helps protect proprietary code from changing a base class to provide more visibility into subclass composition. Many of the dependency injection frameworks from Spring(.Net) to MEF also define packaging requirements which complete the picture.

All of this can be done without an object oriented programming language as well. For example, C uses the extern keyword to define the functions that are exposed for applications to use, as well as structs to package data.

share|improve this answer

Namespaces are important to facilitate the ability for code to work together. When you have generic class or function names, you can very quickly run into collisions without namespaces.

If you are using an OO language, supporting interfaces and traits is superior to just supporting inheritance - it's easier to compose an object of other objects than it is to try and extend an object about which you know very little.

share|improve this answer

The first thing that comes to my mind is a good module system. To support re-use, the module system should:

  • be lightweight - a heavyweight module system will discourage use, which discourages re-use.
  • support versioning - if a client can ask for a specific version of another module, that will make it easier to independently evolve the different modules without breaking clients.
  • control namespace pollution - you don't want the module's internally-defined symbols to compete with those of your client code, so some for of controlled symbol export / import, or namespace definition, will be necessary.
share|improve this answer

A good module system is important. Go's interfaces (Java's are similar, I hear) are pretty great.

share|improve this answer

I would add objects and inheritance. This will allow code to be reused by a child type.

share|improve this answer
-1, that's one of the false myths about object orientation. – Doc Brown Jun 12 '13 at 20:18
"Composition over inheritance". That doesn't mean inheritance has no place (though some recent languages get along rather well without it), but it shouldn't be your go-to solution for code reuse. – delnan Jun 12 '13 at 20:19
OOP is by no means a requirement for code reuse. – Andres F. Jun 12 '13 at 21:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.