blob: d8fbaae6d49e0ecba0f07c9a967955b93b566de0 [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 Wiberge1ed2412017-09-06 09:10:2323### ArrayView
Karl Wibergbb821e22017-09-03 03:02:1224
Karl Wiberge1ed2412017-09-06 09:10:2325When passing an array of values to a function, use `rtc::ArrayView`
26whenever possiblethat is, whenever youre not passing ownership of
27the array, and dont allow the callee to change the array size.
28
29For example,
30
31instead of | use
32------------------------------------|---------------------
33`const std::vector<T>&` | `ArrayView<const T>`
34`const T* ptr, size_t num_elements` | `ArrayView<const T>`
35`T* ptr, size_t num_elements` | `ArrayView<T>`
36
37See [the source](webrtc/api/array_view.h) for more detailed docs.
Karl Wibergfb4e6772017-09-04 09:27:3338
Taylor Brandstetterefc5fbd2017-12-08 19:42:1539### sigslot
40
41sigslot is a lightweight library that adds a signal/slot language
42construct to C++, making it easy to implement the observer pattern
43with minimal boilerplate code.
44
45When adding a signal to a pure interface, **prefer to add a pure
46virtual method that returns a reference to a signal**:
47
48```
49sigslot::signal<int>& SignalFoo() = 0;
50```
51
52As opposed to making it a public member variable, as a lot of legacy
53code does:
54
55```
56sigslot::signal<int> SignalFoo;
57```
58
59The virtual method approach has the advantage that it keeps the
60interface stateless, and gives the subclass more flexibility in how it
61implements the signal. It may:
62
63* Have its own signal as a member variable.
64* Use a `sigslot::repeater`, to repeat a signal of another object:
65
66 ```
67 sigslot::repeater<int> foo_;
68 /* ... */
69 foo_.repeat(bar_.SignalFoo());
70 ```
71* Just return another object's signal directly, if the other object's
72 lifetime is the same as its own.
73
74 ```
75 sigslot::signal<int>& SignalFoo() { return bar_.SignalFoo(); }
76 ```
77
Karl Wiberge1d464e2017-09-08 13:12:3678## **C**
Karl Wibergfb4e6772017-09-04 09:27:3379
80Theres a substantial chunk of legacy C code in WebRTC, and a lot of
81it is old enough that it violates the parts of the C++ style guide
82that also applies to C (naming etc.) for the simple reason that it
83pre-dates the use of the current C++ style guide for this code base.
84
85* If making small changes to C code, mimic the style of the
86 surrounding code.
87* If making large changes to C code, consider converting the whole
88 thing to C++ first.
Karl Wibergee3d7602017-09-04 12:46:4789
Karl Wiberge1d464e2017-09-08 13:12:3690## **Java**
Karl Wiberg241d7102017-09-08 13:07:1591
92WebRTC follows the [Google Java style guide][goog-java-style].
93
94[goog-java-style]: https://google.github.io/styleguide/javaguide.html
95
Karl Wiberge1d464e2017-09-08 13:12:3696## **Objective-C and Objective-C++**
Karl Wiberg241d7102017-09-08 13:07:1597
98WebRTC follows the
99[Chromium Objective-C and Objective-C++ style guide][chr-objc-style].
100
101[chr-objc-style]: https://chromium.googlesource.com/chromium/src/+/HEAD/styleguide/objective-c/objective-c.md
102
Karl Wiberge1d464e2017-09-08 13:12:36103## **Python**
Karl Wiberg241d7102017-09-08 13:07:15104
105WebRTC follows [Chromiums Python style][chr-py-style].
106
107[chr-py-style]: https://chromium.googlesource.com/chromium/src/+/HEAD/styleguide/styleguide.md#python
108
Karl Wiberge1d464e2017-09-08 13:12:36109## **Build files**
Karl Wibergee3d7602017-09-04 12:46:47110
Karl Wiberg91d0ab72017-09-07 15:05:31111The WebRTC build files are written in [GN][gn], and we follow
112the [Chromium GN style guide][chr-gn-style]. Additionally, there are
113some WebRTC-specific rules below; in case of conflict, they trump the
114Chromium style guide.
115
116[gn]: https://chromium.googlesource.com/chromium/src/tools/gn/
117[chr-gn-style]: https://chromium.googlesource.com/chromium/src/tools/gn/+/HEAD/docs/style_guide.md
118
Per Kjellandera7f2d842018-01-10 15:54:53119### <a name="webrtc-gn-templates"></a>WebRTC-specific GN templates
Karl Wiberg91d0ab72017-09-07 15:05:31120
121Use the following [GN templates][gn-templ] to ensure that all
122our [targets][gn-target] are built with the same configuration:
123
124instead of | use
125-----------------|---------------------
126`executable` | `rtc_executable`
127`shared_library` | `rtc_shared_library`
128`source_set` | `rtc_source_set`
129`static_library` | `rtc_static_library`
130`test` | `rtc_test`
131
132[gn-templ]: https://chromium.googlesource.com/chromium/src/tools/gn/+/HEAD/docs/language.md#Templates
133[gn-target]: https://chromium.googlesource.com/chromium/src/tools/gn/+/HEAD/docs/language.md#Targets
134
Per Kjellandera7f2d842018-01-10 15:54:53135### Target visibility and the native API
136
137The [WebRTC-specific GN templates](#webrtc-gn-templates) declare build
138targets whose default `visibility` allows all other targets in the
139WebRTC tree (and no targets outside the tree) to depend on them.
140
141Prefer to restrict the visibility if possible:
142
143* If a target is used by only one or a tiny number of other targets,
144 prefer to list them explicitly: `visibility = [ ":foo", ":bar" ]`
145* If a target is used only by targets in the same `BUILD.gn` file:
146 `visibility = [ ":*" ]`.
147
148Setting `visibility = [ "*" ]` means that targets outside the WebRTC
149tree can depend on this target; use this only for build targets whose
150headers are part of the [native API](native-api.md).
151
Karl Wibergee3d7602017-09-04 12:46:47152### Conditional compilation with the C preprocessor
153
154Avoid using the C preprocessor to conditionally enable or disable
Karl Wiberg91d0ab72017-09-07 15:05:31155pieces of code. But if you cant avoid it, introduce a GN variable,
Karl Wibergee3d7602017-09-04 12:46:47156and then set a preprocessor constant to either 0 or 1 in the build
157targets that need it:
158
159```
160if (apm_debug_dump) {
161 defines = [ "WEBRTC_APM_DEBUG_DUMP=1" ]
162} else {
163 defines = [ "WEBRTC_APM_DEBUG_DUMP=0" ]
164}
165```
166
167In the C, C++, or Objective-C files, use `#if` when testing the flag,
168not `#ifdef` or `#if defined()`:
169
170```
171#if WEBRTC_APM_DEBUG_DUMP
172// One way.
173#else
174// Or another.
175#endif
176```
177
178When combined with the `-Wundef` compiler option, this produces
179compile time warnings if preprocessor symbols are misspelled, or used
180without corresponding build rules to set them.