Skip to content

Commit 1ee08e6

Browse files
authored
chore: improve hello app const correctness + style (#300)
This commit adds `const` where possible in hello apps. It also updates with best practice of bringing in the appropriate SDK namespace.
1 parent 28003bd commit 1ee08e6

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

examples/hello-cpp-client/main.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ char const* get_with_env_fallback(char const* source_val,
1919
char const* error_msg);
2020

2121
using namespace launchdarkly;
22+
using namespace launchdarkly::client_side;
23+
2224
int main() {
2325
char const* mobile_key = get_with_env_fallback(
2426
MOBILE_KEY, "LD_MOBILE_KEY",
@@ -27,7 +29,7 @@ int main() {
2729
"variable.\n"
2830
"The value of MOBILE_KEY in main.c takes priority over LD_MOBILE_KEY.");
2931

30-
auto config = client_side::ConfigBuilder(mobile_key).Build();
32+
auto config = ConfigBuilder(mobile_key).Build();
3133
if (!config) {
3234
std::cout << "error: config is invalid: " << config.error() << '\n';
3335
return 1;
@@ -36,12 +38,13 @@ int main() {
3638
auto context =
3739
ContextBuilder().Kind("user", "example-user-key").Name("Sandy").Build();
3840

39-
auto client = client_side::Client(std::move(*config), std::move(context));
41+
auto client = Client(std::move(*config), std::move(context));
4042

4143
auto start_result = client.StartAsync();
42-
auto status = start_result.wait_for(
43-
std::chrono::milliseconds(INIT_TIMEOUT_MILLISECONDS));
44-
if (status == std::future_status::ready) {
44+
45+
if (auto const status = start_result.wait_for(
46+
std::chrono::milliseconds(INIT_TIMEOUT_MILLISECONDS));
47+
status == std::future_status::ready) {
4548
if (start_result.get()) {
4649
std::cout << "*** SDK successfully initialized!\n\n";
4750
} else {
@@ -54,7 +57,7 @@ int main() {
5457
return 1;
5558
}
5659

57-
bool flag_value = client.BoolVariation(FEATURE_FLAG_KEY, false);
60+
bool const flag_value = client.BoolVariation(FEATURE_FLAG_KEY, false);
5861

5962
std::cout << "*** Feature flag '" << FEATURE_FLAG_KEY << "' is "
6063
<< (flag_value ? "true" : "false") << " for this user\n\n";
@@ -69,8 +72,8 @@ char const* get_with_env_fallback(char const* source_val,
6972
return source_val;
7073
}
7174

72-
char const* from_env = std::getenv(env_variable);
73-
if (from_env && strlen(from_env)) {
75+
if (char const* from_env = std::getenv(env_variable);
76+
from_env && strlen(from_env)) {
7477
return from_env;
7578
}
7679

examples/hello-cpp-server/main.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ char const* get_with_env_fallback(char const* source_val,
1919
char const* env_variable,
2020
char const* error_msg);
2121
using namespace launchdarkly;
22+
using namespace launchdarkly::server_side;
23+
2224
int main() {
2325
char const* sdk_key = get_with_env_fallback(
2426
SDK_KEY, "LD_SDK_KEY",
@@ -28,18 +30,19 @@ int main() {
2830
"variable.\n"
2931
"The value of SDK_KEY in main.c takes priority over LD_SDK_KEY.");
3032

31-
auto config = server_side::ConfigBuilder(sdk_key).Build();
33+
auto config = ConfigBuilder(sdk_key).Build();
3234
if (!config) {
3335
std::cout << "error: config is invalid: " << config.error() << '\n';
3436
return 1;
3537
}
3638

37-
auto client = server_side::Client(std::move(*config));
39+
auto client = Client(std::move(*config));
3840

3941
auto start_result = client.StartAsync();
40-
auto status = start_result.wait_for(
41-
std::chrono::milliseconds(INIT_TIMEOUT_MILLISECONDS));
42-
if (status == std::future_status::ready) {
42+
43+
if (auto const status = start_result.wait_for(
44+
std::chrono::milliseconds(INIT_TIMEOUT_MILLISECONDS));
45+
status == std::future_status::ready) {
4346
if (start_result.get()) {
4447
std::cout << "*** SDK successfully initialized!\n\n";
4548
} else {
@@ -52,10 +55,11 @@ int main() {
5255
return 1;
5356
}
5457

55-
auto context =
58+
auto const context =
5659
ContextBuilder().Kind("user", "example-user-key").Name("Sandy").Build();
5760

58-
bool flag_value = client.BoolVariation(context, FEATURE_FLAG_KEY, false);
61+
bool const flag_value =
62+
client.BoolVariation(context, FEATURE_FLAG_KEY, false);
5963

6064
std::cout << "*** Feature flag '" << FEATURE_FLAG_KEY << "' is "
6165
<< (flag_value ? "true" : "false") << " for this user\n\n";
@@ -70,8 +74,8 @@ char const* get_with_env_fallback(char const* source_val,
7074
return source_val;
7175
}
7276

73-
char const* from_env = std::getenv(env_variable);
74-
if (from_env && strlen(from_env)) {
77+
if (char const* from_env = std::getenv(env_variable);
78+
from_env && strlen(from_env)) {
7579
return from_env;
7680
}
7781

0 commit comments

Comments
 (0)