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

Organize imports with common layout #2

Open
cbeams opened this issue Mar 14, 2018 · 0 comments
Open

Organize imports with common layout #2

cbeams opened this issue Mar 14, 2018 · 0 comments

Comments

@cbeams
Copy link
Contributor

cbeams commented Mar 14, 2018

We keep a master .idea/codeStyles/ directory in the root of the https://github.com/bisq-network/dao repository. This directory contains a Project.xml with an IMPORT_LAYOUT_TABLE to be used across all bisq-* Java projects. We keep this file up to date and copy it to all bisq-* repositories whenever changes to the import layout are necessary.

There is nothing you need to do to set this up. If you're using IDEA to work on bisq-* projects, everything should "just work", because the project you're working on already has a copy of these settings in its own .idea/codeStyles directory.

If you introduce a new import to a bisq-* repository that is not yet listed in the common import layout table, it will get sorted at the bottom of the import list, preceded by several blank lines. This is your clue that you need to add an entry to the import table. You can do this locally in your project, i.e. directly in its .codeStyles/Project.xml, or you can do it at the root bisq-network/dao repository level and copy it everywhere. All that matters is that the change gets committed and that imports get laid out correctly.

The order of imports in the layout table is not alphanumeric. It is based on how high or low level the package is in the overall layering of all packages.

  • bisq.* packages are always sorted at top
  • then packages that are close to Bisq's domain, e.g. org.bitcoinj
  • and from there, infrastructure packages are ordered according to their dependencies on one another or a rough estimation of how high or low level they are relative to one another

This approach to ordering imports is all about providing maximum at-a-glance information to the reader of the source. By simply looking at the name of a class and its imports, a reader should be able to get a good sense of what that class does, what it "touches", what it is responsible for, etc. Indeed, taking an intentional approach to import organization and using this metadata as a code comprehension tool can help provide early warnings about when a class is starting to do too much, get tangled up with other packages and layers, etc.

Recommendation: disable the default "import folding" setting in IDEA. Keep imports "in your face" and use them to your advantage to keep types, packages and layers clean.

Editor > General > Code Folding > Imports

See:

cbeams added a commit to bisq-network/dao that referenced this issue Mar 14, 2018
This IDEA import layout table contains entries for all packages in use
throughout all current `bisq-*` repositories. See the linked issue for
details and rationale.

This commit also adds a `copy-import-layout.sh` script for convenience
when copying changes to this file to all `bisq-*` repositories that
already contain an .idea/codeStyles directory. Note that this script
assumes one is using `mr` and the `.mrconfig` file in the root of this
repository to manage bisq-network GitHub org repositories under a single
root.
cbeams added a commit to bisq-network/bisq-common that referenced this issue Mar 14, 2018
cbeams added a commit to bisq-network/bisq-core that referenced this issue Mar 14, 2018
cbeams added a commit to bisq-network/bisq that referenced this issue Mar 14, 2018
cbeams added a commit to bisq-network/bisq-monitor-deprecated that referenced this issue Mar 14, 2018
cbeams added a commit to bisq-network/bisq-p2p that referenced this issue Mar 14, 2018
cbeams added a commit to cbeams/bisq-pricenode that referenced this issue Mar 14, 2018
cbeams added a commit to bisq-network/bisq-seednode that referenced this issue Mar 14, 2018
cbeams added a commit to bisq-network/bisq-statsnode that referenced this issue Mar 14, 2018
cbeams added a commit to abigail830/bisq-core that referenced this issue Apr 10, 2018
 - Remove dependency on bisq-common from build.gradle
 - Use camel case spelling of 'WeChat' consistently
 - Organize imports with common layout (bisq-network/style#2)
cbeams added a commit to abigail830/bisq-desktop that referenced this issue Apr 10, 2018
 - Use camel case spelling of 'WeChat' consistently
 - Organize imports with common layout (bisq-network/style#2)
@chirhonul
Copy link

I believe the link to the Project.xml file that defines the desired order of imports should be updated to refer to https://github.com/bisq-network/bisq/blob/master/.idea/codeStyles/Project.xml (there doesn't seem to be such a file under the dao repo currently).

I will note that this custom ordering seems complex, compared to the Google Java style guide:

This rule does make it somewhat harder to contribute using non-IDEA editors, since one needs to refer to the Project.xml for each line and compare it with other imports to find the correct ordering.

It was also unclear to me:

  • whether to separate import statement blocks with newlines (i.e between bisq.* packages and "Packages close to bisq's domain")
  • where to order static imports (I ended up modifying my PR to not use them to sidestep this issue)

chirhonul added a commit to chirhonul/bisq-core that referenced this issue Aug 1, 2018
Also drops static imports as it is unclear how to order them.

Attempts to comply with bisq-network/style#2.
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

No branches or pull requests

2 participants