r/ProgrammerAnimemes • • Mar 23 '20

Comments you can't remove

Post image
862 Upvotes

53 comments sorted by

View all comments

Show parent comment

20

u/Kengaro Mar 24 '20

I am obviously talking about why it breaks when the commented section is uncommented....

1

u/xibme Jun 01 '20 edited Jun 01 '20

Then it's a syntax error, but I get what you mean.

int factorial(int n)
{
  int fallback = 1;

  /* This breaks if you remove the comment,
   * so leave it in.
   */
  if (n == 0) {          Handle 0! specially.
    n = 1;               * 0! is the same as 1!.
    return factorial(n);     
  } else {
    return n * factorial(n - 1);
  }

  return fallback;
}

vs (what you probably mean)

int factorial(int n)
{
  int fallback = 1;

  /* This breaks if you remove the comment,
   * so leave it in.
   */
  if (n == 0) {
    n = 1;
    return factorial(n);     
  } else {
    return n * factorial(n - 1);
  }

  return fallback;
}

vs what the rest was thinking of

int factorial(int n)
{
  int fallback = 1;

  /* This breaks if you remove the comment,
   * so leave it in.
   */
  if (n == 0) {    
  } else {
    return n * factorial(n - 1);
  }

  return fallback;
}

1

u/Kengaro Jun 01 '20

The code is basically:

int factorial(int n) {
    int fallback = 1;    /* This breaks if you remove the comment,
                         * so leave it in.
                         */
   if (n == 0) {
   } else {
     return n * factorial(n - 1);
   }
   return fallback; }

The comment behind the if removes the whole if-statement, if it is uncommented what I described above happens.

Without the comment:

int factorial(int n) {
    int fallback = 1;    /* This breaks if you remove the comment,
                         * so leave it in.
                         */
   if (n == 0) {
     n = 1;
     return factorial(n);
   } else {
     return n * factorial(n - 1);
   }
   return fallback; }

I didn't know it will compile without a statement following if....

1

u/xibme Jun 01 '20 edited Jun 02 '20

The comment behind the if removes the whole if-statement

It only removes the block for the if/true path - not the whole if/else construct. Your code matches, your words not. Sorry to be nitpicky here, but that's important as it is very easy to cause misunderstandings.