I don't know whether this has been asked before but is it normal for devs to copy/paste code? I feel like half of the programming I do is copy pasting code. I mean, I know what I'm doing. I'm just lazy to type it out.

  • 6
    Copy pasting by it self is not wrong, its as always how you use it.

    If you repeat almost identical code your creating a maintenance nightmare.

    Same if you copy from the web without really understanding the code you copy, its also a security risk (not necessarily malicious code but possibly insecure or bug ridden).

    But if you need to write very similar code and refactoring for reuse is really not viable, sure use copy pasting.

    The same if you build different projects and copy old working code its mostly fine unless its often enough to build a library to include.

    But as I initially mentioned, its not copy/paste that is bad, its the what and why that is important.
  • 0
    @Voxera Definitely. Not denying that. I was just wondering if it was common copy-pastingšŸ˜…
  • 1
    Recently I’ve been copying and pasting my own code and editing if I have to, but I always try to understand any foreign code I’m copying that way I actually know why it works like that.
  • 1
    Ya as long as you understand it and rework names and document your doing it right.
  • 0
    Copy n Pasta is a tool.

    When you just create code duplication it's wrong.

    @Voxera described the rest perfectly.

    Especially in refactoring, CnP is very useful.
  • 0
    @TheAsianDev I would say that its quite common, to often in less than good ways ;) but I use it quite a lot, especially for names or small snippets.

    For example if I need two nested for loops I usually write the first, copy and paste and change variable name.

    Or in a switch, I copy and paste a number of case statements and just change the values.

    But larger blocks of code is in my opinion a warning signal that you might want to refactor instead.

    But its never a fixed line, making two nearly identical sections with a few changes might be the better option, but once you need four or more you should probably break it down to reduce duplicate code.

    Any duplication run the risk of having a bug fixed in only one instance, causing you to have to fix the same bug more than once.

    But sometimes refactoring can make the code so much more complex that you risk introducing bugs through it, and that is then the worse option.

    But I do think most developers use it.

    And often I might start by copy pasting and then refactor when I realize I am duplicating code and not just saving time ;)

    Because in my opinion, the refactoring is never about saving time now but in the future.

    Right now the fast option is almost always to duplicate and modify as needed, but 6 months down the line and possibly with a new dev, it can take 10 times the time to decipher exactly whats different between the duplicate blocks, especially if a bug got fixed in some of them, is it a bug that was only in just those, or did they forgot to fix all cases.

    Always think about how some one else will be maintaining the code, especially if it happens to be you :D
  • 0
    As long as you fully understand the code you’re copying and pasting, it’s fine in my opinion. Ex. tedious code segments that can be easily tweaked for your own use.
Add Comment