I'm sometimes guilty of copy-pasting, but I try to eliminate duplication wherever possible. The exception is when I have a function which calls several other functions and is very slow. Sometimes the contents of the sub-functions can be combined for speed, or the underlying SQL queries can be combined into fewer or just one.
Example:
In inventory management, the minimum quantity on hand for an item is equal to the quantity in your working reserve plus the quantity in safety stock. Safety stock is equal to half of the working reserve.
Function workingReserve(itemCode, someDate)
' Inside here is a looping structure that adds up the requirements for the next six weeks (because that's how long it takes between reorder and receipt).
End Function
Function safetyStock(itemCode, someDate)
safetyStock = workingReserve(itemCode, someDate) / 2
End Function
Function minimumOnHand(itemCode, someDate)
minimumOnHand = workingReserve(itemCode, someDate) + safetyStock(itemCode, someDate)
End Function
I apologize that this is written in VB, but it's from an Excel VBA function.
In effect, the workingReserve function is running twice on the same data. The performance can be improved by combining the business logic of the safetyStock() function in the minimumOnHand() function.
Function minimumOnHand(itemCode, someDate)
minimumOnHand = workingReserve(itemCode, someDate) * 1.5
End Function
In the real code, I have comments in the code explaining the business logic, but have omitted them here for brevity.