-12

I have read the Indentation style entry on Wikipedia, but it's not very clear.

For K&R style, it says:

each function has its opening brace at the next line on the same indentation level as its header

Multi-line blocks inside a function, however, have their opening braces at the same line as their respective control statements

For the 1TBS (OTBS) style, it says:

functions have their opening braces on the same line separated by a space

It didn't talk about classes at all, but the Java style did:

the opening brace is on the same line not only for the blocks inside a function, but also for class or method declarations

If I am following the 1TBS style, where would the opening brace be placed for a class definition?

I also did a lot of searching (I prefer obtaining an answer immediately, instead of asking a question and waiting a long time), but no result.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Míng
  • 2,500
  • 6
  • 32
  • 48
  • 2
    eslint with 1tbs formatting places brackets for classes on the same line. – BDL Aug 26 '22 at 11:30
  • 1
    If the question is "what does the Wikipedia article mean when it says X? Why did it not explain what happens in situation Y? What should happen in situation Y according to the Wikipedia article?", then that is a question for *the Talk page corresponding to that Wikipedia entry, on Wikipedia* - not here. If the question is "what does 1TBS say about classes?", then the answer is that it does not say anything about classes - because 1TBS is a style for C, which does not have them. – Karl Knechtel Aug 27 '22 at 23:12
  • That said, **the point** of 1TBS as compared to K&R is that 1TBS treats braces for functions the same way (Egyptian) as for `if` statements etc.; so the *natural* conclusion is to apply the same treatment for classes as well: put `{` on the same line. But **none of this actually matters**: deciding on the details of what this terminology means, does not help to write better code. It's only important to know the names of things that the compiler will complain about, or that will change what the code does. – Karl Knechtel Aug 27 '22 at 23:14

1 Answers1

6

There is unlikely to be an objective answer to this, as both the K&R and 1TBS styles emerged in the context of the C programming language, where there were no class definitions.

Since derivative languages of C that support classes (e.g., C++, Java, etc.) share most of the syntax, it is only natural that the brace-style conventions that emerged for C would be used in and adapted for these derived languages. But they would be precisely that—derivations and adaptations. This is why the Wikipedia article mentions "Java" as a "variant" of the K&R style.

There is no right place or wrong place to put the opening brace for a class definition, even when following K&R or 1TBS styles, since classes aren't part of either of those two styles. Inevitably, you'll be following some variant of those original styles that includes a convention for class definitions, and, in a variant, any convention you choose is valid.

Ultimately, it matters not. Write the code in the way you think is readable (if you're writing it from scratch) or in the way that conforms to the other existing code files in the project. Conventions are merely that: conventions. There's no objective answer, and it doesn't matter.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • 2
    On Meta, you argued that the question is not opinion-based (implicitly, since you judged it as on topic). I think this answer *explains why* it is: since the styles in question were designed for a language that doesn't include `class`, determining how to adapt them for a language that does, requires some interpretation. – Karl Knechtel Aug 27 '22 at 23:15