Skip to content

Commit 2bfb860

Browse files
committed
separate names with underscores
1 parent 0701c4d commit 2bfb860

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

CppCoreGuidelines.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -523,8 +523,8 @@ Some language constructs express intent better than others.
523523

524524
If two `int`s are meant to be the coordinates of a 2D point, say so:
525525

526-
drawline(int, int, int, int); // obscure
527-
drawline(Point, Point); // clearer
526+
draw_line(int, int, int, int); // obscure
527+
draw_line(Point, Point); // clearer
528528

529529
##### Enforcement
530530

@@ -2671,23 +2671,23 @@ possibly with the extra convenience of `tie` at the call site.
26712671
}
26722672

26732673
C++98's standard library already used this style, because a `pair` is like a two-element `tuple`.
2674-
For example, given a `set<string> myset`, consider:
2674+
For example, given a `set<string> my_set`, consider:
26752675

26762676
// C++98
2677-
result = myset.insert("Hello");
2677+
result = my_set.insert("Hello");
26782678
if (result.second) do_something_with(result.first); // workaround
26792679

26802680
With C++11 we can write this, putting the results directly in existing local variables:
26812681

26822682
Sometype iter; // default initialize if we haven't already
26832683
Someothertype success; // used these variables for some other purpose
26842684

2685-
tie(iter, success) = myset.insert("Hello"); // normal return value
2685+
tie(iter, success) = my_set.insert("Hello"); // normal return value
26862686
if (success) do_something_with(iter);
26872687

26882688
With C++17 we should be able to use "structured bindings" to declare and initialize the multiple variables:
26892689

2690-
if (auto [ iter, success ] = myset.insert("Hello"); success) do_something_with(iter);
2690+
if (auto [ iter, success ] = my_set.insert("Hello"); success) do_something_with(iter);
26912691

26922692
##### Exception
26932693

@@ -5400,13 +5400,13 @@ To prevent slicing, because the normal copy operations will copy only the base p
54005400
};
54015401

54025402
class D : public B {
5403-
string moredata; // add a data member
5403+
string more_data; // add a data member
54045404
// ...
54055405
};
54065406

54075407
auto d = make_unique<D>();
54085408

5409-
// oops, slices the object; gets only d.data but drops d.moredata
5409+
// oops, slices the object; gets only d.data but drops d.more_data
54105410
auto b = make_unique<B>(d);
54115411

54125412
##### Example
@@ -5419,7 +5419,7 @@ To prevent slicing, because the normal copy operations will copy only the base p
54195419
};
54205420

54215421
class D : public B {
5422-
string moredata; // add a data member
5422+
string more_data; // add a data member
54235423
unique_ptr<B> clone() override { return /* D object */; }
54245424
// ...
54255425
};
@@ -15120,14 +15120,14 @@ Use the least-derived class that has the functionality you need.
1512015120
};
1512115121

1512215122
// bad, unless there is a specific reason for limiting to Derived1 objects only
15123-
void myfunc(Derived1& param)
15123+
void my_func(Derived1& param)
1512415124
{
1512515125
use(param.f());
1512615126
use(param.g());
1512715127
}
1512815128

1512915129
// good, uses only Base interface so only commit to that
15130-
void myfunc(Base& param)
15130+
void my_func(Base& param)
1513115131
{
1513215132
use(param.f());
1513315133
use(param.g());
@@ -16123,29 +16123,29 @@ Sometimes you may be tempted to resort to `const_cast` to avoid code duplication
1612316123
class Bar;
1612416124

1612516125
class Foo {
16126-
Bar mybar;
16126+
Bar my_bar;
1612716127
public:
1612816128
// BAD, duplicates logic
1612916129
Bar& get_bar() {
16130-
/* complex logic around getting a non-const reference to mybar */
16130+
/* complex logic around getting a non-const reference to my_bar */
1613116131
}
1613216132

1613316133
const Bar& get_bar() const {
16134-
/* same complex logic around getting a const reference to mybar */
16134+
/* same complex logic around getting a const reference to my_bar */
1613516135
}
1613616136
};
1613716137

1613816138
Instead, prefer to share implementations. Normally, you can just have the non-`const` function call the `const` function. However, when there is complex logic this can lead to the following pattern that still resorts to a `const_cast`:
1613916139

1614016140
class Foo {
16141-
Bar mybar;
16141+
Bar my_bar;
1614216142
public:
1614316143
// not great, non-const calls const version but resorts to const_cast
1614416144
Bar& get_bar() {
1614516145
return const_cast<Bar&>(static_cast<const Foo&>(*this).get_bar());
1614616146
}
1614716147
const Bar& get_bar() const {
16148-
/* the complex logic around getting a const reference to mybar */
16148+
/* the complex logic around getting a const reference to my_bar */
1614916149
}
1615016150
};
1615116151

@@ -16154,11 +16154,11 @@ Although this pattern is safe when applied correctly, because the caller must ha
1615416154
Instead, prefer to put the common code in a common helper function -- and make it a template so that it deduces `const`. This doesn't use any `const_cast` at all:
1615516155

1615616156
class Foo {
16157-
Bar mybar;
16157+
Bar my_bar;
1615816158

1615916159
template<class T> // good, deduces whether T is const or non-const
1616016160
static auto get_bar_impl(T& t) -> decltype(t.get_bar())
16161-
{ /* the complex logic around getting a possibly-const reference to mybar */ }
16161+
{ /* the complex logic around getting a possibly-const reference to my_bar */ }
1616216162

1616316163
public: // good
1616416164
Bar& get_bar() { return get_bar_impl(*this); }

0 commit comments

Comments
 (0)