blob: ca9bad2fc6c8a43c4bc94d31cc0351a25ae205ce [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
119### WebRTC-specific GN templates
120
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
Karl Wibergee3d7602017-09-04 12:46:47135### Conditional compilation with the C preprocessor
136
137Avoid using the C preprocessor to conditionally enable or disable
Karl Wiberg91d0ab72017-09-07 15:05:31138pieces of code. But if you cant avoid it, introduce a GN variable,
Karl Wibergee3d7602017-09-04 12:46:47139and then set a preprocessor constant to either 0 or 1 in the build
140targets that need it:
141
142```
143if (apm_debug_dump) {
144 defines = [ "WEBRTC_APM_DEBUG_DUMP=1" ]
145} else {
146 defines = [ "WEBRTC_APM_DEBUG_DUMP=0" ]
147}
148```
149
150In the C, C++, or Objective-C files, use `#if` when testing the flag,
151not `#ifdef` or `#if defined()`:
152
153```
154#if WEBRTC_APM_DEBUG_DUMP
155// One way.
156#else
157// Or another.
158#endif
159```
160
161When combined with the `-Wundef` compiler option, this produces
162compile time warnings if preprocessor symbols are misspelled, or used
163without corresponding build rules to set them.