Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++ code style

Name: Anonymous 2010-05-24 12:37

Hi, /prog/.
What is the most beloved syntax style here? KR? BSD? GNU, perhaps?

Name: Anonymous 2010-05-25 0:09

>>24
Okay. Most offensively ugly first:

  • Indenting public: et al. to the same level as other code in a class definition is awful, because they're fundamentally different from the rest of the content. Same applies for labels and case statements. I prefer writing these lines at the indentation level of the enclosing block, but anything except what you did would be an improvement to readability.
  • Spaces inside parentheses / brackets are stupid. You don't write like that in natural language. There's no need to write like that; it doesn't help things make more sense, it just makes them wider. (And if your code is so convoluted with nested parentheses and brackets, consider breaking it up and using a temporary variable or two.)
  • Writing a space on both sides of the asterisk when referring to a type is misleading and will confuse people. Incidentally, this ambiguity is one of those dumb things about C syntax that I hate, but we're stuck with it. I prefer the space before the asterisk, since that way it's consistent with dereferencing. Plus, that way int *a, *b is consistent, but I don't like declaring a bunch of variables on one line, either, so really which way you write it doesn't matter a whole ton. Consistency and clarity do, however, so the goal is to come up with something that communicates your intent, and stick with it, whatever it might be.
    This is one of my biggest annoyances with many reformatting tools: they tend not to actually read the code, so user-defined types end up being formatted incorrectly.
  • Omitting the final comma in a list is asymmetrical, and makes reordering the list or adding items more difficult. C permits it; take advantage of that. (This is one of my Haskell annoyances)
  • I'll assume that you created progtroll dynamically simply to show how you format pointer declarations, but if not, there really wasn't a reason to allocate and then deallocate one single instance of something unconditionally within the same function.
  • And getting totally into nitpicks about the code rather than the style, you should probably be calling srand exactly once, at the start of your program. (If you meant to have a different RNG for each instance of that class, you did it wrong.) I would also avoid the [4] in the array declaration, using [] instead. (Or does C++ disallow that for some insane reason? I'm not really a C++ programmer, I just use it when I have to)

And isn't endl part of std?

Well, those points aside, it is indented, and on top of that, fairly consistent, which is more than a good 90% of first-year CS students seem to do.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List