Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code reuse and refactoring [closed]

What's best practice for reuse of code versus copy/paste?

The problem with reuse can be that changing the reused code will affect many other pieces of functionality.

This is good & bad : good if the change is a bugfix or useful enhancement. Bad if other reusing code unexpectedly becomes broken because it relied on the old version (or the new version has a bug).

In some cases it would seem that copy/paste is better - each user of the pasted code has a private copy which it can customize without consequences.

Is there a best practice for this problem; does reuse require watertight unit tests?

like image 550
horace Avatar asked Jun 15 '09 13:06

horace


People also ask

Is a way to reuse once written code again and again?

Code reuse is the practice of using existing code for a new function or software. But in order to reuse code, that code needs to be high-quality. And that means it should be safe, secure, and reliable. Developing software that fulfills these requirements is a challenge.

When should refactoring not be done?

One should not start refactoring unless he has a clear purpose in mind. Once the purpose has been accomplished, one is done. There is probably not an explicit 10 point check list to tell you when you are done, but most people can determine if they are being productive or just playing.

When should you not reuse a code?

#1 When it trades off with simplicity Sometimes, on a good day, you will come up with code that is not reusable, but that is beautifully simple and elegant, making it easy to read and understand. If the only way to make it reusable is to render it perceptibly more complex, then it's usually best not to touch it.

What is code refactoring?

Refactoring is the process of restructuring code, while not changing its original functionality. The goal of refactoring is to improve internal code by making many small changes without altering the code's external behavior.


2 Answers

Every line of code has a cost.

Studies show that the cost is not linear with the number of lines of code, it's exponential.

Copy/paste programming is the most expensive way to reuse software.

"does reuse require watertight unit tests?"

No.

All code requires adequate unit tests. All code is a candidate for reuse.

like image 59
S.Lott Avatar answered Sep 28 '22 07:09

S.Lott


It seems to me that a piece of code that is used in multiple places that has the potential to change for one place and not for another place isn't following proper rules of scope. If the "same" method/class is needed by two different things to do two different functions, then that method/class should be split up.

Don't copy/paste. If it does turn out that you need to modify the code for one place, then you can extend it, possibly through inheritance, overloading, or if you must, copying and pasting. But don't start out by copy-pasting similar segments.

like image 24
Sean Avatar answered Sep 28 '22 05:09

Sean