r/ProgrammerAnimemes • • Mar 23 '20

Comments you can't remove

Post image
868 Upvotes

53 comments sorted by

View all comments

Show parent comment

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.