mirror of
https://github.com/openharmony/ark_runtime_core.git
synced 2026-07-18 10:18:07 -04:00
1aa56bf224
Signed-off-by: Ilya Trubachev <trubachev.ilya@huawei.com>
2.8 KiB
2.8 KiB
Coding Style
Our Coding Style based on google coding style (you can get google config like this: clang-format -dump-config -style=Google).
But we have some modifications:
- Indent: spaces 4. Line length: 120.
- Delete spaces before public/private/protected.
- All constants in UPPERCASE.
- Enums in UPPERCASE:
enum ShootingHand { LEFT, RIGHT }; - Unix/Linux line ending for all files.
- Same parameter names in Method definitions and declarations.
- No
kprefix in constant names. - No one-line if-clauses:
if (x == kFoo) return new Foo(); - Do not use special naming for getters/setters (google allows this:
int count() and void set_count(int count)) - Always explicitly mark fall through in switch … case. Google uses its own macro, and 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; } - 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 the return statement.
- Use standard flowerbox comments at the top of headers and translation units (agree on the format). Temporary you can use this:
/* * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ - Always put
caseon the next level ofswitch:switch (ch) { case ‘A’: ... } - Always put { } even if the body is one line:
if (foo) { return 5; } - Use
maybe_unusedattribute for unused vars/arguments:int foo3([[maybe_unused]] int bar) { // ... }
We are using clang-format and clang-tidy to check code style.