blob: f8565999d6e250f25d2f414074fcab86297bb2d2 [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 Wiberge1d464e2017-09-08 13:12:36112## **C**
Karl Wibergfb4e6772017-09-04 09:27:33113
114Theres a substantial chunk of legacy C code in WebRTC, and a lot of
115it is old enough that it violates the parts of the C++ style guide
116that also applies to C (naming etc.) for the simple reason that it
117pre-dates the use of the current C++ style guide for this code base.
118
119* If making small changes to C code, mimic the style of the
120 surrounding code.
121* If making large changes to C code, consider converting the whole
122 thing to C++ first.
Karl Wibergee3d7602017-09-04 12:46:47123
Karl Wiberge1d464e2017-09-08 13:12:36124## **Java**
Karl Wiberg241d7102017-09-08 13:07:15125
126WebRTC follows the [Google Java style guide][goog-java-style].
127
128[goog-java-style]: https://google.github.io/styleguide/javaguide.html
129
Karl Wiberge1d464e2017-09-08 13:12:36130## **Objective-C and Objective-C++**
Karl Wiberg241d7102017-09-08 13:07:15131
132WebRTC follows the
133[Chromium Objective-C and Objective-C++ style guide][chr-objc-style].
134
135[chr-objc-style]: https://chromium.googlesource.com/chromium/src/+/HEAD/styleguide/objective-c/objective-c.md
136
Karl Wiberge1d464e2017-09-08 13:12:36137## **Python**
Karl Wiberg241d7102017-09-08 13:07:15138
139WebRTC follows [Chromiums Python style][chr-py-style].
140
141[chr-py-style]: https://chromium.googlesource.com/chromium/src/+/HEAD/styleguide/styleguide.md#python
142
Karl Wiberge1d464e2017-09-08 13:12:36143## **Build files**
Karl Wibergee3d7602017-09-04 12:46:47144
Karl Wiberg91d0ab72017-09-07 15:05:31145The WebRTC build files are written in [GN][gn], and we follow
146the [Chromium GN style guide][chr-gn-style]. Additionally, there are
147some WebRTC-specific rules below; in case of conflict, they trump the
148Chromium style guide.
149
150[gn]: https://chromium.googlesource.com/chromium/src/tools/gn/
151[chr-gn-style]: https://chromium.googlesource.com/chromium/src/tools/gn/+/HEAD/docs/style_guide.md
152
Per Kjellandera7f2d842018-01-10 15:54:53153### <a name="webrtc-gn-templates"></a>WebRTC-specific GN templates
Karl Wiberg91d0ab72017-09-07 15:05:31154
155Use the following [GN templates][gn-templ] to ensure that all
156our [targets][gn-target] are built with the same configuration:
157
158instead of | use
159-----------------|---------------------
160`executable` | `rtc_executable`
161`shared_library` | `rtc_shared_library`
162`source_set` | `rtc_source_set`
163`static_library` | `rtc_static_library`
164`test` | `rtc_test`
165
166[gn-templ]: https://chromium.googlesource.com/chromium/src/tools/gn/+/HEAD/docs/language.md#Templates
167[gn-target]: https://chromium.googlesource.com/chromium/src/tools/gn/+/HEAD/docs/language.md#Targets
168
Per Kjellandera7f2d842018-01-10 15:54:53169### Target visibility and the native API
170
171The [WebRTC-specific GN templates](#webrtc-gn-templates) declare build
172targets whose default `visibility` allows all other targets in the
173WebRTC tree (and no targets outside the tree) to depend on them.
174
175Prefer to restrict the visibility if possible:
176
177* If a target is used by only one or a tiny number of other targets,
178 prefer to list them explicitly: `visibility = [ ":foo", ":bar" ]`
179* If a target is used only by targets in the same `BUILD.gn` file:
180 `visibility = [ ":*" ]`.
181
182Setting `visibility = [ "*" ]` means that targets outside the WebRTC
183tree can depend on this target; use this only for build targets whose
184headers are part of the [native API](native-api.md).
185
Karl Wibergee3d7602017-09-04 12:46:47186### Conditional compilation with the C preprocessor
187
188Avoid using the C preprocessor to conditionally enable or disable
Karl Wiberg91d0ab72017-09-07 15:05:31189pieces of code. But if you cant avoid it, introduce a GN variable,
Karl Wibergee3d7602017-09-04 12:46:47190and then set a preprocessor constant to either 0 or 1 in the build
191targets that need it:
192
193```
194if (apm_debug_dump) {
195 defines = [ "WEBRTC_APM_DEBUG_DUMP=1" ]
196} else {
197 defines = [ "WEBRTC_APM_DEBUG_DUMP=0" ]
198}
199```
200
201In the C, C++, or Objective-C files, use `#if` when testing the flag,
202not `#ifdef` or `#if defined()`:
203
204```
205#if WEBRTC_APM_DEBUG_DUMP
206// One way.
207#else
208// Or another.
209#endif
210```
211
212When combined with the `-Wundef` compiler option, this produces
213compile time warnings if preprocessor symbols are misspelled, or used
214without corresponding build rules to set them.