You're missing the point: repeated blocks of code (whether they are copy+pasted or typed out) are simply bad, not from a computing point of view, in terms of what instructions get executed when, but from a human point of view, in terms of how we read and comprehend code. It's dealing with a different aspect of computer programming, rather than the obvious 'what works, what's efficient', but that makes it more interesting, not less.
Aren't switch-case statements widely accepted in C++ and inherently repeated blocks of code? No one would tell you to not copy-paste and then edit just the different parts in a switch-case block.
And to support both our arguments -- that example first mentions copy-pasting being the likely cause of the error and then provides a rewritten example that doesn't use the same code repeatedly (your point).
But a switch-case statement is a counter-example that you would copy-paste code and isn't abstracted into variable array indexes.
Counter-counter-argument: switch-cases have more obvious differences in a (only potentially) multi-character string.
No, they're not inherently repeated blocks of code. You can repeat bits of code if you like, but for many common cases you have options.
1. both cases have identical code: just fall through from one to the next
case A:
case B:
// code for A and B
break;
2. one case's code is a prefix of the other: have the first case first, let it do its thing, then fall through to the second case
case A:
// code for A only
case B:
// code for A and B
break;
3. both cases have a shared suffix; have them separate, then use a goto to get to the suffix part.
case A:
// code for A only
goto AB;
case B:
// code for B only
AB:
// code for A and B
break;
4. code is somehow parameterized by the switched expression - just reuse it in the code however you need to. This is the most flexible, of course, as it lets you do anything you like.
switch(x) {
case A:
case B:
printf("value is: %d\n",important_array[x]);
if(x==A) /* something */;
} else if(x==B) /* something else */;
Both of those statements can be true, though. Switch-case statements are widely accepted in industry, yet some developers disagree with their use. In my opinion, it's hard to beat a jump table for some operations, which a switch block neatly performs. And there's a rational continuum between boolean conditionals, multi-state (>2) conditionals, and polymophism, such that i think "some people"'s assertion sounds like throwing out the baby with the bathwater.