[HN Gopher] Interesting Bugs Caught by ESLint's no-constant-bina...
___________________________________________________________________
Interesting Bugs Caught by ESLint's no-constant-binary-expression
Author : CharlesW
Score : 24 points
Date : 2023-11-08 20:41 UTC (2 hours ago)
(HTM) web link (eslint.org)
(TXT) w3m dump (eslint.org)
| cultureswitch wrote:
| Having any such binary expression is considered a severe
| violation of coding rules for certified software (avionics,
| medical...). There's a good reason, because it's my almost always
| hiding a bug. And in gray cases where the duplication is not
| obvious, forces to think hard about it.
| yjftsjthsd-h wrote:
| > Eventually it clicked for me: developers don't intend to write
| useless code, and code that does not match the developer's intent
| is by definition a bug. Therefore, any useless code you can
| detect is a bug.
|
| I'm not completely sure I would take it all the way to calling it
| a bug, but I do appreciate a rigorous way to simplify code,
| because the worst case is that you've made it easier to reason
| about and that's a win all by itself.
|
| (EDIT: This post also makes me feel better about my personal
| coding style being paranoid and doing things like using parens to
| force order of operations and avoiding "advanced" constructs like
| ?? because I don't trust myself to not shoot myself in the foot.
| I'm not a professional dev, so I'm happy to write verbose,
| inelegant code in exchange for it being so simple that I'm less
| likely to screw it up)
| darraghmckay wrote:
| This was really interesting, particularly seeing how a lot of
| these are probably mistakes stemming from familiarity with syntax
| of other languages
| fiddlerwoaroof wrote:
| One thing that reading _The Little Prover_ did for me was correct
| a sort of lazy preconception I had that you cannot "reason about"
| code in dynamically typed languages. And it sort of crystallized
| my experience of large dynamically typed codebases not being as
| bad as one might expect. The patterns in OP are one case here
| where the boolean expressions are trivially analyzable and there
| are other classes of such analysis: e.g. it's possible to use the
| condition of an id statement to detect useless expressions in
| both the then and else clauses.
| lalaithion wrote:
| Ideally this lint would ignore if (false) and if (true), even
| though these are still constant binary expressions, they aren't
| vectors for similar bugs like the constant expressions in the
| blog post.
|
| Although "real" production software would use feature flags of
| some kind instead of hardcoding the constant, sometimes you do
| just need to hide some code behind an if statement that is
| currently ineffectual, and linters that prevent that are
| extremely annoying and force me to write a confusing, convoluted
| expression that is too complicated for the linter to detect as
| constant true or constant false.
| twisteriffic wrote:
| > Thinking || allows for set operations: states.includes('VALID'
| || 'IN_PROGRESS')
|
| Genuinely wondering what language that would have been valid in.
___________________________________________________________________
(page generated 2023-11-08 23:00 UTC)