Skip to content

Commit 9964656

Browse files
committed
Better comments in an example
1 parent 3cf55bc commit 9964656

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

lectures/code/error_handling/comparison_game/comparison_game.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ class Game {
5454
int budget_{};
5555
};
5656

57-
// Multiple issues here for now.
58-
// We should handle failure to get a proper value.
57+
// 😱 We should handle failure to get a proper value.
5958
ChangeEntry GetNextChangeEntryFromUser(const Game& game) {
6059
game.Print();
6160
ChangeEntry entry{};

lectures/error_handling.md

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,10 @@ class Game {
120120

121121
void Print() const {
122122
std::cout << "Budget: " << budget_ << std::endl;
123-
std::cout << "Computer numbers: ";
123+
std::cout << "Reference numbers: ";
124124
for (auto number : ref_numbers_) { std::cout << number << "\t"; }
125-
std::cout << "\nPlayer numbers: ";
125+
std::cout << std::endl;
126+
std::cout << "Player numbers: ";
126127
for (auto number : player_numbers_) { std::cout << number << "\t"; }
127128
std::cout << std::endl;
128129
}
@@ -145,6 +146,8 @@ class Game {
145146
budget_ -= difference;
146147
}
147148

149+
const std::vector<int>& ref_numbers() const { return ref_numbers_; }
150+
const std::vector<int>& player_numbers() const { return player_numbers_; }
148151
bool UserHasBudget() const { return budget_ > 0; }
149152

150153
private:
@@ -159,17 +162,18 @@ ChangeEntry GetNextChangeEntryFromUser(const Game& game) {
159162
ChangeEntry entry{};
160163
std::cout << "Please enter number to change: ";
161164
std::cin >> entry.index;
162-
std::cout << "Please provide a a new value: ";
165+
std::cout << "Please provide a new value: ";
163166
std::cin >> entry.value;
164167
return entry;
165168
}
166169

167170
int main() {
168-
Game game{{42, 50, 23}, {42, 40, 99}, 10};
171+
Game game{{42, 49, 23}, {42, 40, 23}, 10};
169172
while (game.UserHasBudget()) {
170173
const auto change_entry = GetNextChangeEntryFromUser(game);
171174
game.ChangePlayerNumberIfPossible(change_entry);
172175
}
176+
game.Print();
173177
if (game.CheckIfPlayerWon()) {
174178
std::cout << "You win!\n";
175179
} else {
@@ -260,6 +264,17 @@ Therefore a typical advice is to catch any wrong values that propagate through o
260264

261265
My favorite tool for this is the [`CHECK`](https://abseil.io/docs/cpp/guides/logging#CHECK) macro that can be found in the [Abseil library](https://abseil.io/docs/). We can use it in our `ChangePlayerNumberIfPossible` to check if the index is within bounds:
262266

267+
<!--
268+
`CPP_SETUP_START`
269+
#define CHECK(expr)
270+
#define CHECK_GE(expr, value)
271+
#define CHECK_LT(expr, value)
272+
273+
$PLACEHOLDER
274+
`CPP_SETUP_END`
275+
`CPP_COPY_SNIPPET` error_handling/main.cpp
276+
`CPP_RUN_CMD` CWD:error_handling mkdir -p absl/log && touch absl/log/check.h && c++ -std=c++17 -c main.cpp
277+
-->
263278
```cpp
264279
#include <absl/log/check.h>
265280

0 commit comments

Comments
 (0)