blob: 32e7b70331a7c653f27682cbedb93ed8e70958a9 [file] [log] [blame]
Ben Chanfad4a0b2012-04-18 15:49:59 -07001Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
mukesh agrawal102a7752011-07-28 14:57:14 -07002Use of this source code is governed by a BSD-style license that can be
3found in the LICENSE file.
4
5To keep the shill source code consistent, please follow the conventions below:
6
7- Use the Chromium Coding Style, as described at
8 http://www.chromium.org/developers/coding-style.
9
10 If you use Emacs, the Google C Style mode will help you with the formatting
11 aspects of style. (Chromium Style generally follows Google Style). Get the
12 Emacs mode at
13 http://google-styleguide.googlecode.com/svn/trunk/google-c-style.el
14
mukesh agrawal102a7752011-07-28 14:57:14 -070015- When working with DBus::Variant:
16 - Read data via the appropriate named method, rather than depending on
17 implicit conversion. E.g.,
18
mukesh agrawal6e277772011-09-29 15:04:23 -070019 ::DBus::Variant var;
mukesh agrawal102a7752011-07-28 14:57:14 -070020 int8 data = var.reader().get_byte();
21
22 rather than
23
mukesh agrawal6e277772011-09-29 15:04:23 -070024 ::DBus::Variant var;
mukesh agrawal102a7752011-07-28 14:57:14 -070025 int8 data = var;
26
27 RATIONALE: The explicit version is only marginally longer than the
28 implicit version, and does not require the reader to understand C++
29 conversion rules.
30
31 - Where there is no named method, call the appropriate cast operator
32 explicitly. E.g.
33
mukesh agrawal6e277772011-09-29 15:04:23 -070034 ::DBus::Variant var;
mukesh agrawal102a7752011-07-28 14:57:14 -070035 vector<unsigned int> data = var.operator vector<unsigned int>();
36
37 RATIONALE: Calling the cast operator explicitly avoids conflicts with
38 constructors that might also be used to make the conversion. It also
39 avoids requiring that the reader understand C++ conversion rules.
40
mukesh agrawal6e277772011-09-29 15:04:23 -070041 - Write data via the appropriate named method. E.g.,
42
43 ::DBus::Variant var;
44 int16_t data;
45 var.writer().append_int16(data);
46
47 rather than
48
49 ::DBus::Variant var;
50 int16_t data;
51 var.writer() << data;
52
53 RATIONALE: Similarly as for reading, the explicit version is only
54 marginally longer, and does not require the reader to understand
55 overload resolution.
56
57 - Where there is no named method, write by using the stream
58 insertion operator. E.g.
59
60 ::DBus::Variant var;
61 ::DBus::MessageIter writer;
62 map<string, string> data;
63 writer = var.writer();
64 writer << data;
65
66 RATIONALE: This case is somewhat unfortunate, because it's not as
67 clear as its analogue for reading. However, the alternative is to
68 duplicate the code of the stream insertion operator overloads.
69
70 Note that the writer can't be omitted. E.g.
71
72 ::DBus::Variant var;
73 map<string, string> data;
74 var.writer() << data;
75
mukesh agrawal15908392011-11-16 18:29:25 +000076 does not work. For an explanation of why the local variable
77 |writer| is needed, see the comment in
78 DBusAdaptor::ByteArraysToVariant.
mukesh agrawal6e277772011-09-29 15:04:23 -070079
mukesh agrawal102a7752011-07-28 14:57:14 -070080- When deferring work from a signal handler (e.g. a D-Bus callback) to
81 the event loop, name the deferred work function by adding "Task" to
82 the name of the function deferring the work. E.g.
83
84 void Modem::Init() {
85 dispatcher_->PostTask(task_factory_.NewRunnableMethod(&Modem::InitTask));
86 }
87
88 RATIONALE: The naming convention makes the relationship between the signal
89 handler and the task function obvious, at-a-glance.
Ben Chanfad4a0b2012-04-18 15:49:59 -070090
Ben Chan80326f32012-05-04 17:51:32 -070091- C++ exceptions are not allowed in the code. An exception to this rule is
92 that try-catch blocks may be used in various D-Bus proxy classes to handle
93 DBus::Error exceptions thrown by the D-Bus C++ code. C++ exceptions should
94 be caught by const reference in general.
95
Ben Chanfad4a0b2012-04-18 15:49:59 -070096- When adding verbose log messages for debug purposes, use the SLOG marco and
Christopher Wileyb691efd2012-08-09 13:51:51 -070097 its variants (see logging.h for details).
Ben Chanfad4a0b2012-04-18 15:49:59 -070098
99 - Choose the appropriate scope and verbose level for log messages. E.g.
100
101 SLOG(WiFi, 1) << message; // for WiFi related code
102
103 - Before defining a new scope, check if any existing scope defined in
104 scope_logger.h already fulfills the needs.
105
106 - To add a new scope:
107 1. Add a new value to the Scope enumerated type in scope_logger.h.
108 Keep the values sorted as instructed in the header file.
109 2. Add the corresponding scope name to the kScopeNames array in
110 scope_logger.cc.
111 3. Update the GetAllScopeNames test in scope_logger_unittest.cc.
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700112
113- When adding externally visible (i.e. via RPC) properties to an object,
114 make sure that a) its setter emits any change notification required by
115 Chrome, and that b) its setter properly handles no-op changes.
116
117 Test that the property changes are handled correctly by adding test
118 cases similar to those in CellularServiceTest.PropertyChanges, and
Paul Stewart6db7b242014-05-02 15:34:21 -0700119 CellularServiceTest.CustomSetterNoopChange.
120
121- When performing trivial iteration through a container, prefer using
122 range based for loops, preferably:
123
Paul Stewart8ae18742015-06-16 13:13:10 -0700124 for (const auto& element : container) {
Paul Stewart6db7b242014-05-02 15:34:21 -0700125
126 Remove "const" where necessary if the element will be modified during
127 the loop. Removal of the "const" and reference for trivial types is
128 allowed but not necessary.