4 * [Coding Style Guideline for C](#coding-style-guideline-for-c)
10 * [Coding Style Guideline for Javascript](#coding-style-guideline-for-javascript)
11 * Javascript Language Rules
12 * Javascript Style Rules
15 * [Coding Style Guideline for Python](#coding-style-guideline-for-python)
18 # Coding Style Guideline for C
20 Our coding style guideline is based on [google c++ coding standard](https://google.github.io/styleguide/cppguide.html),
21 but modified due to some difference between C and C++.
22 When this guideline is ambiguous, just follow the result of running `./tools/check_tidy.py`.
24 Here are `./tools/check_tidy.py` options:
26 --autoedit: Automatically edit the detected clang format errors. No diffs will be displayed.
30 Use [Validated Struct](../devs/Inside-IoT.js-Validated-Struct.md) whenever possible, for encapsulation and validity check.
35 Use #define guard in all header files. `<FILE>_H` format is recommended.
45 maximum 80 characters in a line.
48 2 space indent at a time. Do not use a tab for indentation.
50 ### Vertical whitespace
51 Add two blank lines between functions.
53 Otherwise minimize use of vertical whitespace.
55 This is more a principle than a rule: don't use blank lines when you don't have to. In particular, don't put more than two blank lines between functions, resist starting functions with a blank line, don't end functions with a blank line, and be discriminating with your use of blank lines inside functions.
58 Write a function call all in a line if it fits. If not, break the line into multiple lines after assignment operator, or insert newline between parameters.
59 Do not insert spaces after open paren and before close paren.
61 int value = foo(arg1, arg2, arg3);
64 foo(arg1, arg2, arg3);
66 int value = foo(arg1, arg2,
69 ### Function Declaration and Definition
70 Use named parameters in function declaration.
72 return_type function_name(int, char); // not allowed
73 return_type function_name(int arg1, char arg2); // Use this
75 Return type should be on the same line as function name and parameters if it fits. If not, break between them aligned with the first argument.
77 return_type function_name(int arg1,
80 If even first argument does not fit in a line, write it in a new line with 4 space indent.
82 return_type function_name(
86 The open curly brace should be at the same line. The close curly brace should be either at the same line as its open curly brace or at new line.
88 return_type function_name(int arg1, char arg2) { };
89 return_type function_name(int arg1, char arg2) {
92 return_type function_name(int arg1, char arg2)
98 Use a space between the if and open brace. Open brace on the same line as the if.
104 Short conditional statements may be written without braces.
109 ### Loops and Switches
110 Use a space between the switch and loops(for, while, do-while) and open brace. Open brace on the same line as the switch and loops.
116 Single loop body statement may be written without braces.
119 do_something(); // ok
121 do_something(); // ok
127 Use lower cases and underscore for struct names, and add prefix `iotjs_` and suffix `_t`.
134 Use lower cases and underscore for function names.
136 For constructors, destructor, and methods of validated struct `iotjs_mystruct_t`, use names starting with `iotjs_mystruct_*`.
137 Constructor function name should be either `iotjs_mystruct_create` or `iotjs_mystruct_initialize`,
138 depending on whether the constructor returns the instance as return value, or the constructor just initializes the instance passed by parameter.
142 } IOTJS_VALIDATED_STRUCT(iotjs_mystruct_t);
144 iotjs_mystruct_t iotjs_mystruct_create(); // Ok
145 iotjs_mystruct_t* iotjs_mystruct_create(); // Ok
146 void iotjs_mystruct_initialize(iotjs_mystruct_t*); // Ok
148 void iotjs_mystruct_destroy();
150 int iotjs_mystruct_method();
154 Use lower cases and underscore for variable names.
156 int lower_case_variable;
162 Use either // or /* */ style comments. However, // style is much prefered.
166 # Coding Style Guideline for Javascript
168 This coding standard is based on [google javascript coding standard](https://google.github.io/styleguide/javascriptguide.xml)
170 ## Javascript Language Rules
173 Always declare variable before use.
176 Always use semicolons.
178 ### Function Declaration in blocks
179 Do not declare functions within a block.
181 ### Wrapper objects of primitive types
182 Do not use wrapper objects for primitive types.
185 Do not use `with` statement.
187 ### Modifying prototypes of builtin objects
188 Do not modify prototypes of builtin objects
190 ## Javascript Style Rules
193 Use lowerCamelCase for varible names and function names.
196 function myFirstFunction {
200 Use UpperCamelCase for constructor names
202 function MyConstructorFunction(input) {
203 this.variable = input;
208 Follow C/C++ formatting above.
211 # Coding Style Guideline For Python
213 The coding conventions for Python code follows [PEP 8 - Style Guide for Python Code](https://www.python.org/dev/peps/pep-0008/)