1+ ---
2+ alwaysApply: true
3+ ---
4+
5+
6+ # Coding Style Rules
7+
8+ ## Documentation and Communication
9+ - **No diagrams**: Do not create Mermaid diagrams unless explicitly requested
10+ - **Concise summaries**: Keep explanations brief and focused
11+ - **No verbose documentation**: Avoid lengthy explanations of obvious concepts
12+
13+ ## Code Comments
14+ - **Minimal commenting**: Only comment tricky or non-obvious code
15+ - **No redundant comments**: Don't explain what the code obviously does
16+ - **Focus on why, not what**: When commenting, explain reasoning rather than mechanics
17+
18+ ## Code Duplication Elimination
19+ - **Use template metaprogramming**: Eliminate duplication with templates rather than copying similar classes
20+ - **Template parameters for behavior**: When applicable use bool template parameters to control const/non-const variants
21+ - **Type aliases for clean APIs**: Provide familiar names while using unified implementations
22+ - **Follow standard library patterns**: Use patterns like `std::conditional_t` and SFINAE for type safety
23+ - **Single source of truth**: Maintain logic in one place to reduce maintenance burden
24+
25+ ## Control Flow and Nesting
26+ - **Prefer early exits**: Use guard clauses and early returns to minimize nesting
27+ - **Avoid deep conditionals**: Keep the main logic at the top level rather than buried in if statements
28+ - **Handle edge cases first**: Deal with special cases early and return, leaving the main path unindented
29+ - **Linear flow**: Structure code so the primary logic flows top-to-bottom without deep nesting
30+
31+ ## Examples
32+
33+ ### ❌ Bad (over-commented):
34+ ```cpp
35+ // Increment the size member variable
36+ ++m_size;
37+
38+ // Loop through all fields
39+ for (size_t i = 0; i < num_fields; ++i) {
40+ // Calculate the field offset
41+ layout.field_offsets[i] = layout.total_size;
42+ }
43+ ```
44+
45+ ### ✅ Good (minimal comments):
46+ ```cpp
47+ ++m_size;
48+
49+ for (size_t i = 0; i < num_fields; ++i) {
50+ layout.field_offsets[i] = layout.total_size;
51+ layout.total_size += capacity * s_field_layout.element_sizes[i];
52+ // Align for optimal cache performance
53+ layout.total_size = (layout.total_size + CPU_CACHE_LINE_SIZE - 1) & ~(CPU_CACHE_LINE_SIZE - 1);
54+ }
55+ ```
56+
57+ ### ❌ Bad (duplicated iterator classes):
58+ ```cpp
59+ class my_iterator {
60+ T* current_;
61+ // ... 50 lines of iterator implementation
62+ };
63+
64+ class my_const_iterator {
65+ const T* current_;
66+ // ... 50 lines of nearly identical iterator implementation
67+ };
68+ ```
69+
70+ ### ✅ Good (unified template implementation):
71+ ```cpp
72+ template<bool IsConst>
73+ class my_iterator_impl {
74+ using pointer = std::conditional_t<IsConst, const T*, T*>;
75+ using reference = std::conditional_t<IsConst, const T&, T&>;
76+
77+ pointer current_;
78+ // ... single implementation that works for both cases
79+
80+ // Allow non-const to const conversion
81+ template<bool OtherIsConst, typename = std::enable_if_t<IsConst && !OtherIsConst>>
82+ my_iterator_impl(const my_iterator_impl<OtherIsConst>& other) : current_(other.current_) {}
83+ };
84+
85+ // Clean API through type aliases
86+ using my_iterator = my_iterator_impl<false>;
87+ using my_const_iterator = my_iterator_impl<true>;
88+ ```
89+
90+ ### ❌ Bad (deep nesting):
91+ ```cpp
92+ list_node(list_node&& other) noexcept
93+ {
94+ if (other.is_linked())
95+ {
96+ // Take over the other node's position in the list
97+ next_ = other.next_;
98+ prev_ = other.prev_;
99+
100+ // In a circular list, linked nodes are guaranteed to have valid pointers when the node is linked
101+ assert(next_ != nullptr && prev_ != nullptr);
102+ next_->prev_ = this;
103+ prev_->next_ = this;
104+
105+ // Leave other node in unlinked state
106+ other.next_ = nullptr;
107+ other.prev_ = nullptr;
108+ }
109+ }
110+ ```
111+
112+ ### ✅ Good (early exit, linear flow):
113+ ```cpp
114+ list_node(list_node&& other) noexcept
115+ {
116+ if (!other.is_linked())
117+ {
118+ return;
119+ }
120+
121+ // Take over the other node's position in the list
122+ next_ = other.next_;
123+ prev_ = other.prev_;
124+
125+ // In a circular list, linked nodes are guaranteed to have valid pointers when the node is linked
126+ assert(next_ != nullptr && prev_ != nullptr);
127+ next_->prev_ = this;
128+ prev_->next_ = this;
129+
130+ // Leave other node in unlinked state
131+ other.next_ = nullptr;
132+ other.prev_ = nullptr;
133+ }
134+ ```
135+ # Coding Style Rules
136+
137+ ## Documentation and Communication
138+ - **No diagrams**: Do not create Mermaid diagrams unless explicitly requested
139+ - **Concise summaries**: Keep explanations brief and focused
140+ - **No verbose documentation**: Avoid lengthy explanations of obvious concepts
141+
142+ ## Code Comments
143+ - **Minimal commenting**: Only comment tricky or non-obvious code
144+ - **No redundant comments**: Don't explain what the code obviously does
145+ - **Focus on why, not what**: When commenting, explain reasoning rather than mechanics
146+
147+ ## Code Duplication Elimination
148+ - **Use template metaprogramming**: Eliminate duplication with templates rather than copying similar classes
149+ - **Template parameters for behavior**: When applicable use bool template parameters to control const/non-const variants
150+ - **Type aliases for clean APIs**: Provide familiar names while using unified implementations
151+ - **Follow standard library patterns**: Use patterns like `std::conditional_t` and SFINAE for type safety
152+ - **Single source of truth**: Maintain logic in one place to reduce maintenance burden
153+
154+ ## Control Flow and Nesting
155+ - **Prefer early exits**: Use guard clauses and early returns to minimize nesting
156+ - **Avoid deep conditionals**: Keep the main logic at the top level rather than buried in if statements
157+ - **Handle edge cases first**: Deal with special cases early and return, leaving the main path unindented
158+ - **Linear flow**: Structure code so the primary logic flows top-to-bottom without deep nesting
159+
160+ ## Examples
161+
162+ ### ❌ Bad (over-commented):
163+ ```cpp
164+ // Increment the size member variable
165+ ++m_size;
166+
167+ // Loop through all fields
168+ for (size_t i = 0; i < num_fields; ++i) {
169+ // Calculate the field offset
170+ layout.field_offsets[i] = layout.total_size;
171+ }
172+ ```
173+
174+ ### ✅ Good (minimal comments):
175+ ```cpp
176+ ++m_size;
177+
178+ for (size_t i = 0; i < num_fields; ++i) {
179+ layout.field_offsets[i] = layout.total_size;
180+ layout.total_size += capacity * s_field_layout.element_sizes[i];
181+ // Align for optimal cache performance
182+ layout.total_size = (layout.total_size + CPU_CACHE_LINE_SIZE - 1) & ~(CPU_CACHE_LINE_SIZE - 1);
183+ }
184+ ```
185+
186+ ### ❌ Bad (duplicated iterator classes):
187+ ```cpp
188+ class my_iterator {
189+ T* current_;
190+ // ... 50 lines of iterator implementation
191+ };
192+
193+ class my_const_iterator {
194+ const T* current_;
195+ // ... 50 lines of nearly identical iterator implementation
196+ };
197+ ```
198+
199+ ### ✅ Good (unified template implementation):
200+ ```cpp
201+ template<bool IsConst>
202+ class my_iterator_impl {
203+ using pointer = std::conditional_t<IsConst, const T*, T*>;
204+ using reference = std::conditional_t<IsConst, const T&, T&>;
205+
206+ pointer current_;
207+ // ... single implementation that works for both cases
208+
209+ // Allow non-const to const conversion
210+ template<bool OtherIsConst, typename = std::enable_if_t<IsConst && !OtherIsConst>>
211+ my_iterator_impl(const my_iterator_impl<OtherIsConst>& other) : current_(other.current_) {}
212+ };
213+
214+ // Clean API through type aliases
215+ using my_iterator = my_iterator_impl<false>;
216+ using my_const_iterator = my_iterator_impl<true>;
217+ ```
218+
219+ ### ❌ Bad (deep nesting):
220+ ```cpp
221+ list_node(list_node&& other) noexcept
222+ {
223+ if (other.is_linked())
224+ {
225+ // Take over the other node's position in the list
226+ next_ = other.next_;
227+ prev_ = other.prev_;
228+
229+ // In a circular list, linked nodes are guaranteed to have valid pointers when the node is linked
230+ assert(next_ != nullptr && prev_ != nullptr);
231+ next_->prev_ = this;
232+ prev_->next_ = this;
233+
234+ // Leave other node in unlinked state
235+ other.next_ = nullptr;
236+ other.prev_ = nullptr;
237+ }
238+ }
239+ ```
240+
241+ ### ✅ Good (early exit, linear flow):
242+ ```cpp
243+ list_node(list_node&& other) noexcept
244+ {
245+ if (!other.is_linked())
246+ {
247+ return;
248+ }
249+
250+ // Take over the other node's position in the list
251+ next_ = other.next_;
252+ prev_ = other.prev_;
253+
254+ // In a circular list, linked nodes are guaranteed to have valid pointers when the node is linked
255+ assert(next_ != nullptr && prev_ != nullptr);
256+ next_->prev_ = this;
257+ prev_->next_ = this;
258+
259+ // Leave other node in unlinked state
260+ other.next_ = nullptr;
261+ other.prev_ = nullptr;
262+ }
263+ ```
0 commit comments