Factors that improve code reusability
Asked Answered
D

4

5

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.

Define answered 30/1, 2010 at 13:12 Comment(0)
P
3

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.

Precast answered 30/1, 2010 at 13:12 Comment(0)
M
5

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.

Monad answered 30/1, 2010 at 13:12 Comment(0)
C
3

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.

Caracas answered 30/1, 2010 at 13:12 Comment(2)
i think that it's obvious that code is authored and reused by humans in first place, thus their skills do matter. So please specify any concrete skill, experience or brain function that contributes to a code reusability. Something that could help someone. Downvoted for now: you post does not add any new information to the subject and even worse: it may demotivate a reader.Define
@Alexey, sorry for being to generic in my answer. I updated it to make more sense.Caracas
P
3

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.

Precast answered 30/1, 2010 at 13:12 Comment(0)
V
2

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

Valdavaldas answered 30/1, 2010 at 13:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.