Question

Which principles, code qualities, practices, aproaches, language or framework features help you to reuse effectively functions, classes etc in wider range of cases. All of the situations are interesting: either you can modify both implementation and interface of the code to enable/improve reuse, or only implementation, or nothing at all. The key indicators of effectiveness of reuse are (as for me):

  • how much it reduces effort for implementing and maintanance
  • quality of application does not degrades
  • how much complexity is reduced

(all comparing to reimplementing from lower level).

PS. If possible please specify one factor per answer with description how it helps in your case.

Was it helpful?

Solution

Test Driven Development. For code to be easily unit tested it should:

1) do one thing only

2) have as few dependencies as possible

3) often have those dependencies passed in (so that they can be mocked out)

By an amazing coincidence these factors also make for reusable code. Actually it is not coincidence - the best way to have reusable code it to ensure it is used by at least two callers as early as possible. Code created with TDD starts life with two parents - the code under construction and the unit tests, so it is being reused from the start.

TDD has many other advantages apart from reuse - it gives you automated tests for all your code, it acts as example documentation for how to use the code, and it makes refactoring safer. Writing code with TDD can take longer than writing code without tests, but you will often make it up by needing far less time debugging.

OTHER TIPS

It's a classic - low coupling and high cohesion. If a module or function performs one specific task and has few or no dependencies, it will be much more reusable (as it will fit into a larger variety of situations) than if it performs multiple tasks, has lots of side effects, requires other modules, etc.

The main factor is a human and his skills, experience, brain.
All the best practices are worthless if a person doesn't know how to apply them.

In order to be more specific and address the comment I would list following skills/personal attitudes that contribute to a code reusability:

  • Discipline (copy paste is easy, good reusability requires discipline)
  • Passion (you must WANT to make code reusable and be proud of it)
  • Vision of the project (you MUST see the big picture of the project, not only code statements)
  • Feeling of the code (you MUST be able to catch code smells, see practices and patterns that can help)
  • Only enabling Test-Driven-Development mind can assure code quality alongside reusability.

It is similar to becoming a good mathematician, scientist or artist. There are many practices that everybody can read and learn. But only those who can apply the practices can master their craft.

The main point is that the question of reusability is more about personality, rather than technicality.

Functions should have no side-effects, essentially. Don't use global variables - pass what a function needs in arguments, and pass function's output as return value

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top