News for hackers. Stuff that matters.
Re:The bunny theory of code by rlpb on Monday May 18, 2015 @06:06AM (#9563556)
Copy and paste code out of the same repository, modifying it slightly? Surely the more appropriate thing to do in most cases it to make that code generic, available as a function, and then call it from the two places that now need it?

It sounds like it is the code review of future commits that needs to be fixed here.

Re:The bunny theory of code by juliangregorian on Monday May 18, 2015 @09:01AM (#9564146)
I copy and paste code all the time, but almost only for tests. I don't think tests should be abstracted too much. I don't appreciate having to work to understand what a test is doing, and when the test gets too long it's a sign you're testing at the wrong level.
Re:The bunny theory of code by nothrabannosir on Monday May 18, 2015 @10:19AM (#9564568)
Additionally, one of the biggest problems with copying code is that it will break when the two versions get out of sync. Test code inherently protects you against this, by virtue of having 100% test coverage, itself.
Re:The bunny theory of code by rlpb on Monday May 18, 2015 @10:59AM (#9564805)
> Test code inherently protects you against this, by virtue of having 100% test coverage, itself.

Not necessarily. A false negative bug in one copy of a test will remain even when fixed in a different copy.

Re:The bunny theory of code by im3w1l on Monday May 18, 2015 @07:49AM (#9563857)
Ok, let me give you an example, where my instinct would be to copy and then modify:

We have a library that requires initialization, action, and then freeing of resources. Initialization involves creating and filling in a struct and/or passing parameter values to an initialization function. After that it is necessary to check for success/failure. That's for initializing the library. Then something similar is done to initialize an object we want to manipulate. I would begin with the copied code, replace the appropriate values in the initialization, replace the error handling, replace the action, and keep the freeing of resources as is.

Re:The bunny theory of code by mimog on Monday May 18, 2015 @08:24AM (#9563979)
You are violating DRY principle. Copied code, with minor variation, spread out through a codebase is, seriously, the root of all evil.
Re:The bunny theory of code by scott_s on Monday May 18, 2015 @11:15AM (#9564916)
If you find yourself repeating a pattern in many places, with slight variations in the values used, that's a good candidate for refactoring that pattern into an abstraction for that resources. In your example, I can easily imagine refactoring uses of that library being abstracted into a class or a set of functions so that interaction with the library only happens in one place.
Re:The bunny theory of code by brlewis on Monday May 18, 2015 @08:14AM (#9563942)
If you said what language you're working in, it might be easier for me to understand why you wouldn't use inheritance.
Re:The bunny theory of code by Gupie on Monday May 18, 2015 @08:46AM (#9564086)
An example that superficially looks like "cut and paste" programming occurs is actually implementing a new derived class. In the C++ case, you could:

1. Type out the new .h and cpp files from scratch.

2. Copy/rename the base class .h file, remove all non virtual functions and other text not needed for a derived class, copy/rename this new header creating a .cpp file, convert method declarations to definitions, and add method implementations.

3. Copy/renamed the .h and .cpp files for an existing derived class and replace the existing method implementations.

Option 3 is a reasonable choice and is an example of what article article discusses.

Re:The bunny theory of code by scott_s on Monday May 18, 2015 @11:21AM (#9564943)
Option 3 is a reasonable choice, but I don't think it's an example of what the article discusses.

The example you provided is a good one, because it's an instance where you can't write abstractions to help you in the language you're using. Defining new classes, their interfaces and members is part of the necessary scaffolding when writing in C++. (Let's pretend it's not possible to do this with the C preprocessor.)

But the first example in the article does not sound like your example. Rather, it sounds closer to im3w1l's example, where the programmer copied code for some particular functionality, not for scaffolding.

Re:The bunny theory of code by Gupie on Monday May 18, 2015 @12:37PM (#9565355)
I don't see im3w1l's example as copying code for some particular functionality, it's copying boilerplate code needed to use a particular library. First the library is initialised, passing in a number of parameters to an initialisation function. If the values of these parameters are the same in multiple instances in the same codebase then that is violating DRY, but if the parameters differ it's just the scaffolding that you are reusing.
Re:The bunny theory of code by scott_s on Monday May 18, 2015 @01:18PM (#9565628)
The difference, to me, is that it's scaffolding code that can be abstracted. You can't abstract writing a class - it's a primary activity in the language. Only macros can help you there - either the broken ones provided by the C preprocessor, or real ones provided by languages like Racket or Rust.

But boilerplate code that interacts with an external library is not a primary activity in the language. You can hide it behind a class or a family of functions.

Re:The bunny theory of code by im3w1l on Monday May 18, 2015 @12:07PM (#9565186)
Well consider the following c code. Demonstrates how to allocate memory on gpu, transfer data in and out, how to operate on the memory, and how to then free it. I could see myself copy pasting that to several places, but modified to perform different functions.

http://docs.nvidia.com/cuda/cublas/#example-code

edit: I guess if you write C++, you could protect the resources with RAII, and shave two lines of copy-pasted de-initialization.

Re:The bunny theory of code by scott_s on Monday May 18, 2015 @03:03PM (#9566229)
It can be quite a bit more than just two lines, particularly considering error handling. But when you start refactoring common patterns, you can often end up with higher level abstractions. In your example, Thrust is exactly that: https://developer.nvidia.com/Thrust
Re:The bunny theory of code by im3w1l on Monday May 18, 2015 @11:14PM (#9567988)
I'll proceed as if that was implemented on top of cuda, I have no idea if that is actually the case, but for the sake of argument.

That code does not initialize the library, instead the handle is stored as a global variable that is lazily initialized when using thrust methods. This makes it easier to use but has some drawbacks too. It can not use more than one card at once. It means every call will check if library is initialized safer but slower. Resources are not freed as soon as possible. A good trade off in the majority of cases.

A device vector is created and assigned a value in one call. Size is automatically tracked. Nice.

Errors are not handled. All code looks cleaner if you don't handle errors.