Skip to content

Refactor and simplify#743

Merged
freakboy3742 merged 19 commits into
beeware:mainfrom
danyeaw:refactoring
May 25, 2022
Merged

Refactor and simplify#743
freakboy3742 merged 19 commits into
beeware:mainfrom
danyeaw:refactoring

Conversation

@danyeaw

@danyeaw danyeaw commented May 24, 2022

Copy link
Copy Markdown
Member

I did some refactoring chores to clean up:

  • Explicitly raise from previous errors
  • Return directly from inline variables
  • Simplify if statements, use if expressions when available
  • Converted a couple of strings to f-strings
  • Use list comprehensions

PR Checklist:

  • All new features have been tested
  • All new features have been documented
  • I have read the CONTRIBUTING.md file
  • I will abide by the code of conduct

@freakboy3742 freakboy3742 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For the most part, very straightforward and welcome improvements (especially the passing of exception context).

There are a couple of themes in my review comments:

  • Consistency in the as e addition. It's inconsistent where you've used as e, as error, as exception; unless there's a syntactic ambiguity, I'd argue it's better to stick to as e across the board.
  • Line length. I've got flake8 set up to accept 119 character lines; but that shouldn't be taken as an invitation to make lines long just because you can :-). There's a lot of places (some of which I've flagged explicitly) where pulling out the newlines dramatically reduces readability. In particular, complex comprehensions benefit from being split over multiple lines because the operative clauses are need to be independently understood. Breaking a single 119 char line into 3 lines that are "the pattern for each item", "the list being iterated", "the condition on list items" makes it a lot easier to digest what is going on. The 119 line limit is really there because there are rare occasions where lines need to run long; unless you've got a good reason, black's 89 char line limit (80+10%) is a good rule of thumb to aim for.
  • Similarly, while 119 characters allows you to reduce short if/else statements to a single line, that's not always desirable; Yes, an if-then statement is 4 lines - but we're not paying Github by line count :-) If there's even slightly complex logic in an if/else statement, I'd argue using multiple lines is preferable, even if the single statement will fit in 119 characters. Broadly speaking, the only context in which I really like X if Y else Z as a syntax construction is in a comprehension; in almost every other situation, I find the condensed syntax harder to read.

Comment thread src/briefcase/commands/create.py Outdated
Comment thread src/briefcase/commands/create.py Outdated
Comment thread src/briefcase/config.py Outdated
Comment thread src/briefcase/integrations/android_sdk.py Outdated
Comment thread src/briefcase/integrations/android_sdk.py Outdated
Comment thread src/briefcase/integrations/docker.py Outdated
Comment thread src/briefcase/integrations/docker.py Outdated
Comment thread src/briefcase/integrations/wix.py Outdated
Comment thread src/briefcase/integrations/xcode.py Outdated
Comment thread src/briefcase/platforms/windows/msi.py Outdated
@danyeaw

danyeaw commented May 24, 2022

Copy link
Copy Markdown
Member Author

Hi @freakboy3742! Thanks so much for all the feedback, I made the updates. On the formatting side of things, have you considered using black? I know we discussed it in the past, but boy getting rid of the discussion on formatting is such a nice cognitive overhead reduction 👍😄.

@freakboy3742 freakboy3742 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

One bug (picked up by the test suite), and one minor (and possibly optional, if you're not feeling it) cleanup flagged inline; otherwise this is looking good.

As for black - that's on my list to look at :-) I was primarily waiting until black declared themselves "non-beta"; that happened earlier this year, so it's now down to the mechanics.

I've just run black over the Briefcase codebase, and the changes are 80% '->'"' by volume; there are a handful of other changes that grate against me a little bit, but not as much as the overhead of having to review code format :-) (or, in some cases, can be made palatable by making a small tweak to syntax).

The biggest impediment to "just doing it" is working out what black looks like from a CI and repo config perspective. If we're going to adopt black, it needs to be applied automatically without any explicit interaction from users (because I don't want to adopt a tool and then spend every PR review saying "Could you run black over this code"). Investigating what 'best practice" looks like is the next step on this journey.

Comment thread src/briefcase/integrations/wix.py Outdated
Comment thread src/briefcase/integrations/xcode.py Outdated
@danyeaw

danyeaw commented May 24, 2022

Copy link
Copy Markdown
Member Author

PR updated, thanks for the feedback!

The biggest impediment to "just doing it" is working out what black looks like from a CI and repo config perspective.

Mind if I submit a PR? I can set things up with pre-commit and black.

@freakboy3742

Copy link
Copy Markdown
Member

Mind if I submit a PR? I can set things up with pre-commit and black.

Go right ahead! Make sure the PR has any docs (either in the PR, or ideally part of the contribution guide) to describe what contributors need to do to adapt.

@codecov

codecov Bot commented May 25, 2022

Copy link
Copy Markdown

Codecov Report

Merging #743 (9cfb1ae) into main (ec1e72c) will decrease coverage by 0.02%.
The diff coverage is 90.63%.

Impacted Files Coverage Δ
src/briefcase/integrations/cookiecutter.py 0.00% <0.00%> (ø)
src/briefcase/integrations/git.py 55.55% <0.00%> (ø)
src/briefcase/platforms/windows/msi.py 81.72% <22.22%> (-0.39%) ⬇️
src/briefcase/commands/new.py 93.93% <50.00%> (-0.10%) ⬇️
src/briefcase/platforms/iOS/xcode.py 94.73% <72.72%> (ø)
src/briefcase/platforms/linux/appimage.py 97.11% <85.71%> (ø)
src/briefcase/integrations/android_sdk.py 98.99% <97.26%> (-0.01%) ⬇️
src/briefcase/commands/base.py 98.23% <100.00%> (ø)
src/briefcase/commands/create.py 99.63% <100.00%> (-0.01%) ⬇️
src/briefcase/commands/dev.py 95.16% <100.00%> (-0.08%) ⬇️
... and 11 more

@freakboy3742 freakboy3742 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Awesome - thanks for the cleanups!

@freakboy3742 freakboy3742 merged commit fd9744e into beeware:main May 25, 2022
@danyeaw danyeaw deleted the refactoring branch May 25, 2022 12:59
@mhsmith mhsmith mentioned this pull request May 30, 2022
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants