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

Refactoring: Improve Readability and Simplify Logic in JdbcUrlAntFormatter, TxTransactionalMethodInterceptor, MyBatisModule #645

Merged
merged 2 commits into from
Dec 2, 2023

Conversation

kenil121200
Copy link
Contributor

@kenil121200 kenil121200 commented Nov 21, 2023

JdbcUrlAntFormatter.java Changes:

  • Decomposed the monolithic JdbcUrlAntFormatter method into smaller, more focused methods (initializeAppender, processPatternSubstring, handleNonCurlyBracePattern, handleCurlyBracePattern, processKeyResolver, updatePrevPosition, and appendRemainingPatternSubstring) to handle specific parts of the logic.
  • Renamed variables pos and prev to currentIndex and previousIndex for improved clarity and self-documentation, contributing to better variable readability.
  • Method decomposition improved code maintainability and readability, addressing the code smell related to complex methods.

TxTransactionalMethodInterceptor.java Changes:

  • Removed complex conditional logic by decomposing it into separate boolean variables with meaningful names (bothAEsNull, aeNotNullAndRollback, parentAeRollbackConditions), thereby enhancing code readability and removing the code smell associated with complex conditions.
  • The refactoring in TxTransactionalMethodInterceptor aims to simplify logic and make the code more understandable.
  • The invoke method within the TxTransactionalMethodInterceptorclass previously relied on conditional statements to determine TransactionAttribute based on different TxType values.
  • Refactored code introduces a collection of strategies through a Map, associating each TxType with a dedicated instance of a TransactionAttributeStrategy.
  • Several strategy classes (RequiredTransactionAttributeStrategy, RequiresNewTransactionAttributeStrategy, MandatoryTransactionAttributeStrategy, SupportsTransactionAttributeStrategy, and NeverTransactionAttributeStrategy) now implement the TransactionAttributeStrategy interface, encapsulating the creation and handling of specific TransactionAttribute instances.
  • The TransactionAttributeStrategy interface defines a method getTransactionAttribute(), ensuring each strategy class implements it to return the appropriate TransactionAttribute instance.
  • This refactoring allows the interceptor to focus on managing transactions while distinct strategies handle different transaction types independently, enhancing maintainability, scalability, and readability.

MyBatisModule and AbstractMyBatisModule Changes:

  • Moved the getClasses function from MyBatisModule to the abstract class AbstractMyBatisModule to prevent code repetition and potential duplication in other modules or subclasses.
  • Relocating this function to AbstractMyBatisModule facilitates code reuse and reduces redundancy among subclasses that extend this abstract class.
  • By having getClasses in the shared parent class, future modularization by separating functions into different classes can benefit from its accessibility across multiple child classes, supporting better code organization.

Purpose:

  • Enhance code readability, maintainability, and understandability in both files by breaking down complex methods and conditions.
  • Improve variable names for better readability and self-documentation.
  • Remove code smells related to complex methods and conditions to adhere to best practices.
  • The code changes significantly improve maintainability, scalability, and code reusability by leveraging polymorphism to handle transaction attributes and by enhancing code organization through the pulled-up getClasses function.
  • These modifications reduce code duplication, enhance readability, and establish a more robust foundation for future changes or modularization efforts.

Additional Information:

  • All existing test cases are passing with the refactored code in both files.
  • The refactored code in both files has been reviewed for potential bugs and maintains the original behavior of the methods.

This refactoring aims to enhance the codebase quality in JdbcUrlAntFormatter.java and TxTransactionalMethodInterceptor.java by improving readability, maintainability, and removing code smells. Please review the changes and provide feedback. Thank you.

…odInterceptor.java to enhance code readability, maintainability, and remove code smells.
@coveralls
Copy link

coveralls commented Nov 21, 2023

Coverage Status

coverage: 80.562% (+0.5%) from 80.045%
when pulling 5eee4de on kenil121200:master
into 707a165 on mybatis:master.

…odInterceptor.java to enhance code readability, maintainability, and remove code smells.
@kenil121200 kenil121200 changed the title Refactoring: Improve Readability and Simplify Logic in JdbcUrlAntFormatter and TxTransactionalMethodInterceptor Refactoring: Improve Readability and Simplify Logic in JdbcUrlAntFormatter, TxTransactionalMethodInterceptor, MyBatisModule Nov 27, 2023
@hazendaz hazendaz self-assigned this Dec 2, 2023
@hazendaz
Copy link
Member

hazendaz commented Dec 2, 2023

@kenil121200 Thanks for this work, I agree very clean after done. Just a note in future reviews, may be easier to either keep things a little smaller or at least call out to individually check commits. This is a pretty large rework. Its all good, just took longer to review. Do keep things like this coming!

@hazendaz hazendaz merged commit 6b5f63f into mybatis:master Dec 2, 2023
13 of 14 checks passed
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.

3 participants