News for hackers. Stuff that matters.
Re:Recommendations about coding in C++ by jjallen on Sunday February 19, 2017 @10:34AM (#13680155)
Wow, the first example is simply a typo as a result of copy and pasting the same conditional block repeatedly. You would learn this would cause problems in your first week of programming: that what you type in your code determines whether or not it will perform correctly.

Shouldn't they focus on examples of unexpected or non-obvious mistakes (or at least start with those -- but then why add obvious examples at the end)?

Whether you copy and paste or simply type a single line incorrectly, of course if the wrong thing is entered the code will simply be incorrect.

Re:Recommendations about coding in C++ by malikNF on Sunday February 19, 2017 @10:53AM (#13680225)
Fail to see what your argument is here.

Of-course these are obvious errors once you are given 10-15 lines of code and told that there is an error there. Imagine having this code in the middle of say, several million loc.

Anyways the argument the original author is trying to make here is, someone did this to improve performance by "unrolling" the loop, it is a good example of how you shouldn't do things that a compiler might be able to for you, to try and prioritize making code human readable, since your compiler is smart enough to figure out these micro enhancements.

Re:Recommendations about coding in C++ by oneeyedpigeon on Sunday February 19, 2017 @10:52AM (#13680219)
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.
Re:Recommendations about coding in C++ by jjallen on Sunday February 19, 2017 @11:29AM (#13680432)
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.

Re:Recommendations about coding in C++ by to3m on Sunday February 19, 2017 @04:36PM (#13682146)
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 */;
Re:Recommendations about coding in C++ by ktRolster on Sunday February 19, 2017 @02:11PM (#13681320)
Aren't switch-case statements widely accepted in C++

No. Some people say they're a sign of a poorly architected object model.

Re:Recommendations about coding in C++ by ycombobreaker on Sunday February 19, 2017 @08:49PM (#13683599)
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.
Re:Recommendations about coding in C++ by andy_ppp on Sunday February 19, 2017 @03:22PM (#13681776)
Don't repeat yourself is often frowned upon when it makes the code substantially more complex of course. An art not a science in my opinion...
Re:Recommendations about coding in C++ by Coincoin on Sunday February 19, 2017 @12:52PM (#13680855)
Copy-paste errors are much more common than those once in a lifetime bugs nobody care about. Or are you telling me you never ever made a copy-paste error after your first week of programming?

They are one of the most pernicious error to find because the person reading is doing the exact same mistake as the writer, seeing the first instance is correct, they assume the rest is too.

Re:Recommendations about coding in C++ by Too on Monday February 20, 2017 @04:19AM (#13685763)
This is what I love about PVS studio, it actually catches real life bugs like this one. They may look simple when shown in the spotlight but they do happen and can easily go unavoided. Instead of most other analyzers(or maybe I should call them linters) that only complain when you do something that obviously depends on UB like casting a pointer to an int, or missing paranthesis vs operator precedence.
Re:Recommendations about coding in C++ by skookumchuck on Sunday February 19, 2017 @02:31PM (#13681446)
Copy-paste errors are easier to detect if the code is lined up properly. The first example would have been fairly obvious if each if-then statement were put on one line instead of two, and then all the clauses would line up. The error would then have stood out.