Skip to content

Commit 6c6ff08

Browse files
DOC-4996 removed null checks in example for clarity
1 parent 2314eae commit 6c6ff08

File tree

3 files changed

+105
-129
lines changed

3 files changed

+105
-129
lines changed

content/develop/clients/hiredis/_index.md

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,26 +60,19 @@ int main() {
6060
} else {
6161
printf("Can't allocate redis context\n");
6262
}
63+
6364
exit(1);
6465
}
6566

6667
// Set a string key.
6768
redisReply *reply = redisCommand(c, "SET foo bar");
68-
69-
// Check and free the reply.
70-
if (reply != NULL) {
71-
printf("Reply: %s\n", reply->str); // >>> Reply: OK
72-
freeReplyObject(reply);
73-
}
69+
printf("Reply: %s\n", reply->str); // >>> Reply: OK
70+
freeReplyObject(reply);
7471

7572
// Get the key we have just stored.
7673
reply = redisCommand(c, "GET foo");
77-
78-
// Check and free the reply.
79-
if (reply != NULL) {
80-
printf("Reply: %s\n", reply->str); // >>> Reply: bar
81-
freeReplyObject(reply);
82-
}
74+
printf("Reply: %s\n", reply->str); // >>> Reply: bar
75+
freeReplyObject(reply);
8376

8477
// Close the connection.
8578
redisFree(c);

content/develop/clients/hiredis/connect.md

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ and port as its arguments, and returns a context object.
2626
#include <stdio.h>
2727

2828
#include "hiredis.h"
29+
.
30+
.
31+
.
2932

3033
// The `redisContext` type represents the connection
3134
// to the Redis server. Here, we connect to the
@@ -41,25 +44,19 @@ if (c == NULL || c->err) {
4144
} else {
4245
printf("Can't allocate redis context\n");
4346
}
47+
48+
exit(1);
4449
}
4550

4651
// Set a string key.
4752
redisReply *reply = redisCommand(c, "SET foo bar");
48-
49-
// Check and free the reply.
50-
if (reply != NULL) {
51-
printf("Reply: %s\n", reply->str); // >>> Reply: OK
52-
freeReplyObject(reply);
53-
}
53+
printf("Reply: %s\n", reply->str); // >>> Reply: OK
54+
freeReplyObject(reply);
5455

5556
// Get the key we have just stored.
5657
reply = redisCommand(c, "GET foo");
57-
58-
// Check and free the reply.
59-
if (reply != NULL) {
60-
printf("Reply: %s\n", reply->str); // >>> Reply: bar
61-
freeReplyObject(reply);
62-
}
58+
printf("Reply: %s\n", reply->str); // >>> Reply: bar
59+
freeReplyObject(reply);
6360

6461
// Close the connection.
6562
redisFree(c);

content/develop/clients/hiredis/handle-replies.md

Lines changed: 91 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -106,39 +106,35 @@ the reply is usually reported as an integer.
106106
// Add some values to a set.
107107
redisReply *reply = redisCommand(c, "SADD items bread milk peas");
108108

109-
if (reply != NULL) {
110-
// This gives an integer reply.
111-
if (reply->type == REDIS_REPLY_INTEGER) {
112-
// Report status.
113-
printf("Integer reply\n");
114-
printf("Number added: %lld\n", reply->integer);
115-
// >>> Number added: 3
116-
}
117-
118-
freeReplyObject(reply);
119-
reply = NULL;
109+
if (reply->type == REDIS_REPLY_INTEGER) {
110+
// Report status.
111+
printf("Integer reply\n");
112+
printf("Number added: %lld\n", reply->integer);
113+
// >>> Number added: 3
120114
}
121115

116+
freeReplyObject(reply);
117+
reply = NULL;
118+
119+
122120
reply = redisCommand(c, "SISMEMBER items bread");
123121

124-
if (reply != NULL) {
125-
// This also gives an integer reply but you should interpret
126-
// it as a boolean value.
127-
if (reply->type == REDIS_REPLY_INTEGER) {
128-
// Respond to boolean integer value.
129-
printf("Integer reply\n");
130-
131-
if (reply->integer == 0) {
132-
printf("Items set has no member 'bread'\n");
133-
} else {
134-
printf("'Bread' is a member of items set\n");
135-
}
136-
// >>> 'Bread' is a member of items set
122+
// This also gives an integer reply but you should interpret
123+
// it as a boolean value.
124+
if (reply->type == REDIS_REPLY_INTEGER) {
125+
// Respond to boolean integer value.
126+
printf("Integer reply\n");
127+
128+
if (reply->integer == 0) {
129+
printf("Items set has no member 'bread'\n");
130+
} else {
131+
printf("'Bread' is a member of items set\n");
137132
}
138-
139-
freeReplyObject(reply);
140-
reply = NULL;
133+
// >>> 'Bread' is a member of items set
141134
}
135+
136+
freeReplyObject(reply);
137+
reply = NULL;
142138
```
143139
144140
### Strings
@@ -154,81 +150,75 @@ returned in `reply->str` and the length of the string is in
154150
// Set a numeric value in a string.
155151
reply = redisCommand(c, "SET number 1.5");
156152
157-
if (reply != NULL) {
158-
// This gives a status reply.
159-
if (reply->type == REDIS_REPLY_STATUS) {
160-
// Report status.
161-
printf("Status reply\n");
162-
printf("Reply: %s\n", reply->str); // >>> Reply: OK
163-
}
164-
165-
freeReplyObject(reply);
166-
reply = NULL;
153+
// This gives a status reply.
154+
if (reply->type == REDIS_REPLY_STATUS) {
155+
// Report status.
156+
printf("Status reply\n");
157+
printf("Reply: %s\n", reply->str); // >>> Reply: OK
167158
}
168159
160+
freeReplyObject(reply);
161+
reply = NULL;
162+
163+
169164
// Attempt to interpret the key as a hash.
170165
reply = redisCommand(c, "HGET number field1");
171166
172-
if (reply != NULL) {
173-
// This gives an error reply.
174-
if (reply->type == REDIS_REPLY_ERROR) {
175-
// Report the error.
176-
printf("Error reply\n");
177-
printf("Reply: %s\n", reply->str);
178-
// >>> Reply: WRONGTYPE Operation against a key holding the wrong kind of value
179-
}
180-
181-
freeReplyObject(reply);
182-
reply = NULL;
167+
// This gives an error reply.
168+
if (reply->type == REDIS_REPLY_ERROR) {
169+
// Report the error.
170+
printf("Error reply\n");
171+
printf("Reply: %s\n", reply->str);
172+
// >>> Reply: WRONGTYPE Operation against a key holding the wrong kind of value
183173
}
184174
185-
reply = redisCommand(c, "GET number");
175+
freeReplyObject(reply);
176+
reply = NULL;
186177
187-
if (reply != NULL) {
188-
// This gives a simple string reply.
189-
if (reply->type == REDIS_REPLY_STRING) {
190-
// Display the string.
191-
printf("Simple string reply\n");
192-
printf("Reply: %s\n", reply->str); // >>> Reply: 1.5
193-
}
194178
195-
freeReplyObject(reply);
196-
reply = NULL;
179+
reply = redisCommand(c, "GET number");
180+
181+
// This gives a simple string reply.
182+
if (reply->type == REDIS_REPLY_STRING) {
183+
// Display the string.
184+
printf("Simple string reply\n");
185+
printf("Reply: %s\n", reply->str); // >>> Reply: 1.5
197186
}
198187
199-
reply = redisCommand(c, "ZADD prices 1.75 bread 5.99 beer");
188+
freeReplyObject(reply);
189+
reply = NULL;
200190
201-
if (reply != NULL) {
202-
// This gives an integer reply.
203-
if (reply->type == REDIS_REPLY_INTEGER) {
204-
// Display the integer.
205-
printf("Integer reply\n");
206-
printf("Number added: %lld\n", reply->integer);
207-
// >>> Number added: 2
208-
}
209191
210-
freeReplyObject(reply);
211-
reply = NULL;
192+
reply = redisCommand(c, "ZADD prices 1.75 bread 5.99 beer");
193+
194+
// This gives an integer reply.
195+
if (reply->type == REDIS_REPLY_INTEGER) {
196+
// Display the integer.
197+
printf("Integer reply\n");
198+
printf("Number added: %lld\n", reply->integer);
199+
// >>> Number added: 2
212200
}
213201
214-
reply = redisCommand(c, "ZSCORE prices bread");
202+
freeReplyObject(reply);
203+
reply = NULL;
215204
216-
if (reply != NULL) {
217-
// This gives a string reply with RESP2 and a double reply
218-
// with RESP3, but you handle it the same way in either case.
219-
if (reply->type == REDIS_REPLY_STRING) {
220-
printf("String reply\n");
221-
222-
char *endptr; // Not used.
223-
double price = strtod(reply->str, &endptr);
224-
double discounted = price * 0.75;
225-
printf("Discounted price: %.2f\n", discounted);
226-
// >>> Discounted price: 1.31
227-
}
228205
229-
freeReplyObject(reply);
230-
reply = NULL;
206+
reply = redisCommand(c, "ZSCORE prices bread");
207+
208+
// This gives a string reply with RESP2 and a double reply
209+
// with RESP3, but you handle it the same way in either case.
210+
if (reply->type == REDIS_REPLY_STRING) {
211+
printf("String reply\n");
212+
213+
char *endptr; // Not used.
214+
double price = strtod(reply->str, &endptr);
215+
double discounted = price * 0.75;
216+
printf("Discounted price: %.2f\n", discounted);
217+
// >>> Discounted price: 1.31
231218
}
219+
220+
freeReplyObject(reply);
221+
reply = NULL;
232222
```
233223

234224
### Arrays and maps
@@ -246,26 +236,23 @@ The example below shows how to get the items from a
246236
```c
247237
reply = redisCommand(c, "RPUSH things thing0 thing1 thing2 thing3");
248238

249-
if (reply != NULL) {
250-
printf("Added %lld items\n", reply->integer);
251-
// >>> Added 4 items
252-
freeReplyObject(reply);
253-
reply = NULL;
254-
}
239+
printf("Added %lld items\n", reply->integer);
240+
// >>> Added 4 items
241+
freeReplyObject(reply);
242+
reply = NULL;
243+
255244

256245
reply = redisCommand(c, "LRANGE things 0 -1");
257246

258-
if (reply != NULL) {
259-
for (int i = 0; i < reply->elements; ++i) {
260-
if (reply->element[i]->type == REDIS_REPLY_STRING) {
261-
printf("List item %d: %s\n", i, reply->element[i]->str);
262-
}
247+
for (int i = 0; i < reply->elements; ++i) {
248+
if (reply->element[i]->type == REDIS_REPLY_STRING) {
249+
printf("List item %d: %s\n", i, reply->element[i]->str);
263250
}
264-
// >>> List item 0: thing0
265-
// >>> List item 1: thing1
266-
// >>> List item 2: thing2
267-
// >>> List item 3: thing3
268251
}
252+
// >>> List item 0: thing0
253+
// >>> List item 1: thing1
254+
// >>> List item 2: thing2
255+
// >>> List item 3: thing3
269256
```
270257
271258
A map is essentially the same as an array but it has the extra
@@ -284,13 +271,12 @@ const char *hashCommand[] = {
284271
285272
reply = redisCommandArgv(c, 8, hashCommand, NULL);
286273
287-
if (reply != NULL) {
288-
printf("Added %lld fields\n", reply->integer);
289-
// >>> Added 3 fields
274+
printf("Added %lld fields\n", reply->integer);
275+
// >>> Added 3 fields
276+
277+
freeReplyObject(reply);
278+
reply = NULL;
290279
291-
freeReplyObject(reply);
292-
reply = NULL;
293-
}
294280
295281
reply = redisCommand(c, "HGETALL details");
296282

0 commit comments

Comments
 (0)