ArticleslgStudy

computer science

Code reuse

Code reuse is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Code reuse rather than just read about it. In short: Code reuse is the practice of using existing source code to develop software instead of writing new code. Software reuse is a broader term that implies using any existing software asset to develop software instead of developing it again.

Key takeaways

  • Code reuse belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Code reuse to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Code reuse from memory before moving on to harder problems.

Reference excerpt

Code reuse is the practice of using existing source code to develop software instead of writing new code. Software reuse is a broader term that implies using any existing software asset to develop software instead of developing it again. An asset that is relatively easy to reuse and offers significant value is considered to have high reusability. Code reuse may be achieved different ways depending on a complexity of a programming language chosen and range from a lower-level approaches like code copy-pasting (e.g. via snippets), simple functions (procedures or subroutines) or a bunch of objects or functions organized into modules (e.g. libraries) or custom namespaces, and packages, frameworks or software suites in higher-levels. Code reuse implies dependencies which can make code maintainability harder. At least one study found that code reuse at the class level reduces technical debt.

Overview

Ad hoc code reuse has been practiced from the earliest days of programming. Programmers have always reused sections of code, templates, functions, and procedures. Software reuse as a recognized area of study in software engineering, however, dates only from 1968 when Douglas McIlroy of Bell Laboratories proposed basing the software industry on reusable components. Code reuse aims to save time and resources and reduce redundancy by taking advantage of assets that have already been created in some form within the software product development process. The key idea in reuse is that parts of a computer program written at one time can be or should be used in the construction of other programs written at a later time. Code reuse may imply the creation of a separately maintained version of the reusable assets. While code is the most common resource selected for reuse, other assets generated during the development cycle may offer opportunities for reuse: software components, test suites, designs, documentation, and so on. The software library is a good example of code reuse. Programmers may decide to create internal abstractions so that certain parts of their program can be reused, or may create custom libraries for their own use. Some characteristics that make software more easily reusable are modularity, loose coupling, high cohesion, information hiding and separation of concerns. For newly written code to use a piece of existing code, some kind of interface, or means of communication, must be defined. These commonly include a "call" or use of a subroutine, object, class, or prototype. In organizations, such practices are formalized and standardized by domain engineering, also known as software product line engineering. The general practice of using a prior version of an extant program as a starting point for the next version, is also a form of code reuse. Some so-called code "reuse" involves simply copying some or all of the code from an existing program into a new one. While organizations can realize time to market benefits for a new product with this approach, they can subsequently be saddled with many of the same code duplication problems caused by cut and paste programming. Many researchers have worked to make reuse faster, easier, more systematic, and an integral part of the normal process of programming. These are some of the main goals behind the invention of object-oriented programming, which became one of the most common forms of formalized reuse. A somewhat later invention is generic programming. Another, newer means is to use software "generators", programs which can create new programs of a certain type, based on a set of parameters that users choose. Fields of study about such systems are generative programming and metaprogramming.

Types of reuse Concerning motivation and driving factors, reuse can be:

Opportunistic – While getting ready to begin a project, the team realizes that there are existing components that they can reuse. Planned – A team strategically designs components so that they'll be reusable in future projects. Reuse can be categorized further:

Internal reuse – A team reuses its own components. This may be a business decision, since the team may want to control a component critical to the project. External reuse – A team may choose to license a third-party component. Licensing a third-party component typically costs the team 1 to 20 percent of what it would cost to develop internally. The team must also consider the time it takes to find, learn and integrate the component. Concerning form or structure of reuse, code can be:

Referenced – The client code contains a reference to reused code, and thus they have distinct life cycles and can have distinct versions. Forked – The client code contains a local or private copy of the reused code, and thus they share a single life cycle and a single version. Fork-reuse is often discouraged because it's a form of code duplication, which requires that every bug is corrected in each copy, and enhancements made to reused code need to be manually merged in every copy or they become out-of-date. However, fork-reuse can have benefits such as isolation, flexibility to change the reused code, easier packaging, deployment and version management. The vendor branch approach mitigates some issues encountered in forking.

Systematic

Systematic software reuse is a strategy for increasing productivity and improving the quality of the software industry. Although it is simple in concept, successful software reuse implementation is difficult in practice. A reason put forward for this is the dependence of software reuse on the context in which it is implemented. Some problematic issues that need to be addressed related to systematic software reuse are:

a clear and well-defined product vision is an essential foundation to a software product line (SPL). an evolutionary implementation strategy would be a more pragmatic strategy for the company. there exist a need for continuous management support and leadership to ensure success. an appropriate organisational structure is needed to support SPL engineering. the change of mindset from a project-centric company to a product-oriented company is essential.

Examples

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Code reuse

Start with the simplest possible case. Write down what Code reuse claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Code reuse before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Code reuse ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Code reuse

In research
Code reuse appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Code reuse in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Code reuse is common in secondary-school and first-year university syllabi. It links to neighbouring topics Computer libraries, Programming principles, Repurposing, so understanding it makes those chapters shorter.
In everyday life
Look for Code reuse outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Code reuse” →

How to study Code reuse in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Code reuse means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Code reuse out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Code reuse in simple terms?

Code reuse is the practice of using existing source code to develop software instead of writing new code. Software reuse is a broader term that implies using any existing software asset to develop software instead of developing it again.

Why does Code reuse matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Code reuse?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Code reuse.

Tags

  • Computer libraries
  • Programming principles
  • Repurposing

Keep exploring