google / google/google-java-format

Design Smell Refactoring in JavacTokens

未關閉
#1,201 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
Java
星號
6.2k
分支
937
平均合併
6 分鐘
30 天內合併 PR
3

描述

## 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

2. **Maintainability**
- Centralized position management
- Single point for position-related changes
- Better error handling

3. **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

貢獻指南

開啟貢獻指南

研究方向

從 com.google.googlejavaformat.java 中的 JavacTokens.java 開始,檢查 JavacTokens、RawTok 和 CommentWithTextAndPosition 如何管理 pos 和 endPos。在變更設計之前,審查 TokenPosition 提議的職責和現有測試。位置處理實現集中化並封裝、驗證保持一致,而且所有現有測試在不變更公開 API 的情況下仍然通過,即表示完成。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
java
領域
tooling
Issue 類型
重構
難度
5/5
預估耗時
一週以上
活躍度
停滯
描述清晰度
基本清楚
新手友好度
30/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。