Design Smell Refactoring in JavacTokens
还没有人认领这个 Issue。
评估
调研方向
从 com.google.googlejavaformat.java 中的 JavacTokens.java 开始,检查 JavacTokens、RawTok 和 CommentWithTextAndPosition 如何管理 pos 和 endPos。在更改设计之前,审查 TokenPosition 拟议的职责和现有测试。位置处理实现集中化和封装、验证保持一致,并且所有现有测试在不更改公共 API 的情况下仍然通过,即表示完成。
由索引模型根据 Issue 内容生成。
描述
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
- Organization
- Clear separation of concerns
- Logical grouping of related functionality
- Reduced duplication
- Maintainability
- Centralized position management
- Single point for position-related changes
- Better error handling
- 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
- 主要语言
- Java
- 星标
- 6.2k
- 派生
- 936
- 平均合并
- 6 分钟
- 30 天内合并 PR
- 3
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
google/google-java-format 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 72/100
google/google-java-format#1094 · 1 条评论 ·
-
难度 5/5 一周以上 新手友好度 15/100
google/google-java-format#1450 · 1 个 reaction ·
-
难度 4/5 3-5 天 新手友好度 55/100
google/google-java-format#1439 · 1 条评论 ·
-
难度 3/5 1-2 天 新手友好度 62/100
google/google-java-format#1436 · 2 条评论 ·
-
难度 3/5 1-2 天 新手友好度 48/100
google/google-java-format#1428 · 3 条评论 ·
查看 google/google-java-format 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 65/100
-
bug
难度 2/5 1-3 小时 新手友好度 75/100
-
难度 2/5 1-3 小时 新手友好度 75/100
elastic/gradle-plugins#157 ·
-
难度 2/5 1-3 小时 新手友好度 75/100
cryptomator/hub#497 ·
-
难度 2/5 1-3 小时 新手友好度 75/100
johanhaleby/occurrent#1120 ·