Skip to content

Conversation

woodruffw
Copy link
Member

Another low-priority PR, just cleans up the code a bit and adds a .rubocop.yml for future style enforcement.

rubocop complains about roughly 100 other things in the lib/ tree, although most of them are even less significant (methods over 10 lines, for example).
All of the unresolved violations are here: ruby-macho-rubocop.txt

cc @UniqMartin

@woodruffw woodruffw added this to the Release 1.0.0 milestone Aug 1, 2016
@woodruffw woodruffw mentioned this pull request Aug 1, 2016
9 tasks
@@ -0,0 +1,6 @@
StringLiterals:
Enabled: false
Copy link
Contributor

Choose a reason for hiding this comment

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

You're missing the Style/ prefix in the preceding line and instead of simply disabling the check, it's better to reconfigure it to our preferred style:

Style/StringLiterals:
  EnforcedStyle: double_quotes

You also most likely want to add the following for consistency with the above and with Homebrew code in general:

Style/StringLiteralsInInterpolation:
  EnforcedStyle: double_quotes

Copy link
Contributor

Choose a reason for hiding this comment

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

The second part will also take care of some of the complaints that remained in the RuboCop output.

@woodruffw
Copy link
Member Author

Sorry for the rookie mistakes regarding RuboCop - this is my first time configuring/using it 😅.

@UniqMartin
Copy link
Contributor

Sorry for the rookie mistakes regarding RuboCop - this is my first time configuring/using it

No worries, I'm happy to point you into the right direction (or what I believe to be the right direction). I certainly had more experience tweaking those rules in the context of Homebrew. Overall I'm very happy that you came up with this PR on your own. 👍

@UniqMartin
Copy link
Contributor

rubocop complains about roughly 100 other things in the lib/ tree, although most of them are even less significant (methods over 10 lines, for example). All of the unresolved violations are here: ruby-macho-rubocop.txt

Some of them will already vanish after addressing my comments. Others, like the length and complexity metrics are something I'm less convinced of (or at least I find the limits to be quite harsh), thus I'd be personally alright with:

Metrics/AbcSize:
  Enabled: false
Metrics/ClassLength:
  Enabled: false
Metrics/CyclomaticComplexity:
  Enabled: false
Metrics/MethodLength:
  Enabled: false
Metrics/ModuleLength:
  Enabled: false
Metrics/ParameterLists:
  Enabled: false
Metrics/PerceivedComplexity:
  Enabled: false

We can sort out most of the remaining items either in the next iteration of this PR or just ignore them for the time being and deal with them in a later PR.

Pro Tip: Use rubocop -DES for extra details like the cop name responsible for the output and links to the corresponding section of the style guide. I also like to append -f progress -f offenses to the argument list to get an additional summary of the various types of offenses that were raised.

@woodruffw
Copy link
Member Author

Metrics/AbcSize:
Enabled: false
Metrics/ClassLength:
Enabled: false
Metrics/CyclomaticComplexity:
Enabled: false
Metrics/MethodLength:
Enabled: false
Metrics/ModuleLength:
Enabled: false
Metrics/ParameterLists:
Enabled: false
Metrics/PerceivedComplexity:
Enabled: false

This looks good to me! I'm going to make all the changes you've recommended tonight.

Thanks for the tip about the additional RuboCop flags 😄

@woodruffw woodruffw force-pushed the general-refactoring branch from 39b8e03 to 2eb518a Compare August 5, 2016 23:28
@woodruffw
Copy link
Member Author

RuboCop still complains about unaligned parameters/arrays/cases, so I'm probably going to add those to the .rubocop.yml unless you think we should address them here.

There's also the matter of the unfortunately named MachOFile#set_ncmds and MachOFile#set_sizeofcmds. How do update_ncmds and update_sizeofcmds sound to you? Alternatively, we could do something like raw_ncmds=...

@UniqMartin
Copy link
Contributor

RuboCop still complains about unaligned parameters/arrays/cases, so I'm probably going to add those to the .rubocop.yml unless you think we should address them here.

I just went ahead and pushed a few commits that address these, partially to see for myself how this will change the code. I'd say that we should address them (I only focused my attention on the lib/ directory; it is clean except for one documentation complaint). Feel free to rearrange, drop, or squash any of my commits that I so boldly pushed to your branch.

There's also the matter of the unfortunately named MachOFile#set_ncmds and MachOFile#set_sizeofcmds. How do update_ncmds and update_sizeofcmds sound to you?

I'm in favor of update_*. That's already the verb we use in the documentation comment.

MachO::Section64
else
raise ArgumentError, "not a valid segment"
end
Copy link
Contributor

Choose a reason for hiding this comment

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

If this looks unappealing to you, we could steal the following snippet from Homebrew's .rubocop.yml to gain a bit more flexibility with these:

# we prefer compact if-else-end/case-when-end alignment
Lint/EndAlignment:
  AlignWith: variable
Style/CaseIndentation:
  IndentWhenRelativeTo: end

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I do generally prefer the original form.
I'll add those instructions to the .rubocop.yml.

Copy link
Contributor

Choose a reason for hiding this comment

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

👍

I can see arguments for both styles, but I'm happy with either (but more used to the unindented style due to my Homebrew work).

@woodruffw
Copy link
Member Author

Looks great to me! I'll squash these down into the RuboCop and general refactoring commits in a moment.

I only focused my attention on the lib/ directory; it is clean except for one documentation complaint)

As did I. It might make sense at some point in the future to align the unit tests with our RuboCop enforced style, but that's not particularly high on my list of priorities for ruby-macho 😄

segments.each do |seg|
sections(seg).each do |sect|
next if sect.size == 0
next if sect.size == 0 # rubocop:disable Style/ZeroLengthPredicate
Copy link
Member Author

@woodruffw woodruffw Aug 6, 2016

Choose a reason for hiding this comment

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

Looks like RuboCop still complains here about a Style/NumericPredicate violation. I'd personally just prefer zero? here, since it's equally readable (IMO, of course) and we don't have to disable two checks in-line.

(Ignore all of that, replacing this with Section#empty?)

Copy link
Contributor

Choose a reason for hiding this comment

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

👍 To quote from the other related code comment:

I was still on RuboCop 0.41.2. That's why I didn't notice this was now causing multiple complaints and this suggestion isn't exactly practical.

@woodruffw woodruffw force-pushed the general-refactoring branch from 6f753fc to d169801 Compare August 6, 2016 15:09
@UniqMartin
Copy link
Contributor

I only focused my attention on the lib/ directory; it is clean except for one documentation complaint)

As did I. It might make sense at some point in the future to align the unit tests with our RuboCop enforced style, but that's not particularly high on my list of priorities for ruby-macho 😄

Great! I think it's sufficient to tweak those style issues incrementally as the code gets modified (or maybe in a follow-up PR for the test suite as a whole). A good way to improve incrementally is to let your editor highlight RuboCop style violations for you while coding. I'm using SublimeLinter with its RuboCop plugin for that and loving it.

.rubocop.yml Outdated
Style/TrailingCommaInArguments:
EnforcedStyleForMultiline: no_comma

# TODO: disable in the future
Copy link
Contributor

Choose a reason for hiding this comment

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

Should that be “re-enable in the future”?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep. I was thinking disable as in "disable these lines", but re-enable makes much more sense.

@woodruffw
Copy link
Member Author

A good way to improve incrementally is to let your editor highlight RuboCop style violations for you while coding. I'm using SublimeLinter with its RuboCop plugin for that and loving it.

Thanks! I knew about SublimeLinter, but I didn't know about its RuboCop plugin. I'll install it now 😄

@woodruffw woodruffw force-pushed the general-refactoring branch from d169801 to a4eecf8 Compare August 6, 2016 22:48
raw_string = view.raw_data.slice(lc_str_abs..lc_end)
@string, null_byte, _ = raw_string.partition("\x00")
raise LCStrMalformedError.new(lc) if null_byte.empty?
@string, null_byte, = raw_string.partition("\x00")
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not liking this resolution. The trailing comma on the left-hand side is too easy to overlook. How about the following?

@string, null_byte, _padding = raw_string.partition("\x00")

Copy link
Member Author

Choose a reason for hiding this comment

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

I was thinking the same thing. _padding sounds good to me!

@woodruffw woodruffw force-pushed the general-refactoring branch from a4eecf8 to 5ac8025 Compare August 6, 2016 22:51
@woodruffw woodruffw force-pushed the general-refactoring branch 2 times, most recently from 72613bc to 6891bcd Compare August 7, 2016 15:28
@woodruffw woodruffw force-pushed the general-refactoring branch from 6891bcd to e0adad9 Compare August 7, 2016 15:31
@woodruffw
Copy link
Member Author

Rebased; I'm happy with the state of this PR if you are 🎉

@UniqMartin
Copy link
Contributor

Rebased … and checked whether rubocop complains about any of the new/changed code? 😉

@woodruffw
Copy link
Member Author

Hmm, RuboCop doesn't like that we never use the options arguments to the modification methods inMachOFile. I guess I'll make them _options if (until?) we end up needing them.

@woodruffw woodruffw force-pushed the general-refactoring branch from e0adad9 to e089598 Compare August 7, 2016 15:49
@UniqMartin
Copy link
Contributor

Could you try and adopt a workflow that looks something like this?

  1. Make a change.
  2. Self-review and basic sanity check (in this case re-running rubocop).
  3. Push change.

Sorry if I'm wronging you with this, but it feels like you're sometimes skipping the second step. I don't think I should be pointing out stuff like in the following terminal output and we could be both more productive if I didn't have to, not only because of the incurred roundtrip delay.

$ rubocop -D -f simple lib
== lib/macho/load_commands.rb ==
C:403:  1: Style/EmptyLines: Extra blank line detected.
== lib/macho/open.rb ==
C:  1:  1: Style/Documentation: Missing top-level module documentation comment.

12 files inspected, 2 offenses detected

(And if in doubt: No, I'm not referring to the second offense that has been there from the start. I'm talking about the other one that has been introduced due to the rebase.)

@woodruffw
Copy link
Member Author

Sorry if I'm wronging you with this

You're not, I haven't been proactive about checking work that I think is correct. Sorry for imposing the extra burden on you, it's not fair of me to do.

(That last offense looks like another one of RuboCop's screw-ups - the MachO module is documented in macho.rb.)

Bring code in line with RuboCop.

Freeze constant objects, raise exceptions without #new where possible.
Eliminate unnecessary `self` prefixes and correct formatting.
Use 1.9-style hashes where possible.
Align parameters and array items.
Rename 'set_*' methods to 'update_*'.
Rename 'get_*' methods to 'populate_*'
@woodruffw woodruffw force-pushed the general-refactoring branch from e089598 to cba3369 Compare August 7, 2016 16:48
@UniqMartin UniqMartin merged commit 3eb6ac3 into master Aug 7, 2016
@UniqMartin UniqMartin deleted the general-refactoring branch August 7, 2016 17:03
@UniqMartin
Copy link
Contributor

Thanks! 🙇

(That last offense looks like another one of RuboCop's screw-ups - the MachO module is documented in macho.rb.)

We can look into this later. I'm not 100% sure how RuboCop is performing the check, but I'm relatively confident it works on a file by file basis and thus cannot correlate information from different files. (It's perfectly normal to let it check one file only.) Since modules and classes can be reopened any time, I suspect it's using some heuristics to determine whether the module is new (and thus needs to be documented) or is just being reopened and added to …

@woodruffw
Copy link
Member Author

Yeah, that would make sense. For the time being I'm happy with just ignoring that line in rubocop's output (disabling it probably wouldn't be good, since we don't want to leave future modules/classes accidentally undocumented).

@UniqMartin
Copy link
Contributor

Yes, agreed. It's not exactly bothering me and we can ignore it for the time being.

Slightly off topic, but I guess you'd like to cut a new release soon? Even if it's not 1.0 time yet, I think we have accumulated quite a few important changes since the last release and it would make sense to cut a new one. I think it would also makes sense to bring this new release to Homebrew proper, so that the test bot can throw some real world binaries at it and we can see how this goes, not to mention that we'll need to make some minor adjustments to the code that is bridging between the Homebrew-internal API and the ruby-macho API.

@woodruffw
Copy link
Member Author

I was just about to! It'll be up in a moment.

@woodruffw
Copy link
Member Author

I'll also open a PR on brew to bring the updated ruby-macho in and update our interfacing code.

@lock lock bot added the outdated label Jan 1, 2020
@lock lock bot locked as resolved and limited conversation to collaborators Jan 1, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants