News for hackers. Stuff that matters.
Re:My Thoughts on “Bad Code” by gav on Friday March 17, 2023 @12:00AM (#35192919)
At my first programming job they were religious about the single `return' and followed a pattern that was bizarre and luckily I've not seen since--using a `retVal' variable declared at the top.

  int foo(int bar) {
    int retVal = -1;
    if (bar == 1) {
      retVal = 1;
    }
    if ((retVal % 2) == 0) {
      retVal = 2; 
    }
    return retVal;
   }
Re:My Thoughts on “Bad Code” by ehvatum on Friday March 17, 2023 @12:40AM (#35193208)
It might have improved in the last few years, but as of 2018, MSVC would not do named value return optimization for the case where all branches returned the same local variable, in my experience. There had to be just the one penultimate return statement, in order for NRVO to kick in.
Re:My Thoughts on “Bad Code” by gymbeaux on Friday March 17, 2023 @02:12PM (#35200738)
Most of my teammates are in Serbia and I see naming the return object "toReturn" a lot... so yes, the code reads "return toReturn;"
Re:My Thoughts on “Bad Code” by JohnFen on Friday March 17, 2023 @04:45PM (#35202678)
Yes, I've seen this a lot over the years.

Also, putting code inside a do ... while( false ) loop purely so you can use break as a sneaky goto to avoid deeply nested conditionals while technically adhering to the single return rule.

I think when you're starting to use control statements in bizarre ways like that, it's a good indication that maybe that's a case where breaking the "rules" is the best thing.