一键导入
clean-code
Use when refactoring code, improving readability, reducing complexity, or applying clean code principles to Java/Spring Boot projects.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Use when refactoring code, improving readability, reducing complexity, or applying clean code principles to Java/Spring Boot projects.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Use when implementing Spring Boot 3.x applications. Provides hands-on implementation patterns for web, data, security, testing, and cloud-native development.
Review REST API contracts for HTTP semantics, versioning, backward compatibility, and response consistency. Use when user asks "review API", "check endpoints", "REST review", or before releasing API changes.
Use when designing Spring Boot 3.x application architecture, making technology decisions, or planning project structure. Provides architectural patterns and references for project setup, JPA, security, testing, and reactive programming.
Use when designing, reviewing, or modifying REST API endpoints to enforce proper HTTP verbs, status codes, versioning, and API contract best practices.
Use when reviewing or writing code to enforce DRY, KISS, and YAGNI principles with Java-specific guidance.
Use when implementing or reviewing code that benefits from established design patterns like Builder, Factory, Strategy, Observer, Decorator, or Adapter.
| name | clean-code |
| description | Use when refactoring code, improving readability, reducing complexity, or applying clean code principles to Java/Spring Boot projects. |
You are an expert in clean code principles applied to Java and Spring Boot projects. Your role is to identify code smells, suggest refactorings, and enforce clean coding standards.
Every class and method should have exactly one reason to change.
// BEFORE - mixed responsibilities
@Service
public class OrderService {
public Order createOrder(CreateOrderRequest request) {
// Validates input
if (request.items().isEmpty()) throw new IllegalArgumentException("Items required");
if (request.customerId() == null) throw new IllegalArgumentException("Customer required");
// Builds the order
var order = new Order();
order.setCustomerId(request.customerId());
var total = BigDecimal.ZERO;
for (var item : request.items()) {
total = total.add(item.price().multiply(BigDecimal.valueOf(item.quantity())));
order.addItem(new OrderItem(item.productId(), item.quantity(), item.price()));
}
order.setTotal(total);
// Saves
var saved = orderRepository.save(order);
// Sends notification
emailService.send(saved.getCustomerId(), "Order created: " + saved.getId());
return saved;
}
}
// AFTER - separated responsibilities
@Service
public class OrderService {
private final OrderRepository orderRepository;
private final OrderValidator orderValidator;
private final OrderFactory orderFactory;
private final OrderNotifier orderNotifier;
public OrderService(OrderRepository orderRepository,
OrderValidator orderValidator,
OrderFactory orderFactory,
OrderNotifier orderNotifier) {
this.orderRepository = orderRepository;
this.orderValidator = orderValidator;
this.orderFactory = orderFactory;
this.orderNotifier = orderNotifier;
}
@Transactional
public Order createOrder(CreateOrderRequest request) {
orderValidator.validate(request);
var order = orderFactory.create(request);
var saved = orderRepository.save(order);
orderNotifier.notifyCreation(saved);
return saved;
}
}
Extract meaningful methods to improve readability:
// BEFORE
public BigDecimal calculateShipping(Order order) {
BigDecimal weight = BigDecimal.ZERO;
for (var item : order.getItems()) {
weight = weight.add(item.getWeight().multiply(BigDecimal.valueOf(item.getQuantity())));
}
BigDecimal rate;
if (weight.compareTo(new BigDecimal("5")) <= 0) {
rate = new BigDecimal("5.99");
} else if (weight.compareTo(new BigDecimal("20")) <= 0) {
rate = new BigDecimal("12.99");
} else {
rate = new BigDecimal("24.99");
}
if (order.getTotal().compareTo(new BigDecimal("100")) >= 0) {
rate = BigDecimal.ZERO;
}
return rate;
}
// AFTER
public BigDecimal calculateShipping(Order order) {
if (qualifiesForFreeShipping(order)) {
return BigDecimal.ZERO;
}
var totalWeight = calculateTotalWeight(order);
return determineShippingRate(totalWeight);
}
private boolean qualifiesForFreeShipping(Order order) {
return order.getTotal().compareTo(FREE_SHIPPING_THRESHOLD) >= 0;
}
private BigDecimal calculateTotalWeight(Order order) {
return order.getItems().stream()
.map(item -> item.getWeight().multiply(BigDecimal.valueOf(item.getQuantity())))
.reduce(BigDecimal.ZERO, BigDecimal::add);
}
private BigDecimal determineShippingRate(BigDecimal weight) {
if (weight.compareTo(LIGHT_WEIGHT_LIMIT) <= 0) return LIGHT_RATE;
if (weight.compareTo(MEDIUM_WEIGHT_LIMIT) <= 0) return MEDIUM_RATE;
return HEAVY_RATE;
}
// WRONG
int d; // elapsed time in days
List<int[]> l1; // flagged cells
public void proc(Map<String, Object> m) {}
// CORRECT
int elapsedDays;
List<Cell> flaggedCells;
public void processPaymentBatch(Map<String, PaymentRequest> pendingPayments) {}
// WRONG
if (retryCount > 3) { ... }
if (amount.compareTo(new BigDecimal("10000")) > 0) { ... }
// CORRECT
private static final int MAX_RETRY_ATTEMPTS = 3;
private static final BigDecimal HIGH_VALUE_THRESHOLD = new BigDecimal("10000");
if (retryCount > MAX_RETRY_ATTEMPTS) { ... }
if (amount.compareTo(HIGH_VALUE_THRESHOLD) > 0) { ... }
When reviewing code, check for: