Skip to content

Commit cedcb13

Browse files
add Avoid Unnecessary Complexity and Group Related Data
1 parent 5765748 commit cedcb13

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

doc/src/dev/coding_style.rst

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,60 @@ Example:
262262
263263
264264
265+
Avoid Unnecessary Complexity
266+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
267+
268+
- Prefer easier-to-read language features and idioms.
269+
- Avoid overusing advanced C++ features (e.g., template metaprogramming, operator overloading, SFINAE) unless they are essential.
270+
- Write for clarity first; optimize for performance or conciseness only if needed and after measuring.
271+
272+
.. code-block:: cpp
273+
274+
// Prefer this
275+
std::vector<int> get_ids() const;
276+
277+
// Avoid this unless you truly need it
278+
template<typename T>
279+
auto&& get_ids() const && noexcept;
280+
281+
282+
- **Write short functions.** Functions should do one thing. Short functions are easier to understand, test, and reuse,
283+
and their purpose can be clearly described in a concise comment or documentation block.
284+
- **Limit function length.** If a function is growing too long or is difficult to describe in a sentence or two,
285+
consider splitting it into smaller helper functions.
286+
- **Favor simplicity.** Avoid clever or unnecessarily complex constructs. Code should be easy to read and maintain by others,
287+
not just the original author.
288+
- Before adding new functions, classes, or utilities, check the codebase and documentation to see if a similar utility already exists.
289+
- Reuse or extend existing routines instead of duplicating functionality. This reduces bugs and makes the codebase more maintainable.
290+
291+
292+
Group Related Data
293+
~~~~~~~~~~~~~~~~~~
294+
295+
- Group related data members into `structs` or `classes` rather than passing multiple related parameters separately.
296+
- Each data member and member function should be commented to explain its role, even in simple data structures.
297+
298+
.. code-block:: cpp
299+
300+
/**
301+
* @brief Data about a routing node.
302+
*/
303+
struct t_node_info {
304+
int id; ///< Unique identifier.
305+
float cost; ///< Routing cost.
306+
bool expanded; ///< True if this node has been expanded.
307+
};
308+
309+
// Instead of:
310+
void process_node(int node_id, float cost, bool expanded);
311+
312+
// Prefer:
313+
void process_node(const t_node_info& info);
314+
315+
.. note::
316+
317+
Organizing related data and routines in structs or classes with clear comments makes code easier to extend and understand.
318+
It also helps avoid errors from mismatched or misused arguments.
319+
320+
321+

0 commit comments

Comments
 (0)