1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Guest, PO has ceased our separate tiering and adopted Smogon tiers for SM. More information and updates here.

    Dismiss Notice

PO coding style and conventions

Discussion in 'Development General' started by Mystra, Nov 5, 2010.

  1. Mystra

    Mystra Active Member

    Joined:
    Jul 12, 2010
    Messages:
    1,389
    Likes Received:
    4
    • All code is ASCII only. UTF-8 if you can't help it (but nothing else).
    • Avoid C casts, prefer C++ casts (static_cast, const_cast, reinterpret_cast).
    • Use the constructor to cast simple types: int(myFloat) instead of (int)myFloat

    • 4 spaces are used for indentation
    • As a base rule, the left curly brace goes on the same line as the start of the statement:
      Code (cpp):
      1.  
      2. // Wrong
      3. if (codec)  
      4. {  
      5. }  
      6.  
      7. // Correct  
      8. if (codec) {  
      9. }
      10.  
    • Exception: Function implementations and class declarations always have the left brace on the start of a line:
      Code (cpp):
      1.  
      2. static void foo(int g)
      3. {  
      4.     qDebug("foo: %i", g);  
      5. }  
      6.  
      7. class Moo  
      8. {  
      9. };
      10.  
    • (this one I changed) ALWAYS use curly braces when the statement with a block takes more than 1 line.
      Code (cpp):
      1.  
      2. // Wrong
      3. // While it may be correct for a compiler it is harder to read.
      4. if (address.isEmpty())  
      5.     return false;  
      6.  
      7. for (int i = 0; i < 10; ++i)  
      8.     qDebug("%i", i);
      9.  
      10. // Correct
      11. if (address.isEmpty()) {  
      12.     return false;  
      13. }  
      14.  
      15. for (int i = 0; i < 10; ++i) {  
      16.     qDebug("%i", i);  
      17. }  
      18.  
      19. // Unless statement(s) is(are) very long you should consider using single-line form
      20.  
      21. if (address.isEmpty()) return false;  
      22.  
      23. for (int i = 0; i < 10; ++i) qDebug("%i", i);  
      24.  
    • Use parentheses to group expressions. Compiler has no problems identifying what should it do (even if it is not what you intended), for humans however it may not be so easy.
      Code (cpp):
      1.  
      2. // Wrong
      3. if (a && b || c)  
      4.  
      5. // Correct  
      6. if ((a && b) || c)  
      7.  
      8. // Wrong  
      9. a + b & c  
      10.  
      11. // Correct  
      12. (a + b) & c  
      13.  
    • Every case must have a break (or return) statement at the end or a comment to indicate that there’s intentionally no break.
    • Commas go at the end of a broken line; operators start at the beginning of the new line. The operator is at the end of the line to avoid having to scroll if your editor is too narrow.
      Code (cpp):
      1.  
      2. // Correct
      3. if (longExpression  
      4.     + otherLongExpression  
      5.     + otherOtherLongExpression) {  
      6. }  
      7.  
      8. // Wrong  
      9. if (longExpression +  
      10.     otherLongExpression +  
      11.     otherOtherLongExpression) {  
      12. }  
      13.  


    Also "Compiler/Platform specific issues" in http://qt.gitorious.org/qt/pages/CodingConventions has these things to say:

     
  2. coyotte508

    coyotte508 Well-Known Member Administrator Server Owner Administrator Server Owner

    Joined:
    Apr 21, 2010
    Messages:
    6,363
    Likes Received:
    167
    I prefer

    Code (text):
    1. if (xx)
    2.     return abc;
    to

    Code (text):
    1. if (xx) return abc;
    a lot. Can I still use it?
     
  3. Mystra

    Mystra Active Member

    Joined:
    Jul 12, 2010
    Messages:
    1,389
    Likes Received:
    4
    Can you insert { } and use something like

    Code (cpp):
    1.  
    2. if (xx) {
    3.     return abc;
    4. }
    5.  
    ? It will also help later if you need to insert more code inside this if without creating {} manually. There will be no need to someone else to guess whether you mean it or made a mistake and forgot {} either.
     
  4. coyotte508

    coyotte508 Well-Known Member Administrator Server Owner Administrator Server Owner

    Joined:
    Apr 21, 2010
    Messages:
    6,363
    Likes Received:
    167
    using '}' means one line more... The indent alone should make it readable.
     
  5. Mystra

    Mystra Active Member

    Joined:
    Jul 12, 2010
    Messages:
    1,389
    Likes Received:
    4
    Nobody counts our lines so it's not a problem (^ ^)

    You see, someone will add code later and if the said someone will make a slight mistake like this…

    Code (cpp):
    1.  
    2. if (xx)
    3.     somethingcool(a, b, c, d, e, f);
    4.     iamanotherfunctionbutimwild(z, x, c, v, b);
    5.  
    The third one will be left to wonder whether 2 lines were supposed to be in the if() block or not. I don't think we actually need that. As weird as it sounds it happens (~_~)
     
  6. coyotte508

    coyotte508 Well-Known Member Administrator Server Owner Administrator Server Owner

    Joined:
    Apr 21, 2010
    Messages:
    6,363
    Likes Received:
    167
    when i add a line i always add {}... But ok...
     
  7. Mystra

    Mystra Active Member

    Joined:
    Jul 12, 2010
    Messages:
    1,389
    Likes Received:
    4
    Well, you do. But who knows what others can do… you never know (O.o) Well, that's my line of reasoning anyway (^ ^)
     
  8. En-Cu-Kou

    En-Cu-Kou New Member

    Joined:
    Jan 4, 2011
    Messages:
    16
    Likes Received:
    0
    I have my editor set up for a different number of spaces per tab than all of you. Until all the tabs are replaced by spaces, indentation can get very confusing for people like me :)

    Anyway: Do structs, enums and namespaces get the left brace on the start of a line, like classes and functions?
     
  9. coyotte508

    coyotte508 Well-Known Member Administrator Server Owner Administrator Server Owner

    Joined:
    Apr 21, 2010
    Messages:
    6,363
    Likes Received:
    167
    It depends on the size inside honestly...

    For functions it's on the start of a line except if it's in the class declaration.
     
  10. Blastcore

    Blastcore Developer

    Joined:
    Jul 19, 2010
    Messages:
    763
    Likes Received:
    1

    Well, in my opinion, There aren't a lot of ways to do this. :P

    I usually do

    Code (cpp):
    1.  
    2. if() {
    3. //Everything else
    4. }
    5.  
    But is there any difference of doing:

    Code (cpp):
    1.  
    2. if()
    3. {
    4. //Everything else
    5. }
    6.  
    ?
     
  11. Mystra

    Mystra Active Member

    Joined:
    Jul 12, 2010
    Messages:
    1,389
    Likes Received:
    4
    Since structs are pretty much classes they are the same. As for enums and namespaces I think they should follow them too (left brace on the start of a line).

    @Latios: there is no difference. It's just need to be consistent, so we all should use the same way.

    You don't need a lot of them to wreak havoc :}