Live data from Hacker News

Ask HN: Can someone explain why this line exists?

news.ycombinator.com

1–10 of 17 posts

Re: Ask HN: Can someone explain why this line exists?

#3
post #2

If an exception was thrown, and _ok evaluates to false, then approximately 10% of the time, log the exception to the error log. Probably just to cut down on noise in the logs, or to account for brief periods of latency where the server appears down, but isn't.

Thanks, was reviewing it with a coworker and crept up on the same conclusion. Seems a bit hard to test and definitely alarming at first glance.

Re: Ask HN: Can someone explain why this line exists?

#4
If you read the code, you will notice that the FIRST time this error is encountered, it will always be logged.

The test in question is done frequently, and subsequent failures are about 100 times less interesting than the first. Unless you just like filling up disks with log files while your network is acting up.

Finally, note that the "_ok" flag will be RESET and the resolved condition logged once the test finally returns to succeeding.

Re: Ask HN: Can someone explain why this line exists?

#5
post #4

If you read the code, you will notice that the FIRST time this error is encountered, it will always be logged. The test in question is done frequently, and subsequent failures are about 100 times less interesting than the first. Unless you just like filling up disks with log files while your network is acting up. Finally, note that the "_ok" flag will be RESET and the resolved condition logged once the test finally r…

Awesome, thanks for the explanation

Re: Ask HN: Can someone explain why this line exists?

#6
post #4

If you read the code, you will notice that the FIRST time this error is encountered, it will always be logged. The test in question is done frequently, and subsequent failures are about 100 times less interesting than the first. Unless you just like filling up disks with log files while your network is acting up. Finally, note that the "_ok" flag will be RESET and the resolved condition logged once the test finally r…

Awesome, thanks for the explanation

Thank you for an excellent counterexample for when people claim code comments are useless.

Re: Ask HN: Can someone explain why this line exists?

#8
post #2

If an exception was thrown, and _ok evaluates to false, then approximately 10% of the time, log the exception to the error log. Probably just to cut down on noise in the logs, or to account for brief periods of latency where the server appears down, but isn't.

Thanks, was reviewing it with a coworker and crept up on the same conclusion. Seems a bit hard to test and definitely alarming at first glance.

I think that you're right about the intent of the author, but unless I'm mistaken, that's not what the code does. If _ok is false, it boils down to if(!(Math.random() > 0.1))) which is the same as if(Math.random() Wait, what?

Re: Ask HN: Can someone explain why this line exists?

#9
post #6

Earlier quoted context omitted.

Awesome, thanks for the explanation

Thank you for an excellent counterexample for when people claim code comments are useless.

You could express the same thing without comments:

    boolean alreadyLogged = !_ok;
    boolean skipLogging = alreadyLogged && (Math.random() > 0.1)
    if( skipLogging ) return res;

Re: Ask HN: Can someone explain why this line exists?

#10
post #9
post #6

Earlier quoted context omitted.

Thank you for an excellent counterexample for when people claim code comments are useless.

You could express the same thing without comments: boolean alreadyLogged = !_ok; boolean skipLogging = alreadyLogged && (Math.random() > 0.1) if( skipLogging ) return res;

I strongly dislike your version. Creating a boolean just to use and discard in the next line? Twice? Ugly. I'd much rather keep the code simple and try to cut out confusing parts. Give the flag a better name and remove the useless ternary. I'd also get rid of the conditional return and let the one outside the catch do the work.

  if (!errorLogged || (Math.random() 
But if it's a choice between comments and adding temporary variables that don't do anything, I'll almost always choose comments.
Post reply on HN