How To Reuse Code In Object-Oriented Programming

REUSE GOOD IDEAS

To save development time, code reuse is often seen as a method of cutting project costs and shortening time to market, but it has several benefits of saving time.This study provides an arguably much needed real, industrially-based experiment regarding brainstorming. The results and observations suggest that having a guided approach to sourcing creative stimuli is very beneficial. However, the Sweeper tool using internally sourced stimuli provides a method that is useful and more a practical alternative, as it is based firmly in the domain of activity.For information technology-intensive businesses the code is expensive and time-consuming to develop and code to reuse is considered a fundamental productivity strategy.

Tips on writing reusable code

1. Keep the code DRY. Dry means “Don’t Repeat Yourself”:-

This is one of the main changes I have tried to bring in. Always try to eradicate duplication and if you find any then move remove the duplication to a relevant place. Sometimes this has to lead me to create Static Helper classes or sometimes move it to the class it makes the most sense to have it.

To save development time code reuse is often seen as a method of cutting project costs and shortening time to market, but it has several benefits of saving time.

2. Make a class/method do just one thing:-

This is along the lines of the advice of giving the class only one reason to change. This often means creating methods that other methods use but this helps to make the methods/classes simple and less coupled.

3. Write unit tests for your classes AND make it easy to test classes:-

Writing code that is easy to test is decoupled. If you write code and are thinking about writing a unit test for it then you tend to split up the code into smaller testable chunks.

4. Remove logic or main code away from any framework code:-

Following the rules above will help this.

5. Try to think more abstractly and use Interfaces and Abstract classes:-

Try to hide dependencies of code behind a more Generic interface/abstract class. The benefit this gives the code is it creates a flexible point in the code where you can then hide future changes behind.

6. Code for an extension.

This is particularly true with the above point. If you write code that uses interfaces then you can extend that interface at a later point.

7. Don’t write code that isn’t needed:-

Do the simplest thing possible. Don’t waste your time adding methods and classes that might be used in the future. Keep the code simple and focused on what you are trying to deliver. I think I read/heard Josh Bloch say once that “if in doubt, leave it out”. Basically, who wants to write code that no one (including yourself) is going to use again.

8. Try to reduce coupling:-

When writing code thinks about the links and coupling the code is creating, does it need to be linked to those other classes.

9. Be more Modular:-

OOP is the standard approach to software engineering. when solving a problem, must identify the objects involved.

10. Write your code is an External API:-

Imagine the code you are writing is a self-contained component.
It wasn’t going to be ten until I got to 8 and then thought no one writes 8 tips, let us add two more on. It isn’t really a list but it’s sort of aims and mental notes I try to tell myself when writing code. They are more small bits of code I have written recently that has helped. I would like to hear people’s comments and especially their tips on writing reusable code.

Code Reuse

Code reuse

Code reuse is the use of existing software to deliver functionality. The following are common types of code reuse.

1.Off The Shelf

Using commercial or non-commercial products that exist as opposed to reinventing them.

2.Extension

Extending your current systems, applications, services, and libraries as opposed to doing something completely new.

3.Components

Designing systems and applications to maximize reuse by dividing functionality into useful components that hide complexity behind an interface.

4.Libraries

Packages of reusable components designed to be reused.

5.Services

Services reuse both the code and the operational aspects of a technology. For example, one team can operate a market data service for a bank as opposed to having 50 systems and applications that connect to market data feeds.

6.Platforms & Frameworks

Environments for developing functionality that may include a large number of APIs offered as libraries or services.

7.Cut & Paste

It is common for developers to copy code with which they are familiar and change it to be something new.

8.Forking

Officially taking the code and changing it to be something new by creating a fork in a version control system.

The Benefits and Costs of Code Reuse

  • It allows multiple developers to integrate IP into their code using similar styles and programming conventions.
  • This saves time, improves the features in your program, and generally makes for more cost-effective programming.
  • Reusability is an important issue in software engineering for at least two major reasons.
  • The traditional approach tends to tread the mentioned “concepts” as “equal”, which creates some illusions in those who did not grasp the main idea.
  • Forgot to address one thing. “Overriding” is really an OOP concept. So-called “overloading” is not. The second concept does not deserve a separate name itself. This is one of the worst and confusing terms which misleads too many people in the whole industry.
  • Not using OOP using a pure OOP language may mean different approaches, but is possible when one uses classes formally, not leveraging OOP power in any way. How? One can use only static methods, never virtual ones, not even instance non-virtual methods (in some languages, static methods can be abstract/virtual, quite naturally).
  • There is no any late binding and/or polymorphism without, say, encapsulation and inheritance.
  • A compiler can distinguish them by profile inferred from the calling code.
  • Dont Use Generic Code Instead use small ,composable components.
  • Making Software to generic can make it too complicated to use.

Conclusion

Don’t give up on your efforts to write reusable code! This tip outlines three ways you can modify your existing code to increase its reusability.

Follow me!
Latest posts by Imran Saifi (see all)

Leave a Comment