Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Design Smell Refactoring in JavacTokens

未关闭
#1,201 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
30/100
Issue 类型
重构
描述清晰度
基本清楚
活跃度
停滞
技术栈
java
领域
tooling

调研方向

从 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
  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
主要语言
Java
星标
6.2k
派生
936
平均合并
6 分钟
30 天内合并 PR
3

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

google/google-java-format 的其他 Issue

查看 google/google-java-format 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。