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?
3
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.
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.
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.
A good module system is important. Go’s interfaces (Java’s are similar, I hear) are pretty great.
I would add objects and inheritance. This will allow code to be reused by a child type.
3