top | item 45702691

(no title)

devjab | 4 months ago

Maybe I'm doing things wrong, but I assume this tool is meant to focus on cognetive complexity and not things like code quality, transpiling or performance, but if that's true then why does this:

(score is 7) function get_first_user(data) { first_user = data[0]; return first_user; }

Score better than this:

(score is 8) function get_first_user(data: User[]): Result<User> { first_user = data[0]; return first_user; }

I mean, I know that the type annotations is what gives the lower score, but I would argue that the latter has the lower cognetive complexity.

discuss

order

uallo|4 months ago

I get the same overall FTA score of 7 for both of your examples. When omitting the return type (which can be inferred), you get the exact same scores. Not just the same FTA score. Also note that `Return<User>` should be just `User` if you prefer to specify the return type explicitly. That change will improve several of the scores as well.

whilenot-dev|4 months ago

> Also note that `Return<User>` should be just `User` if you prefer to specify the return type explicitly.

No? first_user = data[0] assigns User | undefined to first_user, since the list isn't guaranteed to be non-empty. I expect Return to be implemented as type Return<T> = T | undefined, so Return<User> makes sense.

k__|4 months ago

Maybe because the type can be inferred and it potentially adds effort for changes in the future.

devjab|4 months ago

I'm not sure how you can infer types on this. Even if you input an array of users from a different function. How would we know that data[0] is a User and not undefined?

zenmac|4 months ago

Then why use TypeScript at all? Just write js and put a TS definition on top. TS is a linter anyway. Now that will make the code easier to read, and in the end it is the code that will be interpreated by the browser or whatever JS runtimes.

motorest|4 months ago

> (...) I assume this tool is meant to focus on cognetive complexity and not things like code quality, transpiling or performance (...)

I don't know about transpiling or performance, but cyclomatic complexity is associated with both cognitive complexity and code quality.

I mean, why would code quality not reflect cognitive load? What would be the point, then?