Você está na página 1de 8

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 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


1. It allows multiple developers to integrate IP into their code using
similar styles and programming conventions.
2. This saves time, improves the features in your program, and
generally makes for more cost-effective programming.
3. Reusability is an important issue in software engineering for at
least two major reasons.
4. The traditional approach tends to tread the mentioned “concepts”
as “equal”, which creates some illusions in those who did not grasp
the main idea.
5. 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.
6. 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).
7.There is no any late binding and/or polymorphism without, say,
encapsulation and inheritance.
8. A compiler can distinguish them by profile inferred from the calling
code.
9. Dont Use Generic Code Instead use small ,composable
components.
10. 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.
Connect with source url:-
https://www.loginworks.com/blogs/reuse-code-object-oriented-
programming/

Você também pode gostar