-
Notifications
You must be signed in to change notification settings - Fork 340
Code metric fixes #4890
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Code metric fixes #4890
Conversation
daff603
to
cd230e7
Compare
} else { | ||
pos = date.length; | ||
} | ||
pos = separator.length > 0 ? date.indexOf(separator, index) : date.length; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changes like this seem like an improvement. Fyi @pq have we thought about adding a lint for preferring conditionals?
The one in Dart Code Metrics goes a bit overboard. I'd imagine if we added it to the core linter it would only fire for short expressions like this and not the large ones that code metrics suggests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about isNotEmpty
for seperator
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd also be happy with a lint + fix for isEmpty and isNotEmpty. Seems like a nice simple lint to nudge people to use the Dart core libraries. Looks like our users use .isNotEmpty
about 10X as often then .length > 0
so the lint shouldn't be contreversial.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We HAVE that lint. Somewhere...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://dart-lang.github.io/linter/lints/prefer_is_not_empty.html – HA! We should expand this to String!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
child: Text( | ||
frameDescription, | ||
style: selected ? theme.selectedTextStyle : theme.subtleTextStyle, | ||
child = asyncMarker |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kenzieschmoll what do you think about cases like this? Is this cleaner or more obfuscated?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find it harder to read when one or more of the three operands need to be split across multiple lines.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do think the if / else block is a little bit easier to read here given how many lines each operand is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. Reverted this one
@@ -807,7 +805,7 @@ class InspectorController extends DisposableController | |||
} | |||
|
|||
void selectionChanged() { | |||
if (visibleToUser == false) { | |||
if (!visibleToUser) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fyi @pq. This is a lint fix from Dart code metrics that I'd be comfortable having in the recommended set if we were able to run Dart code metrics without significant performance overhead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, that's:
https://dartcodemetrics.dev/docs/rules/common/no-boolean-literal-compare
I'm a little surprised that was never defined in the core linter...
invokeServiceMethodObservatoryInspectorRef(methodName, ref), | ||
); | ||
} | ||
return useDaemonApi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this was a larger block but I still think this was a little bit of an improvement over the original code.
Color get grey => isLight | ||
? const Color.fromARGB(255, 128, 128, 128) | ||
: const Color.fromARGB(255, 128, 128, 128); | ||
Color get grey => const Color.fromARGB(255, 128, 128, 128); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Case the linter caught we had a useless check for isLight
return MediaSize.l; | ||
else | ||
return MediaSize.xl; | ||
if (height < 900) return MediaSize.l; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fyi @pq this sort of case is probably one we would want to start suggesting and quick fixing to Patterns.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
} else { | ||
_altKeyPressed = false; | ||
} | ||
_altKeyPressed = event.isAltPressed ? true : false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be further simplified to _altKeyPressed = event.isAltPressed;
(as the branches just return the value of the condition).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done. filed https://github.com/dart-lang/linter/issues/3888 to track this as this really obvious unnecessary code has been committed over 200X in g3.
child: Text( | ||
frameDescription, | ||
style: selected ? theme.selectedTextStyle : theme.subtleTextStyle, | ||
child = asyncMarker |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find it harder to read when one or more of the three operands need to be split across multiple lines.
), | ||
); | ||
} else { | ||
// Use a placeholder for pages with no explicit documentation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
was the comment automatically removed? we should probably leave this even with the ternary operator
return scriptRef == null | ||
? node.scriptRef != null | ||
? false | ||
: name == node.name | ||
: node.scriptRef == null | ||
? false | ||
: scriptRef == node.scriptRef; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the nested ternary operators makes this a bit difficult to read
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agreed this is overly clever. reverted.
: image == null | ||
? const SizedBox() | ||
: scaleImage | ||
? Image(image: AssetImage(image), width: 20, height: 10) | ||
: Image(image: AssetImage(image)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
triple nest 😱
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
revertedh. if we were to add a lint about ternary operators, it would never suggest more than one nesting level and never suggest large blocks.
return guardClassType == '@Class' || guardClassType == 'Class' | ||
? true | ||
: false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could be further simplified to return guardClassType == '@Class' || guardClassType == 'Class';
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed and filed https://github.com/dart-lang/linter/issues/3888
); | ||
} | ||
return isAsyncBreak | ||
? result |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kenzieschmoll what do you think of this case is. Brian thought it is a bit harder to read but I could go either way on this one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also could go either way. I think I prefer the if / else here but the ternary isn't terrible since of of the options is a one-liner.
6358e9c
to
d889164
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PTAL
@jacob314 hi, if you have any feedback about the package (aside the comments above which I agree with, especially
This is interesting 🙂 . I'm working on a DCM version without the plugins API (and thus not listed in pubspec at all) and the overall performance is very good (VS Code integration is ready, now finishing the IntelliJ part). I'd be happy to discuss this idea in more details to understand what can be done from our side to enable DCM for the Flutter teams. |
By default configuration I mean the basic configuration suggested by https://dartcodemetrics.dev/docs/getting-started/configuration General feedback: all of the lints I tried were ones I would have liked to have turned on by default except for |
ef945bd
to
3c0291c
Compare
return color != null | ||
? dashedLine as bool | ||
? createDashWidget(color) | ||
: createSolidLine(color) | ||
: image == null | ||
? const SizedBox() | ||
: Image(image: AssetImage(image)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we revert the double ternary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Reverted this file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
one comment then lgtm
3c0291c
to
217d07b
Compare
Oh, I see. I guess we need to update it coz the list of rules is far more bigger right now 🙂 (you can find the whole list in presets https://github.com/dart-code-checker/dart-code-metrics/tree/master/lib/presets)
VS Code: the overall memory consumption for so called DCM analysis-server is about one and a half times less than for the Dart analysis-server In numbers (for monorepo of like 6 packages):
Just for the DCM repo https://github.com/dart-code-checker/dart-code-metrics:
Meaning that the DCM + Dart consume about twice less as Dart + plugin 😁. Please note: the measurements above taken after both processes were running for some time. The measurement on a start gives an even bigger difference (like 3 times). Additionally, there is no "new plugin instance for each package" problem, meaning that it scales pretty good. And the overall responsiveness is better (if both the extension and the plugin are enabled, I see the highlight from the extension and then from the plugin in a noticeable amount of time). IntelliJ: I haven't yet measured the IntelliJ difference, since the implementation is still in progress, but hopefully soon I'll be able to measure. Overall performance for the issues highlight is good, IMO.
Thanks, noted! We have a separate rule for limiting the nesting level https://dartcodemetrics.dev/docs/rules/common/avoid-nested-conditional-expressions, but idea to make
An example would be great, tbh (so I'd be able to fix it). But I can agree that the quick fixes we have don't check for the output validity and more like "this should work" type of a result.
Would be amazing!
Definitely a bug, thank you! |
Experiment applying most of the fixes suggested by the default dart_code_metrics configuration.
Feedback appreciated. Most of the switches to conditionals seem like an improvement but a few might be going overboard.