blob: 50f45fc6e88d7cc365175630543526de55c89182 [file] [log] [blame] [view]
Karl Wibergbb821e22017-09-03 03:02:121# WebRTC coding style guide
2
Karl Wiberge1d464e2017-09-08 13:12:363## **General advice**
Karl Wiberg241d7102017-09-08 13:07:154
5Some older parts of the code violate the style guide in various ways.
6
7* If making small changes to such code, follow the style guide when
8 its reasonable to do so, but in matters of formatting etc., it is
9 often better to be consistent with the surrounding code.
10* If making large changes to such code, consider first cleaning it up
11 in a separate CL.
12
Karl Wiberge1d464e2017-09-08 13:12:3613## **C++**
Karl Wibergbb821e22017-09-03 03:02:1214
15WebRTC follows the [Chromium][chr-style] and [Google][goog-style] C++
Karl Wiberge1ed2412017-09-06 09:10:2316style guides. In cases where they conflict, the Chromium style guide
17trumps the Google style guide, and the rules in this file trump them
18both.
Karl Wibergbb821e22017-09-03 03:02:1219
Karl Wiberg91d0ab72017-09-07 15:05:3120[chr-style]: https://chromium.googlesource.com/chromium/src/+/HEAD/styleguide/c++/c++.md
Karl Wibergbb821e22017-09-03 03:02:1221[goog-style]: https://google.github.io/styleguide/cppguide.html
22
Karl Wiberg180d9922018-03-15 10:21:0523### C++ version
24
25WebRTC is written in C++11, but with some restrictions:
26
27* We only allow the subset of C++11 (language and library) in the
28 allowed section of [this Chromium page][chromium-cpp11].
29* We only allow the subset of C++11 that is also valid C++14;
30 otherwise, users would not be able to compile WebRTC in C++14 mode.
31
32[chromium-cpp11]: https://chromium-cpp.appspot.com/
33
Karl Wiberg08c5cb02018-03-14 11:23:1234### <a name="h-cc-pairs"></a>`.h` and `.cc` files come in pairs
35
36`.h` and `.cc` files should come in pairs, with the same name (except
37for the file type suffix), in the same directory, in the same build
38target.
39
40* If a declaration in `path/to/foo.h` has a definition in some `.cc`
41 file, it should be in `path/to/foo.cc`.
42* If a definition in `path/to/foo.cc` file has a declaration in some
43 `.h` file, it should be in `path/to/foo.h`.
44* Omit the `.cc` file if it would have been empty, but still list the
45 `.h` file in a build target.
46* Omit the `.h` file if it would have been empty. (This can happen
47 with unit test `.cc` files, and with `.cc` files that define
48 `main`.)
49
50This makes the source code easier to navigate and organize, and
51precludes some questionable build system practices such as having
52build targets that dont pull in definitions for everything they
53declare.
54
55[Examples and exceptions](style-guide/h-cc-pairs.md).
56
Karl Wiberge1ed2412017-09-06 09:10:2357### ArrayView
Karl Wibergbb821e22017-09-03 03:02:1258
Karl Wiberge1ed2412017-09-06 09:10:2359When passing an array of values to a function, use `rtc::ArrayView`
60whenever possiblethat is, whenever youre not passing ownership of
61the array, and dont allow the callee to change the array size.
62
63For example,
64
65instead of | use
66------------------------------------|---------------------
67`const std::vector<T>&` | `ArrayView<const T>`
68`const T* ptr, size_t num_elements` | `ArrayView<const T>`
69`T* ptr, size_t num_elements` | `ArrayView<T>`
70
Karl Wiberg3b4c5902018-03-14 11:27:5571See [the source](api/array_view.h) for more detailed docs.
Karl Wibergfb4e6772017-09-04 09:27:3372
Taylor Brandstetterefc5fbd2017-12-08 19:42:1573### sigslot
74
75sigslot is a lightweight library that adds a signal/slot language
76construct to C++, making it easy to implement the observer pattern
77with minimal boilerplate code.
78
79When adding a signal to a pure interface, **prefer to add a pure
80virtual method that returns a reference to a signal**:
81
82```
83sigslot::signal<int>& SignalFoo() = 0;
84```
85
86As opposed to making it a public member variable, as a lot of legacy
87code does:
88
89```
90sigslot::signal<int> SignalFoo;
91```
92
93The virtual method approach has the advantage that it keeps the
94interface stateless, and gives the subclass more flexibility in how it
95implements the signal. It may:
96
97* Have its own signal as a member variable.
98* Use a `sigslot::repeater`, to repeat a signal of another object:
99
100 ```
101 sigslot::repeater<int> foo_;
102 /* ... */
103 foo_.repeat(bar_.SignalFoo());
104 ```
105* Just return another object's signal directly, if the other object's
106 lifetime is the same as its own.
107
108 ```
109 sigslot::signal<int>& SignalFoo() { return bar_.SignalFoo(); }
110 ```
111
Karl Wiberg0d446252018-03-19 22:27:35112### std::bind
113
114Dont use `std::bind`there are pitfalls, and lambdas are almost as
115succinct and already familiar to modern C++ programmers.
116
117### std::function
118
119`std::function` is allowed, but remember that its not the right tool
120for every occasion. Prefer to use interfaces when that makes sense,
121and consider `rtc::FunctionView` for cases where the callee will not
122save the function object.
123
Taylor Brandstetter212a2062017-10-12 18:22:48124### Forward declarations
125
126WebRTC follows the [Google][goog-forward-declarations] C++ style guide
127with respect to forward declarations. In summary: avoid using forward
128declarations where possible; just `#include` the headers you need.
129
130[goog-forward-declarations]: https://google.github.io/styleguide/cppguide.html#Forward_Declarations
131
Karl Wiberge1d464e2017-09-08 13:12:36132## **C**
Karl Wibergfb4e6772017-09-04 09:27:33133
134Theres a substantial chunk of legacy C code in WebRTC, and a lot of
135it is old enough that it violates the parts of the C++ style guide
136that also applies to C (naming etc.) for the simple reason that it
137pre-dates the use of the current C++ style guide for this code base.
138
139* If making small changes to C code, mimic the style of the
140 surrounding code.
141* If making large changes to C code, consider converting the whole
142 thing to C++ first.
Karl Wibergee3d7602017-09-04 12:46:47143
Karl Wiberge1d464e2017-09-08 13:12:36144## **Java**
Karl Wiberg241d7102017-09-08 13:07:15145
146WebRTC follows the [Google Java style guide][goog-java-style].
147
148[goog-java-style]: https://google.github.io/styleguide/javaguide.html
149
Karl Wiberge1d464e2017-09-08 13:12:36150## **Objective-C and Objective-C++**
Karl Wiberg241d7102017-09-08 13:07:15151
152WebRTC follows the
153[Chromium Objective-C and Objective-C++ style guide][chr-objc-style].
154
155[chr-objc-style]: https://chromium.googlesource.com/chromium/src/+/HEAD/styleguide/objective-c/objective-c.md
156
Karl Wiberge1d464e2017-09-08 13:12:36157## **Python**
Karl Wiberg241d7102017-09-08 13:07:15158
159WebRTC follows [Chromiums Python style][chr-py-style].
160
161[chr-py-style]: https://chromium.googlesource.com/chromium/src/+/HEAD/styleguide/styleguide.md#python
162
Karl Wiberge1d464e2017-09-08 13:12:36163## **Build files**
Karl Wibergee3d7602017-09-04 12:46:47164
Karl Wiberg91d0ab72017-09-07 15:05:31165The WebRTC build files are written in [GN][gn], and we follow
166the [Chromium GN style guide][chr-gn-style]. Additionally, there are
167some WebRTC-specific rules below; in case of conflict, they trump the
168Chromium style guide.
169
170[gn]: https://chromium.googlesource.com/chromium/src/tools/gn/
171[chr-gn-style]: https://chromium.googlesource.com/chromium/src/tools/gn/+/HEAD/docs/style_guide.md
172
Per Kjellandera7f2d842018-01-10 15:54:53173### <a name="webrtc-gn-templates"></a>WebRTC-specific GN templates
Karl Wiberg91d0ab72017-09-07 15:05:31174
175Use the following [GN templates][gn-templ] to ensure that all
176our [targets][gn-target] are built with the same configuration:
177
178instead of | use
179-----------------|---------------------
180`executable` | `rtc_executable`
181`shared_library` | `rtc_shared_library`
182`source_set` | `rtc_source_set`
183`static_library` | `rtc_static_library`
184`test` | `rtc_test`
185
186[gn-templ]: https://chromium.googlesource.com/chromium/src/tools/gn/+/HEAD/docs/language.md#Templates
187[gn-target]: https://chromium.googlesource.com/chromium/src/tools/gn/+/HEAD/docs/language.md#Targets
188
Per Kjellandera7f2d842018-01-10 15:54:53189### Target visibility and the native API
190
191The [WebRTC-specific GN templates](#webrtc-gn-templates) declare build
192targets whose default `visibility` allows all other targets in the
193WebRTC tree (and no targets outside the tree) to depend on them.
194
195Prefer to restrict the visibility if possible:
196
197* If a target is used by only one or a tiny number of other targets,
198 prefer to list them explicitly: `visibility = [ ":foo", ":bar" ]`
199* If a target is used only by targets in the same `BUILD.gn` file:
200 `visibility = [ ":*" ]`.
201
202Setting `visibility = [ "*" ]` means that targets outside the WebRTC
203tree can depend on this target; use this only for build targets whose
204headers are part of the [native API](native-api.md).
205
Karl Wibergee3d7602017-09-04 12:46:47206### Conditional compilation with the C preprocessor
207
208Avoid using the C preprocessor to conditionally enable or disable
Karl Wiberg91d0ab72017-09-07 15:05:31209pieces of code. But if you cant avoid it, introduce a GN variable,
Karl Wibergee3d7602017-09-04 12:46:47210and then set a preprocessor constant to either 0 or 1 in the build
211targets that need it:
212
213```
214if (apm_debug_dump) {
215 defines = [ "WEBRTC_APM_DEBUG_DUMP=1" ]
216} else {
217 defines = [ "WEBRTC_APM_DEBUG_DUMP=0" ]
218}
219```
220
221In the C, C++, or Objective-C files, use `#if` when testing the flag,
222not `#ifdef` or `#if defined()`:
223
224```
225#if WEBRTC_APM_DEBUG_DUMP
226// One way.
227#else
228// Or another.
229#endif
230```
231
232When combined with the `-Wundef` compiler option, this produces
233compile time warnings if preprocessor symbols are misspelled, or used
234without corresponding build rules to set them.