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

Refactored code to resolve implementation as well as design smells and increase maintainability. #233

Closed
wants to merge 4 commits into from

Conversation

Drashti2608
Copy link

@Drashti2608 Drashti2608 commented Mar 29, 2024

I introduced below given test cases utilizing Mockito to enhance the testing coverage of the project.

  1. testGenerateNewKey(): To test GenerateNewKey() method of UtilsRandom class.
  2. indexOf_singleOccurrence(): To test indexOf() method of UtilsString class.
  3. testExpectInput(): To test expectInput() method of UtilsLogger class.

Furthermore, I have also attempted to remove some of the implementation code smells by performing below given methods.

  1. Performed 'Extract method' on installMod() method to reduce the Cyclomatic complexity of the method..
  2. Performed 'Decompose conditional' to reduce the complexity of conditional statement in preVisitDirectory() method of FileManager class.
  3. Performed 'Extract method' on SyncFilesManager() method of SyncFileManager class to reduce the Cyclomatic complexity of the method.
  4. Renamed variables 'mods_update_check_name_for_mod_loader' and 'plugins_updater_web_database_min_usages' to remove the code smell of 'Long Identifier'.
    I have used 'Designite' tool to detect the smells present in the code.

In further refactoring I have performed below given methods to resolve the design smells present in the code:

  1. Extract class:
  • FileManager.java class is handling multiple responsibilities. I've refactored the code by extracting related methods into a new class, FileSearcher.java. This enhances modularity and maintainability by focusing each class on specific responsibilities.
  • ConFileManager.java class has multiple responsibilities, which makes it less cohesive. Refactored the code by creating a new class, FileSystemUtils.java, and extracting the related method to that class.
  1. Replace conditional using polymorphism:
  • In the MainWindow class, Made changes to improve how we detect the type of theme. Instead of using many if-statements, I introduced ThemeSetup interface. All the theme types implement this interface. This change makes the code easier to manage and ready for adding new types of themes in the future. Hence, MainWindow class also adheres to Open Closed principle.
  1. Move method:
  • From TaskServerUpdater class, moved a couple of methods to UpdaterConfig class to remove the design smell of Feature envy. These methods belonged more to UpdaterConfig class than TaskServerUpdater class.

Copy link
Owner

@Osiris-Team Osiris-Team left a comment

Choose a reason for hiding this comment

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

Thanks a lot for your PR!

If you are interested in larger PRs check out these issues:

Copy link
Owner

Choose a reason for hiding this comment

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

These names are long yes, but because they follow a standard like all the other fields in this class, which is to represent the yaml keys. Please revert these.

Copy link
Owner

Choose a reason for hiding this comment

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

are all those extra imports are needed?

}

@Test
public void testTextIsClearedOnFocus() {
Copy link
Owner

Choose a reason for hiding this comment

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

this isn't an existing feature, this test isnt needed

pom.xml Outdated
Copy link
Owner

Choose a reason for hiding this comment

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

List<File> filesToSendTo = new ArrayList<>();
//List<String> ipsToSendTo = new ArrayList<>();
Copy link
Owner

Choose a reason for hiding this comment

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

keep these comments since they are relevant for future feature implementations

Copy link
Author

Choose a reason for hiding this comment

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

Thank you so much for reviewing my pull request!
I will look into the code again considering the changes you suggested!

@Drashti2608 Drashti2608 changed the title Added Test cases to improve branch coverage and Refactored code to resolve implementation code smells. 2)Refactored code to resolve implementation code smells and increase maintainability. Mar 31, 2024
@Drashti2608 Drashti2608 changed the title 2)Refactored code to resolve implementation code smells and increase maintainability. Refactored code to resolve implementation code smells and increase maintainability. Mar 31, 2024
@Drashti2608 Drashti2608 changed the title Refactored code to resolve implementation code smells and increase maintainability. Refactored code to resolve implementation as well as design smells and increase maintainability. Mar 31, 2024
@Drashti2608
Copy link
Author

I have made the suggested changes to the code and have committed them. Could you please review them?
I have also performed Move method, Extract class method and Replace conditional with polymorphism method to enhance the code readability and maintainability. Please have a look at them as well.

Copy link
Owner

@Osiris-Team Osiris-Team left a comment

Choose a reason for hiding this comment

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

Sorry, I somehow forgot to review this.

Copy link
Owner

Choose a reason for hiding this comment

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

revert this.
only functions strictly related to the config should be allowed here.

private static MinecraftMod initializeMinecraftMod(File modsDir, String tempName, String input) {
MinecraftMod mod = new MinecraftMod(new File(modsDir + "/" + tempName).getAbsolutePath(), tempName, "0", "",
"0", "0", "");
String repo;
Copy link
Owner

Choose a reason for hiding this comment

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

must be initialized: String repo = "modrinth";


try { // SPIGOT
Copy link
Owner

Choose a reason for hiding this comment

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

keep these comments

Copy link
Owner

Choose a reason for hiding this comment

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

the FileManager is basically a FileSearcher, no need to split that up, or if you really want to split it up, make each method into its own class, because yes they are pretty long.

Copy link
Owner

Choose a reason for hiding this comment

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

these changes I really like, good job


Consumer<FileEvent> onFileChangeEvent = event -> {
private Consumer<FileEvent> defineFileChangeEventHandler(List<File> filesToSendTo) {
Copy link
Owner

Choose a reason for hiding this comment

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

shorter: createFileChangeListener

@@ -64,7 +64,7 @@ public boolean open() throws Exception {
} else if (requestType == 6) {
doProtocolForCopyOrCutFiles();
} else if (requestType == 7) {
doProtocolForSendingRoots();
FileSystemUtils.sendRoots(dos);
Copy link
Owner

Choose a reason for hiding this comment

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

previous way was fine, no need to split into another class, since this is more related to connection stuff, than filesystem stuff

} else {
AL.warn("The selected theme '" + generalConfig.autoplug_system_tray_theme.asString() + "' is not a valid option! Using default.");
if (!FlatLightLaf.setup()) throw new Exception("Returned false!");
ThemeSetup themeSetup = getThemeSetup(generalConfig.autoplug_system_tray_theme.asString());
Copy link
Owner

Choose a reason for hiding this comment

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

yes its nicer, but if you compare the additional amount of code needed before this, I don't think its better.
revert.

@Osiris-Team
Copy link
Owner

@Drashti2608 any news?

@Osiris-Team Osiris-Team closed this Jul 7, 2024
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