Coder Social home page Coder Social logo

Comments (7)

bmish avatar bmish commented on May 20, 2024 1

Makes sense to count these. I'm used to test coverage tools counting them as separate branches.

from eslint.

screendriver avatar screendriver commented on May 20, 2024 1

The null coalescing operator ?? should also be counted in my opinion because it also creates conditional branches which are increasing complexity:

- value is null or undefined
- value is not null or undefined

Forget about it. It seems that this is already the case šŸ‘

from eslint.

amareshsm avatar amareshsm commented on May 20, 2024

I don't think optional chaining and parameters will increase the cyclomatic complexity of the code

function a(x) {
   return x?.a?.b?.c?.d?.e?.f;
}

This code doesn't have control flow statements such as loops or conditionals. It's a simple expression that uses the optional chaining operator (?.) to access nested properties in the object x. Therefore, the control flow graph is straightforward (ie 1) and doesn't introduce additional complexity.

/*eslint complexity: ["error", 2]*/

function a(x) {
   return x?.a?.b?.c?.d?.e?.f;
}

function b(x) {
  const { a = '', b = 0 } = x;
} 

function c(a = '', b = 0) {
}

According to JSHint, the complexity of the above functions is 1.

from eslint.

lo1tuma avatar lo1tuma commented on May 20, 2024

This code doesn't have control flow statements such as loops or conditionals. It's a simple expression that uses the optional chaining operator (?.)

How would you implement the code without optional chaining? Iā€™m pretty sure that you would need a conditional. IMHO optional chaining is only syntax sugar for an if statement or an ternary. Every access to an optional property via optional chaining creates two possible code branches. Which is AFAIK the definition of cyclomatic complexity. In the example x?.b there are two possible branches in which the code can execute.

Also coverage tools like c8 report untested optional chaining expressions as an uncovered branch.

According to JSHint, the complexity of the above functions is 1.

Iā€™m not sure if this is relevant. Iā€™m not following the development of JSHint anymore, but I would be surprised if they implement rule-specific logic for modern JS language features.

from eslint.

nzakas avatar nzakas commented on May 20, 2024

I agree with @lo1tuma -- both optional chaining and default parameters represent branches along a code path, which is the definition of complexity.

I'm šŸ‘ for this change but would like some other opinions from @eslint/eslint-team.

from eslint.

JoshuaKGoldberg avatar JoshuaKGoldberg commented on May 20, 2024

Agreement from me on account of what nzakas and bmish have already said.

from eslint.

nzakas avatar nzakas commented on May 20, 2024

Looks like there is consensus, so marking as accepted. @lo1tuma if you want to submit a PR, we can review it.

from eslint.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. šŸ“ŠšŸ“ˆšŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.