Skip to main content
Code Review

Return to Answer

added 48 characters in body
Source Link
chux
  • 36.2k
  • 2
  • 43
  • 96
  1. "whether its bad practice or not" to sub-divide. No, not bad practice - in fact good in certain areas. It often comes down to maintenance - how many functions per file and the optimal answer is highly problem dependent. IMO, too often files need fewer functions than code needs fewer files.

  2. ungetch() have troubles as it ungets a EOF. This special value should not be unget-able. Further – I have doubts that when char is signed, that these 2 functions properly handle some char values as the real getc() returns unsigned char and EOF. Example: If (char) 255 is un-gotten, on get, it may have the value of EOF. Recommend making your unget buffer unsigned char.

  3. Unclear to the whole need for user unget/get functionality as standard library ones well handle 1 level of get/unget.

  4. The whole addtree(), should input be alphabetical, devolves into a linked list. Consider AVL trees.

  5. Below test i against size after the assignment. Recommend test before to cope with pathological size values like 1. Recommend re-write - avoiding code like (word+i++). Further, consider size_t rather than int for array indexes.

    // from
    do {
     *(word+i++) = c;
    } while (isalpha(c = getch()) && i < size-1);
    // to
    if (size > 0) size--; 
    while (i < size && isalpha(c)) {
     word[i++] = c;
     c = getch()
     if (!isalpha(c)) {
     break;
     }
    };
    
  6. MAX_WORD is fuzzy. Suggest MAX_WORD_SIZE.

  7. ungetch() and getch() look way too much like standard functions. Suggest io_ungetch() and io_getch().

  8. Cast not needed.

     // return (struct tnode *) malloc(sizeof(struct tnode));
     return malloc(sizeof(struct tnode));
     // p = (char *) malloc(strlen(s)+1);
     p = malloc(strlen(s)+1);
    
  9. Magic number 4. Why 4?

     // printf("%4d %s\n", node->count, node->word);
     printf("%d %s\n", node->count, node->word);
    
  10. If a function does not modify a pointer's contents, use const.

     // void treeprint(struct tnode *node, int min_count, int max_count)
     void treeprint(const struct tnode *node, int min_count, int max_count)
    
  11. Minor: .h file void ungetch(int); --> void ungetch(int ch); Also add a little documentation of the .h files, it goes a long way in understanding.

  12. Minor: spelling: doesnt --> doesn't

  13. Pedantic point: char may be signed and is...() defined for unsigned char and EOF, not signed char.

     // if(isalpha(word[0]))
     if(isalpha((unsigned char) word[0]))
    
  1. "whether its bad practice or not" to sub-divide. No, not bad practice - in fact good in certain areas. It often comes down to maintenance - how many functions per file and the optimal answer is highly problem dependent. IMO, too often files need fewer functions than code needs fewer files.

  2. ungetch() have troubles as it ungets a EOF. This special value should not be unget-able. Further – I have doubts that when char is signed, that these 2 functions properly handle some char values as the real getc() returns unsigned char and EOF. Example: If (char) 255 is un-gotten, on get, it may have the value of EOF. Recommend making your unget buffer unsigned char.

  3. Unclear to the whole need for user unget/get functionality as standard library ones well handle 1 level of get/unget.

  4. The whole addtree(), should input be alphabetical, devolves into a linked list. Consider AVL trees.

  5. Below test i against size after the assignment. Recommend test before to cope with pathological size values like 1. Recommend re-write - avoiding code like (word+i++). Further, consider size_t rather than int for array indexes.

    // from
    do {
     *(word+i++) = c;
    } while (isalpha(c = getch()) && i < size-1);
    // to
    if (size > 0) size--; 
    while (i < size && isalpha(c)) {
     word[i++] = c;
     c = getch()
    };
    
  6. MAX_WORD is fuzzy. Suggest MAX_WORD_SIZE.

  7. ungetch() and getch() look way too much like standard functions. Suggest io_ungetch() and io_getch().

  8. Cast not needed.

     // return (struct tnode *) malloc(sizeof(struct tnode));
     return malloc(sizeof(struct tnode));
     // p = (char *) malloc(strlen(s)+1);
     p = malloc(strlen(s)+1);
    
  9. Magic number 4. Why 4?

     // printf("%4d %s\n", node->count, node->word);
     printf("%d %s\n", node->count, node->word);
    
  10. If a function does not modify a pointer's contents, use const.

     // void treeprint(struct tnode *node, int min_count, int max_count)
     void treeprint(const struct tnode *node, int min_count, int max_count)
    
  11. Minor: .h file void ungetch(int); --> void ungetch(int ch); Also add a little documentation of the .h files, it goes a long way in understanding.

  12. Minor: spelling: doesnt --> doesn't

  13. Pedantic point: char may be signed and is...() defined for unsigned char and EOF, not signed char.

     // if(isalpha(word[0]))
     if(isalpha((unsigned char) word[0]))
    
  1. "whether its bad practice or not" to sub-divide. No, not bad practice - in fact good in certain areas. It often comes down to maintenance - how many functions per file and the optimal answer is highly problem dependent. IMO, too often files need fewer functions than code needs fewer files.

  2. ungetch() have troubles as it ungets a EOF. This special value should not be unget-able. Further – I have doubts that when char is signed, that these 2 functions properly handle some char values as the real getc() returns unsigned char and EOF. Example: If (char) 255 is un-gotten, on get, it may have the value of EOF. Recommend making your unget buffer unsigned char.

  3. Unclear to the whole need for user unget/get functionality as standard library ones well handle 1 level of get/unget.

  4. The whole addtree(), should input be alphabetical, devolves into a linked list. Consider AVL trees.

  5. Below test i against size after the assignment. Recommend test before to cope with pathological size values like 1. Recommend re-write - avoiding code like (word+i++). Further, consider size_t rather than int for array indexes.

    // from
    do {
     *(word+i++) = c;
    } while (isalpha(c = getch()) && i < size-1);
    // to
    if (size > 0) size--; 
    while (i < size) {
     word[i++] = c;
     c = getch()
     if (!isalpha(c)) {
     break;
     }
    }
    
  6. MAX_WORD is fuzzy. Suggest MAX_WORD_SIZE.

  7. ungetch() and getch() look way too much like standard functions. Suggest io_ungetch() and io_getch().

  8. Cast not needed.

     // return (struct tnode *) malloc(sizeof(struct tnode));
     return malloc(sizeof(struct tnode));
     // p = (char *) malloc(strlen(s)+1);
     p = malloc(strlen(s)+1);
    
  9. Magic number 4. Why 4?

     // printf("%4d %s\n", node->count, node->word);
     printf("%d %s\n", node->count, node->word);
    
  10. If a function does not modify a pointer's contents, use const.

     // void treeprint(struct tnode *node, int min_count, int max_count)
     void treeprint(const struct tnode *node, int min_count, int max_count)
    
  11. Minor: .h file void ungetch(int); --> void ungetch(int ch); Also add a little documentation of the .h files, it goes a long way in understanding.

  12. Minor: spelling: doesnt --> doesn't

  13. Pedantic point: char may be signed and is...() defined for unsigned char and EOF, not signed char.

     // if(isalpha(word[0]))
     if(isalpha((unsigned char) word[0]))
    
added 211 characters in body
Source Link
chux
  • 36.2k
  • 2
  • 43
  • 96
  1. "whether its bad practice or not" to sub-divide. No, not bad practice - in fact good in certain areas. It often comes down to maintenance - how many functions per file and the optimal answer is highly problem dependent. IMO, too often files need fewer functions than code needs fewer files.

  2. ungetch() have troubles as ifit ungets a EOF. This special value should not be unget-able. Further – I have doubts that when char is signed, that these 2 functions properly handle some char values as the real getc() returns unsigned char and EOF. Recommend Example: If (char) 255 is un-gotten, on get, it may have the value of EOF. Recommend making your unget buffer unsigned char.

  3. Unclear to the whole need for user unget/get functionality as standard library ones well handle 1 level of get/unget.

  4. The whole addtree(), should input be alphabetical, devolves into a linked list. Consider AVL trees.

  5. Below test i against size after the assignment. Recommend test before to cope with pathological size values like 1. Recommend re-write - avoiding code like (word+i++). Further, consider size_t rather than int for array indexes.

    // from
    do {
     *(word+i++) = c;
    } while (isalpha(c = getch()) && i < size-1);
    // to
    if (size > 0) size--; 
    while (i < size && isalpha(c)) {
     word[i++] = c;
     c = getch()
    };
    
  6. MAX_WORD is fuzzy. Suggest MAX_WORD_SIZE.

  7. ungetch() and getch() look way too much like standard functions. Suggest io_ungetch() and io_getch().

  8. Cast not needed.

     // return (struct tnode *) malloc(sizeof(struct tnode));
     return malloc(sizeof(struct tnode));
     // p = (char *) malloc(strlen(s)+1);
     p = malloc(strlen(s)+1);
    
  9. Magic number 4. Why 4?

     // printf("%4d %s\n", node->count, node->word);
     printf("%d %s\n", node->count, node->word);
    
  10. If a function does not modify a pointer's contents, use const.

     // void treeprint(struct tnode *node, int min_count, int max_count)
     void treeprint(const struct tnode *node, int min_count, int max_count)
    
  11. Minor: .h file void ungetch(int); --> void ungetch(int ch); Also add a little documentation of the .h files, it goes a long way in understanding.

  12. Minor: spelling: doesnt --> doesn't

  13. Pedantic point: char may be signed and is...() defined for unsigned char and EOF, not signed char.

     // if(isalpha(word[0]))
     if(isalpha((unsigned char) word[0]))
    
  1. "whether its bad practice or not" to sub-divide. No, not bad practice - in fact good in certain areas. It often comes down to maintenance - how many functions per file and the optimal answer is highly problem dependent. IMO, too often files need fewer functions than code needs fewer files.

  2. ungetch() have troubles as if ungets a EOF. This special value should not be unget-able. Further – I have doubts that when char is signed, that these 2 functions properly handle some char values as the real getc() returns unsigned char and EOF. Recommend making your unget buffer unsigned char.

  3. The whole addtree(), should input be alphabetical, devolves into a linked list. Consider AVL trees.

  4. Below test i against size after the assignment. Recommend test before to cope with pathological size values like 1. Recommend re-write - avoiding code like (word+i++). Further, consider size_t rather than int for array indexes.

    // from
    do {
     *(word+i++) = c;
    } while (isalpha(c = getch()) && i < size-1);
    // to
    if (size > 0) size--; 
    while (i < size && isalpha(c)) {
     word[i++] = c;
     c = getch()
    };
    
  5. MAX_WORD is fuzzy. Suggest MAX_WORD_SIZE.

  6. ungetch() and getch() look way too much like standard functions. Suggest io_ungetch() and io_getch().

  7. Cast not needed.

     // return (struct tnode *) malloc(sizeof(struct tnode));
     return malloc(sizeof(struct tnode));
     // p = (char *) malloc(strlen(s)+1);
     p = malloc(strlen(s)+1);
    
  8. Magic number 4. Why 4?

     // printf("%4d %s\n", node->count, node->word);
     printf("%d %s\n", node->count, node->word);
    
  9. If a function does not modify a pointer's contents, use const.

     // void treeprint(struct tnode *node, int min_count, int max_count)
     void treeprint(const struct tnode *node, int min_count, int max_count)
    
  10. Minor: .h file void ungetch(int); --> void ungetch(int ch); Also add a little documentation of the .h files, it goes a long way in understanding.

  11. Minor: spelling: doesnt --> doesn't

  12. Pedantic point: char may be signed and is...() defined for unsigned char and EOF, not signed char.

     // if(isalpha(word[0]))
     if(isalpha((unsigned char) word[0]))
    
  1. "whether its bad practice or not" to sub-divide. No, not bad practice - in fact good in certain areas. It often comes down to maintenance - how many functions per file and the optimal answer is highly problem dependent. IMO, too often files need fewer functions than code needs fewer files.

  2. ungetch() have troubles as it ungets a EOF. This special value should not be unget-able. Further – I have doubts that when char is signed, that these 2 functions properly handle some char values as the real getc() returns unsigned char and EOF. Example: If (char) 255 is un-gotten, on get, it may have the value of EOF. Recommend making your unget buffer unsigned char.

  3. Unclear to the whole need for user unget/get functionality as standard library ones well handle 1 level of get/unget.

  4. The whole addtree(), should input be alphabetical, devolves into a linked list. Consider AVL trees.

  5. Below test i against size after the assignment. Recommend test before to cope with pathological size values like 1. Recommend re-write - avoiding code like (word+i++). Further, consider size_t rather than int for array indexes.

    // from
    do {
     *(word+i++) = c;
    } while (isalpha(c = getch()) && i < size-1);
    // to
    if (size > 0) size--; 
    while (i < size && isalpha(c)) {
     word[i++] = c;
     c = getch()
    };
    
  6. MAX_WORD is fuzzy. Suggest MAX_WORD_SIZE.

  7. ungetch() and getch() look way too much like standard functions. Suggest io_ungetch() and io_getch().

  8. Cast not needed.

     // return (struct tnode *) malloc(sizeof(struct tnode));
     return malloc(sizeof(struct tnode));
     // p = (char *) malloc(strlen(s)+1);
     p = malloc(strlen(s)+1);
    
  9. Magic number 4. Why 4?

     // printf("%4d %s\n", node->count, node->word);
     printf("%d %s\n", node->count, node->word);
    
  10. If a function does not modify a pointer's contents, use const.

     // void treeprint(struct tnode *node, int min_count, int max_count)
     void treeprint(const struct tnode *node, int min_count, int max_count)
    
  11. Minor: .h file void ungetch(int); --> void ungetch(int ch); Also add a little documentation of the .h files, it goes a long way in understanding.

  12. Minor: spelling: doesnt --> doesn't

  13. Pedantic point: char may be signed and is...() defined for unsigned char and EOF, not signed char.

     // if(isalpha(word[0]))
     if(isalpha((unsigned char) word[0]))
    
Source Link
chux
  • 36.2k
  • 2
  • 43
  • 96
  1. "whether its bad practice or not" to sub-divide. No, not bad practice - in fact good in certain areas. It often comes down to maintenance - how many functions per file and the optimal answer is highly problem dependent. IMO, too often files need fewer functions than code needs fewer files.

  2. ungetch() have troubles as if ungets a EOF. This special value should not be unget-able. Further – I have doubts that when char is signed, that these 2 functions properly handle some char values as the real getc() returns unsigned char and EOF. Recommend making your unget buffer unsigned char.

  3. The whole addtree(), should input be alphabetical, devolves into a linked list. Consider AVL trees.

  4. Below test i against size after the assignment. Recommend test before to cope with pathological size values like 1. Recommend re-write - avoiding code like (word+i++). Further, consider size_t rather than int for array indexes.

    // from
    do {
     *(word+i++) = c;
    } while (isalpha(c = getch()) && i < size-1);
    // to
    if (size > 0) size--; 
    while (i < size && isalpha(c)) {
     word[i++] = c;
     c = getch()
    };
    
  5. MAX_WORD is fuzzy. Suggest MAX_WORD_SIZE.

  6. ungetch() and getch() look way too much like standard functions. Suggest io_ungetch() and io_getch().

  7. Cast not needed.

     // return (struct tnode *) malloc(sizeof(struct tnode));
     return malloc(sizeof(struct tnode));
     // p = (char *) malloc(strlen(s)+1);
     p = malloc(strlen(s)+1);
    
  8. Magic number 4. Why 4?

     // printf("%4d %s\n", node->count, node->word);
     printf("%d %s\n", node->count, node->word);
    
  9. If a function does not modify a pointer's contents, use const.

     // void treeprint(struct tnode *node, int min_count, int max_count)
     void treeprint(const struct tnode *node, int min_count, int max_count)
    
  10. Minor: .h file void ungetch(int); --> void ungetch(int ch); Also add a little documentation of the .h files, it goes a long way in understanding.

  11. Minor: spelling: doesnt --> doesn't

  12. Pedantic point: char may be signed and is...() defined for unsigned char and EOF, not signed char.

     // if(isalpha(word[0]))
     if(isalpha((unsigned char) word[0]))
    
lang-c

AltStyle によって変換されたページ (->オリジナル) /