Imagine if developers didn't have access to Copy-Paste



  • I just found two similarly-named functions in our generally-decent codebase. They're in the same codefile and right next to each other and each one has about 25 lines of code. So I take a look at them to see what the difference is, and there is absolutely no difference. I do Find References on each and find that one is called about half the time and the other is called the rest of the time.

    What causes developers to do this? It's obvious it was copy-pasted since it's a perfect match, whitespace and all. Why would you create an exact copy and use that instead of using the existing function?

    My head hurts. I'm afraid to go digging through the SVN logs because I might find out that I'm TRWTF.


  • Discourse touched me in a no-no place

    @mott555 said:

    What causes developers to do this?
    Interruptions are generally the cause if it's not simple outright stupidity. i.e. copy/paste function, rename it, get interrupted, forget about needing to modify the body of the function and end up using both later on.



  • @mott555 said:

    What causes developers to do this? It's obvious it was copy-pasted since it's a perfect match, whitespace and all. Why would you create an exact copy and use that instead of using the existing function?

    Are you sure it's copied and pasted and not typed out by hand twice? Now that would be hilarious. :)

    Edit: Actually, come to think of it, maybe this was by design, so that if functionA() call fails, then the backup plan would be to run functionB(). Surely that would work!



  • @mott555 said:

    What causes developers to do this? It's obvious it was copy-pasted since it's a perfect match, whitespace and all. Why would you create an exact copy and use that instead of using the existing function?

    Because it gives you two chances to guess right if you can't remember the function name when you're writing some code that calls it, thus reducing potential bugs by 50%!




  • @DaveK said:

    Because it gives you two chances to guess right if you can't remember the function name when you're writing some code that calls it, thus reducing potential bugs by 50%!


    Ooo, I like that pattern. I'm going to start using that in my codebase: every class, method, and function will have a duplicate. Then I can turn off autocomplete and boost the overall performance of my IDE and code faster! Reduced bugs, and improved efficiency!



  • @dohpaz42 said:

    @DaveK said:
    Because it gives you two chances to guess right if you can't remember the function name when you're writing some code that calls it, thus reducing potential bugs by 50%!

     

    Ooo, I like that pattern. I'm going to start using that in my codebase: every class, method, and function will have a duplicate. Then I can turn off autocomplete and boost the overall performance of my IDE and code faster! Reduced bugs, and improved efficiency!

     

    Take it a step further and write one master function that calls every single function on every object in the heap. Then you only have to remember one function name!

     



  • @mott555 said:

    Take it a step further and write one master function that calls every single function on every object in the heap. Then you only have to remember one function name!

     

    Yeah, and I can make it 11 arguments long.... oh wait, that's been done before. Damn.



  • @dohpaz42 said:

    @mott555 said:

    Take it a step further and write one master function that calls every single function on every object in the heap. Then you only have to remember one function name!

     

    Yeah, and I can make it 11 arguments long.... oh wait, that's been done before. Damn.

    Better to just pass it a Object[] so you don't lose flexibility



  • @dohpaz42 said:

    @mott555 said:

    Take it a step further and write one master function that calls every single function on every object in the heap. Then you only have to remember one function name!

     

    Yeah, and I can make it 11 arguments long.... oh wait, that's been done before. Damn.

    Eleven arguments?  That's ridiculous.  It's not even funny.



  • @dohpaz4 said:

    Yeah, and I can make it 11 arguments long.... oh wait, that's been done before. Damn.

    I have lots of "functions" with 11 or even 100, 101, 110 or more arguments and never see it as a problem...



  • I have lots of "functions" with .... arguments
     

    For some reason this made me wonder if it would be possible to construct a language in which it would be possible for a function to have a negative number of arguments...perhaps stealing them from other functions?  Seems like it would be related to variable length argument lists somehow.



  • @too_many_usernames said:

    I have lots of "functions" with .... arguments
     

    For some reason this made me wonder if it would be possible to construct a language in which it would be possible for a function to have a negative number of arguments...perhaps stealing them from other functions?  Seems like it would be related to variable length argument lists somehow.

     

    So functions that put values on the stack instead of popping them off? I think it would basically just be multiple return values then.

     



  • @TheCPUWizard said:

    @dohpaz4 said:

    Yeah, and I can make it 11 arguments long.... oh wait, that's been done before. Damn.

    I have lots of "functions" with 11 or even 100, 101, 110 or more arguments and never see it as a problem...

    Okay, I'll byte: for some of those triple-digit functions, why? As in, I'm sure there is a Good Enough ™ reason that you're doing it that way (that's not the question), but what purposes do these types of functions perform?



  • @dohpaz42 said:

    @TheCPUWizard said:

    @dohpaz4 said:

    Yeah, and I can make it 11 arguments long.... oh wait, that's been done before. Damn.

    I have lots of "functions" with 11 or even 100, 101, 110 or more arguments and never see it as a problem...

    Okay, I'll byte: for some of those triple-digit functions, why? As in, I'm sure there is a Good Enough ™ reason that you're doing it that way (that's not the question), but what purposes do these types of functions perform?

    Surely you dont restrict yourself to only methods with 0 or 1 parameters????  [although 10 parameters would still be below the previously discussed size]



  • @dohpaz42 said:

    @TheCPUWizard said:

    @dohpaz4 said:

    Yeah, and I can make it 11 arguments long.... oh wait, that's been done before. Damn.

    I have lots of "functions" with 11 or even 100, 101, 110 or more arguments and never see it as a problem...

    Okay, I'll byte: for some of those triple-digit functions, why? As in, I'm sure there is a Good Enough ™ reason that you're doing it that way (that's not the question), but what purposes do these types of functions perform?

    Dude, is a binary joke



  • @serguey123 said:

    Dude, is a binary joke
     

    Wow I missed that too, looks like I need to double check over today's work tomorrow to catch dumb bugs.



  • @mott555 said:

    So functions that put values on the stack instead of popping them off? I think it would basically just be multiple return values then.
     

    Ehh... no. That would a function returning multiple values.

    I'm thining more something like where you had to pass in a negative number of arguments to work.  But that's kind of conceptually odd, because 'arguments' are objects, and I'm not sure how to pass in a negative argument.  Something like the function owes you an argument?

    I'll have to think about this more some other time...



  • Obviously, a function taking -3 arguments would steal the most recent 3 args off the stack when called. I.e. it would take the last 3 args of the calling function.



  • @superjer said:

    Obviously, a function taking -3 arguments would steal the most recent 3 args off the stack when called. I.e. it would take the last 3 args of the calling function.

    Or the first three in Pascal!

     



  • @locallunatic said:

    @serguey123 said:

    Dude, is a binary joke
     

    Wow I missed that too, looks like I need to double check over today's work tomorrow to catch dumb bugs.

     I was starting to fear I might actually have to explain it!

    Of course, there is the "other way" to avoid large number of parameter:

     Foo(Wrapper parameters) {} // Only one parameter

    class Wrapper
    {
    public object field1;
    public object field2;
    // Snip
    public object field98;
    dublic object field99;
    }



  • @serguey123 said:

    @dohpaz42 said:

    for some of those triple-digit functions, why? As in, I'm sure there is a Good Enough ™ reason that you're doing it that way (that's not the question), but what purposes do these types of functions perform?

    Dude, is a binary joke

    For suitably small values of "joke".

     



  • @mott555 said:

    I just found two similarly-named functions in our generally-decent codebase. They're in the same codefile and right next to each other and each one has about 25 lines of code. So I take a look at them to see what the difference is, and there is absolutely no difference. I do Find References on each and find that one is called about half the time and the other is called the rest of the time.

    What causes developers to do this? It's obvious it was copy-pasted since it's a perfect match, whitespace and all. Why would you create an exact copy and use that instead of using the existing function?

    My head hurts. I'm afraid to go digging through the SVN logs because I might find out that I'm TRWTF.

    I've got you beat. I have this situation all over the place in inherited code, but I found out why they did it. The reason is way more disturbing than the outcome. Examples:





    In the Employee "Data layer" class:


    public void GetEmployees(DataGridView g)
    {
        foreach (DataRow r in DBHelper.ExecuteProc("GetEmployees").Rows)
        {
            DataGridViewRow gr = g.Rows[g.Rows.Add()];
            gr.Cells[0].Value = r["FirstName"];
            gr.Cells[1].Value = r["LastName"];
        }
    }


    In another "Data Layer" class, this would show up...

    public void GetEmployees(DataGridView g)
    {
        foreach (DataRow r in DBHelper.ExecuteProc("GetEmployees").Rows)
        {
            DataGridViewRow gr = g.Rows[g.Rows.Add()];
            gr.Cells[0].Value = r["LastName"];
            gr.Cells[1].Value = r["FirstName"];
        }
    }


    Each data layer function works on exactly one UI element. If the same data needed to be loaded somewhere else, they would copy the function and tweak as necessary. I count 5 unique WTFs in 10 lines of code, 13 if you get to count each instance. It's 14 if I get to include the fact that this was found in a Visual SourceSafe repository.


  • @da Doctah said:

    @serguey123 said:

    Dude, is a binary joke

    For suitably small values of "joke".

     

    Zero or one, those are your only two choices!



  • @superjer said:

    Obviously, a function taking -3 arguments would steal the most recent 3 args off the stack when called. I.e. it would take the last 3 args of the calling function.
     

    No ... a function taking -3 arguments returns a function that needs 3 arguments (or a function needing one argument, returning a function needing 2 arguments, etc. ... you get my drift).
    That's what I'd understand as "owing" arguments.

    Now, a function taking a complex number of arguments ... we'd need a two-dimensional "stack", ie. a plane for that ... Hmm, is there prior art for something like this? I'm off to the patent office ...having n-dimensional argument "stacks" is surely something that's worth protecting.

    Or a function taking 0.354 arguments ... is that probabilistic? How about NAN arguments? Does that need a string description, ie. "two and a half"?


  • Discourse touched me in a no-no place

     

    Imagine if developers didn't have access to Copy-Paste

    This developer would never get anything done, because it would then become impossible to implement our "source control" system.

     

    Guys, we own TFS AND VSS. I know neither of them is spectacularly fucking awesome, but they're still better than this.



  • @serguey123 said:

    Dude, is a binary joke

    D'oh! Sigh. I should have known better.



  • @flop said:

    Now, a function taking a complex number of arguments ... we'd need a two-dimensional "stack", ie. a plane for that ... Hmm, is there prior art for something like this? I'm off to the patent office ...having n-dimensional argument "stacks" is surely something that's worth protecting.
     

    Eh, that's just passing in an array of data representing a 2D grid where one axis is mapped to the imaginary numbers.  To actually pass in an imaginary argument is conceptually more difficult.  There's still a lot of debate about if imaginary numbers can actually have a physical manifestation or if they are just a mathematical modeling artifact (yes, I know how imaginary numbers model true physical systems, but generally you have to turn everything back to "real" numbers to use them in a meaningful way in the physical universe).



  • @flop said:

    Now, a function taking a complex number of arguments ... we'd need a two-dimensional "stack", ie. a plane for that ... Hmm, is there prior art for something like this?

    Befunge is fairly close.




  • @too_many_usernames said:

    I have lots of "functions" with .... arguments
     

    For some reason this made me wonder if it would be possible to construct a language in which it would be possible for a function to have a negative number of arguments...perhaps stealing them from other functions?  Seems like it would be related to variable length argument lists somehow.


    That sounds like the sort of thing Intercal would do.



  • @mott555 said:

    I just found two similarly-named functions in our generally-decent codebase. They're in the same codefile and right next to each other and each one has about 25 lines of code. So I take a look at them to see what the difference is, and there is absolutely no difference. I do Find References on each and find that one is called about half the time and the other is called the rest of the time.

    What causes developers to do this?

    Were the functions called setColor and setColour?

     



  • @Zecc said:

    @mott555 said:

    I just found two similarly-named functions in our generally-decent codebase. They're in the same codefile and right next to each other and each one has about 25 lines of code. So I take a look at them to see what the difference is, and there is absolutely no difference. I do Find References on each and find that one is called about half the time and the other is called the rest of the time.

    What causes developers to do this?

    Were the functions called setColor and setColour?
    ZoomToItems and ZoomToGraphics. Not that that would mean much to you, but there you go.



  • @flop said:

    @superjer said:

    Obviously, a function taking -3 arguments would steal the most recent 3 args off the stack when called. I.e. it would take the last 3 args of the calling function.
     

    No ... a function taking -3 arguments returns a function that needs 3 arguments (or a function needing one argument, returning a function needing 2 arguments, etc. ... you get my drift).
    That's what I'd understand as "owing" arguments.

    Now, a function taking a complex number of arguments ... we'd need a two-dimensional "stack", ie. a plane for that ... Hmm, is there prior art for something like this? I'm off to the patent office ...having n-dimensional argument "stacks" is surely something that's worth protecting.

    Or a function taking 0.354 arguments ... is that probabilistic? How about NAN arguments? Does that need a string description, ie. "two and a half"?

    How about an argument list that is NP-hard to compute?



  • @zelmak said:

    @flop said:

    @superjer said:

    Obviously, a function taking -3 arguments would steal the most recent 3 args off the stack when called. I.e. it would take the last 3 args of the calling function.
     

    No ... a function taking -3 arguments returns a function that needs 3 arguments (or a function needing one argument, returning a function needing 2 arguments, etc. ... you get my drift).
    That's what I'd understand as "owing" arguments.

    How about an argument list that is NP-hard to compute?
    No, I think I solved the puzzle.

    Well, let's start easy ... a positive number of arguments get passed in, like this (Lisp syntax):

      (defun function-1 (arg1 arg2 arg3)
        ...)

    Negative means the opposite, so these have to be "invented" by the function. As it turns out, Lisp has a nice syntax for that, for some time already:

      (defun function-1 (arg1 arg2 arg3 &aux var-1 var-2 var-3)
        ...)

    What does that tell us? The "founding fathers" that blessed us with Lisp already knew about that.

    Incidentally, this view is confirmed by common practice ... the passed-in arguments get reached from the frame-pointer, while local variables have offsets of the opposite sign.

     

    So, with that solved, let's go on and find out what a fractional, complex, or higher-dimensional number of arguments might mean ...

    (About the latter I could offer the quick opinion that this means addressing of variables in higher (or lower, depending on your view) stack frames ... but that's something that I have to flesh out a bit.)



  • Oh God make it stop!



  • @blakeyrat said:

    Oh God make it stop!
    Any god in particular, or just someone who's hanging around doing nothing ... say, for example, how about the Oh God Of Hangovers?

    Or would you insist on Odin, who likes to go to floating parties in the sky with his big hammer, picking up girls with "The interesting thing about being an immortal god is ..."?



  • @flop said:

    @blakeyrat said:

    Oh God make it stop!
    Any god in particular, or just someone who's hanging around doing nothing ... say, for example, how about the Oh God Of Hangovers?

    Or would you insist on Odin, who likes to go to floating parties in the sky with his big hammer, picking up girls with "The interesting thing about being an immortal god is ..."?

     

    Just steer clear of that [url="http://en.wikipedia.org/wiki/Leda_and_the_Swan"]furry[/ul] Zeus.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.