News for hackers. Stuff that matters.
Re:Interesting Bugs Caught by ESLint's no-constant-binary-ex... by n2d4 on Wednesday November 08, 2023 @10:17PM (#38200719)
Regarding the recommendations:

0 is to prevent mistakes where you declare a function, but forget to implement it. If that's really intended I like to put a comment "do nothing" in empty functions, even in projects without the ESLint rule, so future readers know what's going on.

1 is for consistency & readability. Imagine you read someone else's code like <div children="foo" />, and you see that it's self-closing, so you expect it's an empty div. Even more confusing when you have more than just one attribute. Or say you want to modify the code to add a new child, so you remove the self-closing and add the child, breaking the old children in the process. What is the reason for doing children="foo" in the first place anyways?

2 is to prevent infinite loops, it's good practice to keep an upper bound (in a single-threaded world like JS, infinite loops can be very deadly and hard to diagnose). I like NASA's ten coding commandments (this is #2): https://devm.io/careers/power-ten-nasas-coding-commandments-...

Re:Interesting Bugs Caught by ESLint's no-constant-binary-ex... by FranksTV on Wednesday November 08, 2023 @10:31PM (#38200807)
I find throwing an error that says "not implemented" to be a good solution. Or at least logging a warning.
Re:Interesting Bugs Caught by ESLint's no-constant-binary-ex... by josephg on Thursday November 09, 2023 @05:59AM (#38203470)
Yeah I love rust’s todo!() macro for this. Not only does it throw an error, but it also typechecks in any context. Normally a function needs to actually return whatever it says it will return. But throw a todo!() in there, and you don’t need to return anything at all. (I assume there’s some type magic going on behind the scenes. Whatever it is, I love it.)

But generally I agree that it’s overly persnickety to complain about empty functions. My use case for them is that sometimes you need to pass a function as an argument and the function may be null - to indicate you don’t want to do any work. It’s often cleaner to use an empty function as a default value rather than add null checks everywhere.

I don’t use eslint at all because of defaults like this. Having my coding style negged by a tool feels awful. I hate gofmt. I ran it once and it deleted a bunch of empty lines in my code that I had put in on purpose to aid readability. And wow, that pissed me right off! I just know I’ll hate a lot of eslint’s rules just as much. 30 years of coding experience gives you opinions. Kids these days don’t even know how to use the ternary operator properly.

Re:Interesting Bugs Caught by ESLint's no-constant-binary-ex... by pmezard on Thursday November 09, 2023 @07:28AM (#38204140)
> Kids these days don’t even know how to use the ternary operator properly.

You mean not using it at all? 30 years of coding experience gives you opinions.

Re:Interesting Bugs Caught by ESLint's no-constant-binary-ex... by tbossanova on Thursday November 09, 2023 @10:25PM (#38214728)
For being explicit that I’ve deliberately chosen to leave a function empty, I like to have a single noOp function defined that’s documented and use that to show I meant to do it. Only if you need it more than three times though of course!