Commit 2b5f2d67 authored by Matúš Sulír's avatar Matúš Sulír
Browse files

Edit a function to become a refactoring candidate

parent 2a45dea9
......@@ -7,7 +7,7 @@ post_t *create_post(char *text) {
if (strlen(text) <= MAX_POST_LENGTH) {
post_t *post = malloc(sizeof(post_t));
strcpy(post->text, text);
post->num_likes = 0;
post->like_count = 0;
return post;
} else {
return NULL;
......@@ -19,20 +19,30 @@ void destroy_post(post_t *post) {
}
void like_post(post_t *post) {
post->num_likes++;
post->like_count++;
}
void unlike_post(post_t *post) {
if (post->num_likes > 0)
post->num_likes--;
if (post->like_count > 0)
post->like_count--;
}
/* The buffer must be at least ... bytes long. */
/*
* Returns a text representing the number of likes of the post, taking
* singular/plural forms into account.
*
* post - the post of interest
* buf - a buffer at least 30 bytes long (if int is 64-bit)
*
* This function is a candidate for refactoring.
*/
char *like_count_text(post_t *post, char *buf) {
if (post->num_likes == 1) {
sprintf(buf, "1 like");
} else {
sprintf(buf, "%d likes", post->num_likes);
int var1 = post->like_count;
int var2 = (var1 == 1);
sprintf(post, "%d", var1);
int len = strlen(buf);
if (var2) {
sprintf(post, "%d like", var1);
}
return buf;
}
......@@ -5,7 +5,7 @@
typedef struct {
char text[MAX_POST_LENGTH + 1];
int num_likes;
int like_count;
} post_t;
post_t *create_post(char *text);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment