Skip to content

Commit 6089da1

Browse files
add coding_style.rst
Explain when using auto is acceptable
1 parent 369623f commit 6089da1

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

doc/src/dev/coding_style.rst

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
Use of `auto`
3+
=============
4+
5+
Use `auto` only when the type is long, complex, or hard to write explicitly.
6+
7+
Examples where `auto` is appropriate:
8+
9+
.. code-block:: cpp
10+
11+
auto it = container.begin(); // Iterator type is long and not helpful to spell out
12+
13+
// The return type is std::vector<std::vector<std::pair<int, float>>>
14+
auto matrix = generate_adjacency_matrix();
15+
16+
// Lambdas have unreadable and compiler-generated types — use auto for them
17+
auto add = [](int x, int y) { return x + y; };
18+
19+
20+
Avoid `auto` when the type is simple and clear:
21+
22+
.. code-block:: cpp
23+
24+
// Use type names when they are short and readable.
25+
for (RRNodeId node_id : device_ctx.rr_graph.nodes()) {
26+
t_rr_node_route_inf& node_inf = route_ctx.rr_node_route_inf[rr_id];
27+
}
28+
29+
int count = 0;
30+
std::string name = "example";
31+
std::vector<int> numbers = {1, 2, 3};
32+
33+
Avoid:
34+
35+
.. code-block:: cpp
36+
37+
auto count = 0; // Simple and obvious type
38+
auto name = std::string("x"); // Hides a short, clear type
39+
40+
for (auto node_id : device_ctx.rr_graph.nodes()) {
41+
// node_id is RRNodeId. Write it out for clarity.
42+
auto& node_inf = route_ctx.rr_node_route_inf[rr_id];
43+
// node_inf is t_rr_node_route_inf. Use the explicit type since it's simple and meaningful.
44+
}
45+
46+
Rationale: clear, explicit types help with readability and understanding. Avoid hiding simple types behind `auto`.

doc/src/dev/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ Developer Guide
1111
c_api_doc
1212
code_documentation
1313
tutorials/index
14+
coding_style
1415
../SUPPORT
1516
../LICENSE

0 commit comments

Comments
 (0)