Comment

Comment by dlazar

Only commenting on the coding style: that kind of formatting is just looking for trouble. Quick, tell me which of the functions below contains a bug?

  //return = strlen(target)
  unsigned strmcat(char *target, const char *source, unsigned length) {
    const char *origin = target;
    while(*target && length) target++, length--;
    return (target - origin) + strmcpy(target, source, length);
  }

  //return = strlen(target)
  unsigned strmcat(char *target, const char *source, unsigned length) {
    const char *origin = target;
    while(*target && length) target++; length--;
    return (target - origin) + strmcpy(target, source, length);
  }
How about this?

  //return = strlen(target)
  unsigned strmcat(char *target, const char *source, unsigned length) {
    const char *origin = target;
    while(*target && length)
      target++, length--;
    return (target - origin) + strmcpy(target, source, length);
  }

  //return = strlen(target)
  unsigned strmcat(char *target, const char *source, unsigned length) {
    const char *origin = target;
    while(*target && length)
      target++;
    length--;
    return (target - origin) + strmcpy(target, source, length);
  }
Granted, it's a tiny example, but you're going to be writing fewer bugs in the long run if you avoid being clever. So here are two rules to start with:

1. Never put more than one statement on one line.

2. Never use the comma operator outside the "for" syntax.

parent dlazar 15 years ago

No comments yet

Be the first to share your thoughts.

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton