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

OO Maintenance: Better checks, clean up dead code, refactoring #11779

Merged
merged 15 commits into from
Sep 23, 2024

Conversation

subhramit
Copy link
Collaborator

@subhramit subhramit commented Sep 16, 2024

  • Clean up some dead code (due to redundant conditions).
  • Merge pre-conditions more efficiently, use real condition checks instead of flags always being set to true/false.
  • Follow-up to the beginning of refactoring at Add setting: always add "Cited on pages" text to JStyles. #11732 (comment) - since OO preferences are now declared at class level, it makes sense to use it everywhere instead of calling the getter again and again. In other cases as well, this PR removes extra get calls for null checks.
  • Minimize usage of preferences by passing specific preferences via constructor.
  • Use sub-typed exception handling in certain cases.
  • Use more relevant names for variables.

Mandatory checks

  • Change in CHANGELOG.md described in a way that is understandable for the average user (if applicable)
  • Tests created for changes (if applicable)
  • Manually tested changed features in running JabRef (always required)
  • Screenshots added in PR description (for UI changes)
  • Checked developer's documentation: Is the information available and up to date? If not, I outlined it in this pull request.
  • Checked documentation: Is the information available and up to date? If not, I created an issue at https://github.com/JabRef/user-documentation/issues or, even better, I submitted a pull request to the documentation repository.

@subhramit subhramit added openoffice/libreoffice type: code-quality Issues related to code or architecture decisions labels Sep 16, 2024
@@ -601,12 +587,12 @@ private boolean checkThatEntriesHaveKeys(List<BibEntry> entries) {
Optional<BibDatabaseContext> databaseContext = stateManager.getActiveDatabase();
if (citePressed && databaseContext.isPresent()) {
// Generate keys
CitationKeyPatternPreferences prefs = preferences.getCitationKeyPatternPreferences();
CitationKeyPatternPreferences citationKeyPatternPreferences = preferences.getCitationKeyPatternPreferences();
Copy link
Member

Choose a reason for hiding this comment

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

Maybe, you can have these passed in the constructor - and get rid of preferences parameter alltogheter? ^^

Copy link
Collaborator Author

@subhramit subhramit Sep 16, 2024

Choose a reason for hiding this comment

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

That would be the ideal level of abstraction, but preferences would be needed here:

image

Reason for moving openOfficePreferences in the constructor initialization block was that it is the core preference used all over the class (22 usages), whereas the rest of the preferences (ExternalApplicationsPreferences, LayoutFormatterPreferences, CitationKeyPatternPreferences) are used just used once in within their respective scopes (three times all together in total), thus declared locally.

Copy link
Collaborator Author

@subhramit subhramit Sep 16, 2024

Choose a reason for hiding this comment

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

@koppor I tried to clean it up a bit further by passing all the preference subtypes via constructor. Although I couldn't get rid of preferences, I could minimize its usage to this one place (above comment). Check commit f759087. If this is not useful, we can revert the commit.

Advantage (imo) - more inversion of control (gets rid of all getters).
Ping: @calixtus

calixtus
calixtus previously approved these changes Sep 16, 2024
@@ -511,12 +511,6 @@ private void pushEntries(CitationType citationType, boolean addPageInfo) {
}

final BibDatabaseContext bibDatabaseContext = stateManager.getActiveDatabase().get();
if (bibDatabaseContext == null) {
Copy link
Member

Choose a reason for hiding this comment

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

Future: It is ok to add assert statements.

Example:

assert bibDatabaseContext  != null

OK, one needs to add -ea somehow: https://stackoverflow.com/a/18168305/873282 -- I would have bet that I saw assertion errors when developing...

The long discussion at https://stackoverflow.com/q/2758224/873282 says: do something else, but I like assertions:

  1. More semantic meaning than removed code
  2. More semantic meaning than code comments
  3. Runs in debug mode only (in contrast to Objects.requireNonNull
  4. Does not clutter the code with @NonNull annotations, which need a separate framework for checking - (https://github.com/kelloggm/checkerframework-gradle-plugin)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Okay, will follow that.
Reason for removal here was that bibDatabaseContext could never be null here.

Copy link
Member

Choose a reason for hiding this comment

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

Sure. At the time of the code. But what if there is a future refactoring. One could argue that the whole research of pre- and post conditions is useless, because the author was sure at when writing the code 🤣🤣

This kind of code (or JSpecify annitations are useful for working on the code at a later point of time. To avoid regressions.

Side track: Another tool for avoiding regressions is OpenFastTrace - see https://devdocs.jabref.org/requirements/ for details ^^.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If it is to ensure the precondition, it is better to revert this change and use the dedicated error dialog instead of using an internal assertion. Will be updated in latest commit.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Checks for empty database and databasecontext merged in (updated) lines 509-515.

Comment on lines -401 to -402
boolean hasStyle = true; // (style != null);
boolean hasDatabase = true; // !getBaseList().isEmpty();
Copy link
Member

Choose a reason for hiding this comment

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

These two comments (non-null check / isEmpty) are typical assert statements.

Copy link
Collaborator Author

@subhramit subhramit Sep 17, 2024

Choose a reason for hiding this comment

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

Okay wait, don't merge yet, I'll see if I can put those conditions/assertions (replacing style->currentStyle) instead of complete removal. I don't know if there were reasons these were commented and true was hardcoded.

koppor
koppor previously requested changes Sep 17, 2024
Copy link
Member

@koppor koppor left a comment

Choose a reason for hiding this comment

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

As discussed: use of assert

@subhramit subhramit marked this pull request as draft September 18, 2024 08:12
Siedlerchr
Siedlerchr previously approved these changes Sep 20, 2024
@Siedlerchr
Copy link
Member

form my point of view okay now

@subhramit
Copy link
Collaborator Author

I'll try to finish this up by tomorrow. Not much left. Just checking if we can work without listeners (I did something similar when disabling buttons during the csl project)...or else just add a few assertions.

@subhramit subhramit dismissed stale reviews from calixtus and Siedlerchr via 7958e7d September 22, 2024 00:37
@subhramit subhramit marked this pull request as ready for review September 22, 2024 00:39
@subhramit
Copy link
Collaborator Author

subhramit commented Sep 22, 2024

Whenever available, I request one of you to check the diff since the last changes and give an approval for merge.

@subhramit subhramit changed the title OO Maintenance: Clean up dead code, refactoring OO Maintenance: Clean up dead code, better checks, refactoring Sep 22, 2024
@subhramit subhramit changed the title OO Maintenance: Clean up dead code, better checks, refactoring OO Maintenance: Better checks, clean up dead code, refactoring Sep 22, 2024
@subhramit subhramit added this pull request to the merge queue Sep 23, 2024
Merged via the queue into JabRef:main with commit 0e10cef Sep 23, 2024
23 checks passed
@subhramit subhramit deleted the oo-maintenance branch September 23, 2024 18:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
openoffice/libreoffice type: code-quality Issues related to code or architecture decisions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants