Programmers: please learn when to not use while loops

K&R style made more sense in the old days of 24-line text-only computer screens. It allowed programmers to see more of their code at a glance.

Nowadays, with GUIs, integrated development environments, high-resolution graphics screens and high-speed laser printers, this is no longer a major consideration.

And wouldn’t you know it, I’m surrounded by K&R-brace-style bigots at my current job. Some of them have even tried to make up and enforce a “coding style guide” where everyone has to use the K&R brace style. When the old Jargon File jokingly referred to K&R as “the one true brace style”, these bozos took it as gospel.

All right, K&R fanatics, if your brace style is so good, tell me quickly – at a glance – which of the following lines of code lies within the brace-delimited block, and which of them are part of the for statement:



for (pointerA = pointerB->OrphanAPointer();
    pointerA  &&  pointerA->GetName()  &&  pointerA->GetName()[0];
    pointerA = pointerB->GetNextAPointer(pointerA)) {
    ++ count;
    pointerA->AssumeANSIAttribution();
}

This thread is funny. I just got an A in my Intro to Programming and Problem Solving with C++ class.

On both the midterm and the final I wrote my program and then went back and did the problem solving. Everytime i do problem solving before I write my code I find that I was unable to forsee something and it was a waste of time anyway. But i guess if you can type 120 wpm you can get away with that stuff :slight_smile:

Just wanted to add that all you teachers dissin’ on your students, suck.

Well, you CAN indent and format the code better than that. If you had written it like this:



for (     pointerA = pointerB->OrphanAPointer();
          pointerA  &&  pointerA->GetName()  &&  pointerA->GetName()[0];
          pointerA = pointerB->GetNextAPointer(pointerA)) {
    ++ count;
     pointerA->AssumeANSIAttribution();
}


Then it would be pretty clear. Any code can be formatted to make it hard or easy to read. Althought I will admit that multi-line control statements can be a mess.
As for ‘kids’ doing it, I used to code exclusively using K&R style braces, and I wrote my first ‘C’ code in 1979. Never even saw the other style for probably a decade. I suspect the K&R style was popular at a time when monitors displayed 80 x 24 characters, and every line was precious when you were trying to follow code on a screen.

We had a lot of rules back then to prevent mistakes. For instance, when you can’t see the start of a block because the brace follows the control statement, you don’t do this:



  If (x == y)
    do_something();
  else
    do_something_else();


The rule for us when using KR formatting was that even for single line clauses, you enclose them in braces. First, because it only takes up one extra line instead of two it doesn’t pad your code much. And second, it helps catch those nasty bugs where someone adds a line thinking it’s inside a block.

Imagine you’re looking at a screen that can only display 24 lines. What’s more readable? This:



  if (x == y) {
      z=do_something();
      if (z) {
          do_the_other_thing();
      }
  }
  else {
       z=do_something_else();
      if (z)  {
          do_yet_another_thing();
      }
  }


or



  if (x == y) 
  {
      Z=do_something();
      if (z) 
      {
          do_the_other_thing();
      }
  }
  else 
  {
    z=do_something_else();
    if (z)  
    {
        do_yet_another_thing();
    }
  }


The second construct fills up 2/3 of my screen. If there were a few more lines of code in there, I wouldn’t even be able to see the whole thing at once. The first construct saves me four lines of screen real-estate - something that was actually pretty valuable back then.

You can write good code either way, and enforcing certain standards can keep you out of the weeds. But I no longer make a big hairy thing out of coding standards, and neither does anyone else on my team. As long as people are consistent and don’t do blatantly stupid things, good enough. We have to work with so many different languages, legacy codebases, and different coding teams that trying to maintain a single standard is folly, so it’s better to just roll with it.

Nicely said. That’s the biggest point I try to make in terms of the readability and stylistic issues to my students. I have my preferred style for my own coding, but I basically try to get across to them – there are lots of styles, there’s no one that’s “right” or “wrong”, but whatever you do, code so it’s readable, and be consistent in what you do.

Gosh, this thread is getting downright civil. Are we still in the Pit? :wink:

I’m surprised that there isn’t already a coding style where you work. I was under the impression that standard coding styles were pretty much the default?