Question about programming habit

adamantine.me

Member
Oct 30, 2015
152
4
36
www.adamantine.me
For a specific situation - say you have a working method/ main method but need to apply that solution to a couple other tables/arrays/objects/whatevers. Basically when you're tempted to copy/paste code and change the variable names where needed. Do you just copy/pasta or actually retype it? I find if you try to save time by copy pasting, you end up with syntactically correct but semantically incorrect code which is often more time consuming to debug than just a few syntax errors.
 

KB

Diamond Member
Nov 8, 1999
5,401
386
126
If the code is reusable it should never be copied and pasted or retyped. Instead find a way to make it reusable with only one copy of the code. The best way to do this is with object inheritance. Have all objects that will use the code inherit from a class that contains the code.
Another way to do it would be to implement a single class containing the desired method. This method can takes a series of primitive parameters or object parameters (for example String tablename or Array) and act on the parameters in a standard way.
 

purbeast0

No Lifer
Sep 13, 2001
52,931
5,802
126
As KB Mentioned, copying code is a very bad habit. Either create a class that has the common functionality and extend from it, or create a "static" class with the method in it so that you can call it from anywhere. I put "static" in quotes because I basically am referring to a singleton class, and it's different how you create those based on the language/environment you are in.

I'm also not really following what you mean by saying the solution works for multiple things but you have to change variable names. I'd think if the same solution/logic can be reused, then you wouldn't want to change variable names since well, it's the same logic and solution for both. If it's a function that say does the same thing but you can pass in a "cat" or a "dog", then simply name the variable "animal". Make the names more generic to match all cases.
 

Red Squirrel

No Lifer
May 24, 2003
67,917
12,379
126
www.anyf.ca
If I feel myself doing something repetitive I always try to take a step back and look at an OOP way of doing it. Such as creating one or more classes or breaking down the task into reusable units. Really depends on how/if I anticipate the program to grow in size. Initially it can take longer and more effort to code proper OOP so if it's a quick app I know is going to not need to grow much I might cheat.

I will usually just copy and paste, and not actually rewrite it, in that case.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
I do copy-paste as a starting point sometimes. Usually when the logic is almost the same but not quite (so shared code won't work), and it is only needed in a couple of places. And yes, now and then that has gone horribly wrong

But one copy-paste fail out of 10-15+ was acceptable for the time saved, and so was not making class(es) to abstract out the common parts and then specializing -- that usually makes sense for ten uses, but not always for two. And the layers of OOP can add their own bugs.

I like to think I've built up a decent feel for the trade-offs after many years of doing this.
 

Cogman

Lifer
Sep 19, 2000
10,278
126
106
I'm with Dave on this. Once upon a time I was all for base classes, but they have bitten me. Then I turned to static classes, and they two have taken their toll.

I don't see copy and paste as a jump off point being the worst thing in the world. Heck, sometimes it is a necessity when you want just a small bit of functionality from a library and not the whole thing with all of its sub dependencies.

I find myself today relying heavily on dependency injection and composition to get work done. I've seen too many problems created by inheritance to want to touch it. I'm sure there are some good applications for it, but I find that often things that aught not be in the base class end up getting hoisted more for convenience then anything else.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |