@@ -106,39 +106,35 @@ the reply is usually reported as an integer.
106106// Add some values to a set.
107107redisReply *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+
122120reply = 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.
155151reply = 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.
170165reply = 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
247237reply = 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
256245reply = 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
271258A map is essentially the same as an array but it has the extra
@@ -284,13 +271,12 @@ const char *hashCommand[] = {
284271
285272reply = 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
295281reply = redisCommand(c, "HGETALL details");
296282
0 commit comments