3 ms·
It's frightening to see how many of these are errors which are solely created due to copy-pasting - they could have been avoided rather easily by a bit of think
by stinos 12y ago
It's frightening to see how many of these are errors which are solely created due to copy-pasting - they could have been avoided rather easily by a bit of thinking and applying common good practices (typically storing functionality in a function and/or using a loop would already fix like 90% of these, aka DRY), instead of the lazy but easy 'solution' of copy-pasting.
One of the first things a programming guru told me was basically: whenever you are ready to hit Crl-C/Ctrl-V (or Ctrl-D or whatever 'duplicate' is in your editor) you shall ask yourself if you are not going to violate the all-important DRY rule. Ignorant and stubborn as I was at the time being I was like 'pfffft what can be wrong with some copy-pasting'. As I learned through the years and is shown by this quiz: a whole lot obviously. It can not be stressed enough how important and widely applicable yet so simple DRY principle is. So as a call-out to all programmers who haven't learnt the habit yet: please think twice or more before even considering copy-pasting.
- overgard 12y agoYou're not wrong, per se, but C++'s memory model tends to make refactoring this sort of thing a lot more painful than needed. If you introduce function you have to spend a lot of time thinking about how do the variables copy over the stack, is this data structure safe to pass to a function? And do I pass by reference? And do you return values from the stack or in allocated memory or write into a passed reference? Or do you get really gross and write a macro? And so on, until you just think "fuck it, copy-paste". It's not an excuse, but languages definitely shape how you think, and C++ definitely encourages long functions and copy pasta.
- shadowfox 12y agoI don't disagree with you per se. But I think you may be overstating the case. Once you are a C++ programmer for a while, you start to discern "standard" patterns for this sort of thing and tend to automatically use them. (The patterns might depend on the C++ version and to an extent the code base in question; but it is common to have such helper function take a const ref as input, return non-primitive return values via a out-parameter etc). So while C++ might "encourage long functions", I think that may be the case only for newbies to the language. Once you have a some experience working with it, you no longer need to put that much thought in to every little decision.