arkcompiler_runtime_core/docs/coding-style.md
huangyu c658ccf319 Update runtime_core code
Issue: https://gitee.com/openharmony/arkcompiler_runtime_core/issues/I5G96F
Test: Test262 suit, ark unittest, rk3568 XTS, ark previewer demo

Signed-off-by: huangyu <huangyu76@huawei.com>
Change-Id: I3f63d129a07deaa27a390f556dcaa5651c098185
2022-07-17 10:20:32 +08:00

2.3 KiB
Raw Blame History

CodeStyle

Our CodeStyle based on google code style (you can get google config like this: clang-format -dump-config -style=Google). But we have some modifications:

  1. Indent: spaces 4. Line length: 120.

  2. Delete spaces before public/private/protected.

  3. All constants in UPPERCASE.

  4. Enums in uppercase Example:

    enum ShootingHand { LEFT, RIGHT };
    
  5. Unix/Linux line ending for all files.

  6. Same parameter names in Method definitions and declarations.

  7. No k prefix in constant names.

  8. No one-line if-clauses:

    if (x == kFoo) return new Foo();
    
  9. Do not use special naming for getters/setters (google allows this:

    int count() and void set_count(int count))
    
  10. Always explicitly mark fall through in switch … case. Google uses its own macro, we can agree on /* fallthrough */.

    switch (x) {
      case 41:  // No annotation needed here.
      case 43:
        if (dont_be_picky) {
          // Use this instead of or along with annotations in comments.
          /* fallthrough */
        } else {
          CloseButNoCigar();
          break;
        }
      case 42:
        DoSomethingSpecial();
        /* fallthrough */
      default:
        DoSomethingGeneric();
        break;
    }
    
  11. When a return statement is unreachable, but the language syntax requires it, mark it with something like return nullptr; /* unreachable */, or define UNREACHABLE as assert(0 && "Unreachable") and insert it before such return

  12. Use standard notices in comments (e.g. TODO:, NB!, no FIXME: allowed).

  13. Use standard flowerbox comments at the top of headers and translation units (agree on the format). Temporary you can use this:

     /**
    
  • Copyright (c) Huawei Technologies Co., Ltd. 2019-2021. All rights reserved. */
  1. switch and case on the same level For example:

    switch (ch) { case A: ... }

  2. Always put { } even if the body is one line: For example

    if (foo) {
        return 5;
    }
    
  3. Use maybe_unused attribute for unused vars/arguments.

    int foo3([[maybe_unused]] int bar) {
        // ...
    }
    

We are using clang-format and clang-tidy to check code style.