Skip to content
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

[NO-TICKET] RFC: Adopt standardrb for Profiler #3801

Merged
merged 48 commits into from
Aug 13, 2024

Conversation

ivoanjo
Copy link
Member

@ivoanjo ivoanjo commented Jul 22, 2024

What does this PR do?

This PR replaces the current rubocop style configuration with the standardrb linter for the profiler only.

This PR has several objectives:

  1. Experiment with a path for incremental migration of the gem to standardrb
  2. Validate co-existence with Rubocop during the migration
  3. Serve as an example for future migrations

And in particular it shows:

  • How to tweak Rubocop to ignore the files and folders to be managed by standardrb
  • How to configure standardrb to operate only on a subset of files
  • How to set up .git-blame-ignore-revs to improve blame-ability of updated files
  • How to integrate standardrb on our usual CI setup

Motivation:

We've been adopting standardrb for our new codebases/gems, so we've recently discussed, and I had an action item to experiment with the profiler adopting standardrb.

Additional Notes:

You'll note that I've added a .standard_todo.yml file rather than fixing every file.
Specifically, I've yet to fix the following cops:

  • Layout/SpaceInsideHashLiteralBraces
  • Style/QuotedSymbols
  • Style/RedundantBegin
  • Style/StringLiterals
  • Style/TrailingCommaInArguments

This is because fixing each of these cops will create a LOT of diff noise. Thus, I've explicitly not included them in this PR, and plan to fix them separately, in smaller PRs, so we can still sanely review the diff on this PR.

How to test the change?

No behavior changes are expected, so our test coverage should be enough.

…he project on rubocop

This allows us to experiment with standard without fully committing to
it for the full library yet.
Nothing to fix on this one.
Nothing to fix on this one.
Nothing to fix on this one.
Nothing to fix on this one.
Nothing to fix on this one.
Nothing to fix on this one.
Nothing to fix on this one.
This file is really weird by design + it's only really used for testing
so I think it's OK if there's some small inconsistency with the rest of
the codebase.
Nothing to fix on this one.
Gemfile Outdated Show resolved Hide resolved
Rakefile Outdated Show resolved Hide resolved
Gemfile Outdated Show resolved Hide resolved
This last set showed up after I adjusted the inline rubocop comments
and I missed it.
…`.standard_todo.yml`

Thanks for the great suggestion @lloeki; this approach does seem to
be a bit cleaner and future-proof.
@@ -104,7 +104,7 @@ def self.libdatadog_folder_relative_to_ruby_extensions_folders(
# the Gem.dir.
expected_ruby_extensions_folders = [
"#{gem_dir}/extensions/platform/extension_api_version/datadog_version/",
"#{gem_dir}/bundler/gems/extensions/platform/extension_api_version/datadog_version/",
"#{gem_dir}/bundler/gems/extensions/platform/extension_api_version/datadog_version/"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think all the cop changes looks super reasonable!
Except for removing the trailing comma from multi-line lists, like this one.

This guarantees that diffs for this list have a useless diff line on the trailing element, just adding a comma.

It's a small thing, but you always makes me slightly confused when I'm reviewing changes and I don't know if someone changed the last element of the list or not. Specially, for long lines, like "#{gem_dir}/bundler/gems/extensions/platform/extension_api_version/datadog_version/", I have to inspect the whole string, instead of it simply never appearing in the diff, because in fact it did not change at all.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeap, I'm with you on that one -- I think trailing comma is the best thing since sliced bread for exactly the reason you pointed out.

But I guess the whole thing with standardrb is to accept it as-is, without customizing so >_>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

accept it as-is

I want to yield such power!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this was already reported: standardrb/standard#611

Copy link
Contributor

@lloeki lloeki Jul 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah the comma thing is the most annoying one ever (with the "double-quote strings everywhere" as a close second and some occasionally very awkward alignment things like foo = case .. when). I mean beyond diffs, in editors "duplicate line" is a very useful thing.

I really don't understand it, I recall their argument being mostly "well you shouldn't care because it's auto formatted on save".

A workaround is:

  • disable the rule(s) at the standard level (standard.yml)
  • enforce the chosen rule(s) at the rubocop level

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it's clear there are ways of bending standardrb to our will on this one.

My question is: do we want to?

I'm leaning towards soft-no just because it goes ~slightly against the spirit of standardrb, but being a soft-no, I'm happy to go in the other direction.

Copy link
Contributor

@lloeki lloeki Aug 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's value in not quibbling. See, this PR seems blocked because of this? ;)

We can revisit later if we find it very annoying. Let's just pick something.

@@ -0,0 +1,3 @@
# For available configuration options, see:
# https://github.com/testdouble/standard
ruby_version: 2.5
Copy link
Member

@marcotc marcotc Jul 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ruby_version: 2.5
ruby_version: 2.5
ignore:
- '**/*':
- Style/TrailingCommaInArguments
- Style/TrailingCommaInArrayLiteral
- Style/TrailingCommaInHashLiteral

I think this is the closest we can get to it 😝
(I'm looking at https://github.com/standardrb/standard/blob/0234926ddf233edfa46479d152dc9aa91171b8fc/config/base.yml#L1765-L1778 for the reference on what to change)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting... I'd be happy to go with this also, I do like leaving the trailing commas.

I guess let's see what the rest of the team thinks? I'm happy to apply the change, but want to see if we can get consensus :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, enforce the commas at the Rubocop level.

Copy link
Contributor

@p-datadog p-datadog left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is my opinion that different code fragments call for different formatting in order to most clearly express the intent and minimize confusion and likelihood of errors. A competent programmer can intelligently choose formatting and style on a case-by-case basis to make their code clear to the reader. I find that at least in Ruby, any programmatically enforced code style produces inferior readability and maintainability compared to an intelligent programmer writing their code with care, because any chosen style is worse for some code fragments and the tool demands the selected formatting all the time including when it makes the code harder to read or harder to maintain.

Specifically for this PR, I am a strong believer in trailing commas to reduce diff noise and my biggest issue with the existing rubocop rule set is that it demands inversion of flow in conditionals by demanding the action if condition pattern for single-statement actions, which appears to be retained in this PR (I don't know if standard also demands this inversion of flow or permits remaining inverted conditions to remain, but would also permit normal flow for single-statement actions.)

I think standard is inferior to rubocop due to trailing comma deletion, and they are either tied for conditional flow inversion or possibly standard is better if it does not force flow inversion. Code as I type it does not satisfy any programmatically defined style, therefore whether rubocop or standard is used I would have them reformat everything. As such, the two tools seem to be to be approximately equal. Therefore I do not object to replacing one with the other.

@lloeki
Copy link
Contributor

lloeki commented Aug 6, 2024

Seems like we by and large don't like the comma thing, so I'm OK to globally disable that rule at the standard level.

find that at least in Ruby, any programmatically enforced code style produces inferior readability and maintainability compared to an intelligent programmer writing their code with care

@p-datadog I agree that intentional formatting helps conveying intent. In practice I've found only a few cases where standard made choices that annoyed me:

  • high irritation: last-item comma, adding items by duping lines is annoying (although autocorrected on save) and diffs become an eyesore
  • medium irritation: first-column foo = if / then / else / end and foo = case / when / else / end alignment, which visually obscures the assignment
  • low irritation: single versus double quotes for strings. I really like the single quote => no interpolation nor escape signalling without having to visually scan the string.

Otherwise the remainder is really helpful and not too intrusive, the set of enforced rules is much smaller than Rubocop defaults, and I've been able to format code and convey intent as usual, in ways that Rubocop rule defaults would prevent and be much more annoying.

@lloeki
Copy link
Contributor

lloeki commented Aug 6, 2024

BTW I chimed in: standardrb/standard#611 (comment)

@ivoanjo
Copy link
Member Author

ivoanjo commented Aug 13, 2024

Thanks everyone! I've excluded the cops that check for trailing commas, so we can continue to use them, and I believe annoyed everyone about this topic so I think we're good to go!

I'll press the green button. As usual, this is still open for revisiting, and please do share any issues/concerns :)

@ivoanjo ivoanjo merged commit 1ced609 into master Aug 13, 2024
186 checks passed
@ivoanjo ivoanjo deleted the ivoanjo/profiler-adopt-standard branch August 13, 2024 18:20
@github-actions github-actions bot added this to the 2.3.0 milestone Aug 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
profiling Involves Datadog profiling
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants