DEV Community

Muhammad Syuqri
Muhammad Syuqri

Posted on

How did you get out of the habit of copying/pasting code?

Like the title describes, I would like to know how you got out of the (bad) habit, particularly when it saves time and does the job.

Oldest comments (14)

Collapse
 
elmuerte profile image
Michiel Hendriks

I removed the Ctrl+V key.

Seriously though, copy/pasting code does not save time unless the thing you create is to be used once and thrown away.

Arguments like "saves time and does the job" are really bad. It conflicts with quality. If software is going to be used for other things and for a longer period (which is generally will) then quality is vital.

Collapse
 
dansyuqri profile image
Muhammad Syuqri

... be used once and thrown away.

May I know what you mean by the above statement?

I understand that quality is vital. I believe that the fastest way and ensuring that the software is functional first is more crucial especially when deadlines are due. But perhaps that's more of my newbie mindset kicking in. Still a lot to learn here :)

Collapse
 
elmuerte profile image
Michiel Hendriks

Prototypes, for example, are used once and thrown away. You use it to prove something is possible (or not), then you throw it away to make a actual solution. Other examples are code/scripts to perform a one-off automation of a task.

It is software which you are not going to maintain. Because it will no longer exist after it has been used.

Collapse
 
moopet profile image
Ben Sinclair

I don't think I ever got into the habit, except maybe when I was first learning to use a computer and copying code from magazines and books, which doesn't really count.

If I ever copy code from a Stack Overflow answer, I'll invariably have to rewrite it to fit the purpose, or more likely I'll take some sort of inspiration for it and write something more appropriate. Copying more than a line or two at a time seems like a weird idea to me. I'd spend more time trying to figure out why it didn't work than I saved.

Collapse
 
nateous profile image
Nate

Copying a pattern isn't bad.

Just improve your typing skills by typing it all out manually again.

This will do two things I can think of right now.

First it gives you a chance to rename things appropriately, otherwise you tend to forget things and then the code doesn't make sense later.

Second, while you type it out you might think to yourself, gee, all I am changing is this or that, perhaps I could make a common view or function that both use and then you will have dryer code!

I think there are more benefits but these are the two that really stick out to me.

Collapse
 
dansyuqri profile image
Muhammad Syuqri

That does make sense. The reason why I thought copying is bad is because I have always thought that creating your own patterns and being able to code out solutions to existing problems are what defines a good developer.

But when you put it into those two perspectives, it does not sound bad at all. Rather, it contributes to the learning process.

Thanks for the input :D

Collapse
 
nateous profile image
Nate

Copying a good pattern isn't bad. I've been developing for over ten years and I still copy good patterns from others. Just make sure you understand what you are copying. Next time you'll be able to use that solution from memory! Or better yet, improve it for your specific need.

Collapse
 
conectado profile image
Conectado

I don't see how it is bad.

Collapse
 
kspeakman profile image
Kasey Speakman

Copy/paste is not a good design strategy, but it does have its place. Here is an explanation of the Ginger Cake pattern by Dan North. I can't explain it better than he did in the video. The whole talk is worth watching.

Collapse
 
dansyuqri profile image
Muhammad Syuqri

Wow I didn't know there is such a pattern! Thanks a lot for sharing! Learning something new everyday :D

Collapse
 
abandonedfridge profile image
Daniel Nielson

By appealing to my need to understand things. I just tell myself that I need to understand this thing better and I can usually talk myself into reworking the implementation a bit.

Collapse
 
31547 profile image
31547

i break down what the language or library is actually doing into small, byte (heh) sized terms i can understand, then i try to recreate everything piece by piece until i have the whole unit.

then i apply it to a bigger project.

for a while i struggled with ecmascript fat-arrow functions. these:

const foo = (arg) => {
//do something with arg
return arg;
};

what i did to understand was that i went into open-source code, codeacademy, freecodecamp, etc. code sources and looked at big code, for example, something copied and pasted from a codeacademy page:

const multiplyByNineFifths = (celsius) => {
  return celsius * (9/5);
};

const getFahrenheit = (celsius) => {
  return multiplyByNineFifths(celsius) + 32;
};

console.log('The temperature is ' + getFahrenheit(15) + '°F');
// Output: The temperature is 59°F

when i first saw this, i had some vague idea of where everything was, but i didnt know the specifics. thats what messes you up. always. the specifics. so i looked closely.

const multiplyByNineFifths = (celsius) => {
  return celsius * (9/5);
};

so, the function is first declared like a variable, with const at the start and a semicolon at the end. alright, ill think of this like a variable.

okay, so, after const theres a name, just like declaring a variable, as if i declared const multiplyByNineFifts = what-the-value-should-be.

its like a variable but dynamic. you can do things with this variable.

what i did after breaking it down like this and understanding it was that i tried converting normal functions into this syntax. for example:

var app = express.createServer();

app.get('/', function(req, res){
    res.send('Hello World');
});

app.listen(3000);

this is taken from the express 2.x start page, because im too lazy to convert the current fat-arrow back to this format.

i broke the function down. im calling get on app, which is just an alias for telling express to make a server. get takes parameters of a route, which is /, and then a function telling it what to do with this route being used.

so this is just like typing

function(req, res)
{
  res.send("hello world");
}

alright, let me convert this to fat-arrow syntax:

(req, res) => {
  res.send("hello world");
}

let me plug it back in, like with math:

app.get("/", (req, res) => {
  res.send("hello world");
});

then there was clarity because i took myself through what this whole thing was made of, how to remake it, and what every important part of it did, by thinking about it critically

Collapse
 
pratikaambani profile image
Pratik Ambani • Edited

Let me share my short and hilarious story of getting rid of copying/pasting code:

I haven't, and I won't.

WhyWouldI 😐

Why

Collapse
 
awilliams17 profile image
Austin Williams

I disagree that copying and pasting code is a bad thing.

There's copying and pasting code mindlessly, and there's doing it smart.

When I copy and paste something, I always make sure to check over it to make sure the quality is up to my standards (don't look at my recent project pls ;-;) and then I make sure it fits seamlessly into my project.

I think it's bad to rely too much on copying and pasting code, but for certain patterns or problems why reinvent the wheel?