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

Design Smell Refactoring in JavacTokens #1201

Open
bishaddebroy opened this issue Nov 26, 2024 · 0 comments · May be fixed by #1202
Open

Design Smell Refactoring in JavacTokens #1201

bishaddebroy opened this issue Nov 26, 2024 · 0 comments · May be fixed by #1202

Comments

@bishaddebroy
Copy link

Overview

Several design smells were identified in the JavacTokens class of google-java-format project. The goal focused on improving code organization, maintainability, and encapsulation while preserving all functionality.

Location

  • Package: com.google.googlejavaformat.java
  • Class: JavacTokens
  • File: JavacTokens.java

Design Smells Identified

1. Position Management Issues

Smells:

  • Unnecessary Abstraction
  • Multifaceted Abstraction

Symptoms:

  • Position fields (pos, endPos) scattered across classes
  • Position-related logic mixed with other concerns
  • Duplicate position handling in RawTok and CommentWithTextAndPosition

2. Position Validation Logic Issues

Smells:

  • Multifaceted Abstraction
  • Feature Envy

Symptoms:

  • Position validation separated from position data
  • Validation logic duplicated or inconsistent
  • High coupling between classes for position validation

3. Field Organization Issues

Smells:

  • Leaky Encapsulation
  • Duplicate Abstraction

Symptoms:

  • Position fields not properly encapsulated
  • Same fields appearing in multiple classes
  • Inconsistent position field management

Possible Refactoring Solutions

1. Extract Class

  • Created new TokenPosition class to handle position management
  • Centralized all position-related functionality
  • Improved code organization and maintainability
  • Clear separation of responsibilities

2. Move Method

  • Moved position validation logic to TokenPosition class
  • Placed validation near the data it validates
  • Improved cohesion and reduced coupling
  • Ensured consistent validation across usage

3. Move Field

  • Consolidated position fields into TokenPosition class
  • Eliminated field duplication across classes
  • Enforced consistent position handling
  • Enhanced encapsulation of position data

Implementation Impact

Code Quality Improvements

  1. Organization
  • Clear separation of concerns
  • Logical grouping of related functionality
  • Reduced duplication
  1. Maintainability
  • Centralized position management
  • Single point for position-related changes
  • Better error handling
  1. Reliability
  • Consistent position validation
  • Reduced chance of position-related bugs
  • Better type safety

Verification

  • ✅ All existing tests pass
  • ✅ No public API changes
  • ✅ Original functionality preserved
  • ✅ Backward compatible
  • ✅ Follows project coding standards

Conclusion

The implemented refactorings address the identified design smells while maintaining functionality and compatibility. The code is now more maintainable, better organized, and less prone to position-related errors.

Labels

  • design-smells
  • refactoring
  • code-quality
  • java
  • google-java-format
bishaddebroy added a commit to bishaddebroy/google-java-format that referenced this issue Nov 26, 2024
@bishaddebroy bishaddebroy linked a pull request Nov 26, 2024 that will close this issue
3 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 a pull request may close this issue.

1 participant