DEV Community

James dengel
James dengel

Posted on

Why "copy, paste, fixed" needs to change to "copy, paste, why"

By "copy, paste, fixed" I mean copying a few lines you don't understand into your code that fixes an issue. I am going to present an argument why we need to change our approach to "copy, paste, why".

When we are young we learn by copying actions, my daughter will say:

"My baby needs to go to sleep Daddy!"

She has no idea why the baby needs to go to sleep (at 7 or 8 in the morning) but she is learning by copying our actions and talk by saying that our baby (her) needs to go to sleep.

When we are adults, we have more insight into learning, we can look at something and try to ascertain why something occurs. We know that we only really "need" to go to bed when we are tired, or when it's late and you know you have to wake up early, or it's good for us to have regular and undisturbed nights sleep.

Basically when we are young we learn by "copy, paste, fixed".

But when you are an adult you can "copy, paste, fixed" but sometimes this will lead us down the wrong path (Like putting the baby to bed at 8 in the morning)

Take for instance a common gotcha in Python 2.

UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 20: ordinal not in range(128)

You do a search as this is confusing and hit upon someone suggesting:

string.encode('utf-8')

You go whoo, problem fixed move on.

WAIT, WAIT, WAIT, WAIT

Try "copy, paste, why",:

We apply the fix and it works, now we ask why ?

5 minutes later:

"Oh all strings in python 2.7 are assumed by default to be ASCII, oh I was so naïve."

10 minutes later:

"Oh between Python 3 and Python 2 string are handled differently as this was causing confusion generally"

15 minutes later:

"Ok I didn't even know about the unicode type in python 2"

Obviously there are times when you don't have 15-20 minutes to spare for every possible issue, but get into the habit of asking why will make you a better developer.

Let me know if you try this approach rather than "copy paste fixed" and how you get on.

Top comments (0)