$npx -y skills add decebals/claude-code-java --skill clean-codeClean Code principles (DRY, KISS, YAGNI), naming conventions, function design, and refactoring. Use when user says "clean this code", "refactor", "improve readability", or when reviewing code quality.
| 1 | # Clean Code Skill |
| 2 | |
| 3 | Write readable, maintainable code following Clean Code principles. |
| 4 | |
| 5 | ## When to Use |
| 6 | - User says "clean this code" / "refactor" / "improve readability" |
| 7 | - Code review focusing on maintainability |
| 8 | - Reducing complexity |
| 9 | - Improving naming |
| 10 | |
| 11 | --- |
| 12 | |
| 13 | ## Core Principles |
| 14 | |
| 15 | | Principle | Meaning | Violation Sign | |
| 16 | |-----------|---------|----------------| |
| 17 | | **DRY** | Don't Repeat Yourself | Copy-pasted code blocks | |
| 18 | | **KISS** | Keep It Simple, Stupid | Over-engineered solutions | |
| 19 | | **YAGNI** | You Aren't Gonna Need It | Features "just in case" | |
| 20 | |
| 21 | --- |
| 22 | |
| 23 | ## DRY - Don't Repeat Yourself |
| 24 | |
| 25 | > "Every piece of knowledge must have a single, unambiguous representation in the system." |
| 26 | |
| 27 | ### Violation |
| 28 | |
| 29 | ```java |
| 30 | // ❌ BAD: Same validation logic repeated |
| 31 | public class UserController { |
| 32 | |
| 33 | public void createUser(UserRequest request) { |
| 34 | if (request.getEmail() == null || request.getEmail().isBlank()) { |
| 35 | throw new ValidationException("Email is required"); |
| 36 | } |
| 37 | if (!request.getEmail().contains("@")) { |
| 38 | throw new ValidationException("Invalid email format"); |
| 39 | } |
| 40 | // ... create user |
| 41 | } |
| 42 | |
| 43 | public void updateUser(UserRequest request) { |
| 44 | if (request.getEmail() == null || request.getEmail().isBlank()) { |
| 45 | throw new ValidationException("Email is required"); |
| 46 | } |
| 47 | if (!request.getEmail().contains("@")) { |
| 48 | throw new ValidationException("Invalid email format"); |
| 49 | } |
| 50 | // ... update user |
| 51 | } |
| 52 | } |
| 53 | ``` |
| 54 | |
| 55 | ### Refactored |
| 56 | |
| 57 | ```java |
| 58 | // ✅ GOOD: Single source of truth |
| 59 | public class EmailValidator { |
| 60 | |
| 61 | public void validate(String email) { |
| 62 | if (email == null || email.isBlank()) { |
| 63 | throw new ValidationException("Email is required"); |
| 64 | } |
| 65 | if (!email.contains("@")) { |
| 66 | throw new ValidationException("Invalid email format"); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | public class UserController { |
| 72 | private final EmailValidator emailValidator; |
| 73 | |
| 74 | public void createUser(UserRequest request) { |
| 75 | emailValidator.validate(request.getEmail()); |
| 76 | // ... create user |
| 77 | } |
| 78 | |
| 79 | public void updateUser(UserRequest request) { |
| 80 | emailValidator.validate(request.getEmail()); |
| 81 | // ... update user |
| 82 | } |
| 83 | } |
| 84 | ``` |
| 85 | |
| 86 | ### DRY Exceptions |
| 87 | |
| 88 | Not all duplication is bad. Avoid premature abstraction: |
| 89 | |
| 90 | ```java |
| 91 | // These look similar but serve different purposes - OK to duplicate |
| 92 | public BigDecimal calculateShippingCost(Order order) { |
| 93 | return order.getWeight().multiply(SHIPPING_RATE); |
| 94 | } |
| 95 | |
| 96 | public BigDecimal calculateInsuranceCost(Order order) { |
| 97 | return order.getValue().multiply(INSURANCE_RATE); |
| 98 | } |
| 99 | // Don't force these into one method - they'll evolve differently |
| 100 | ``` |
| 101 | |
| 102 | --- |
| 103 | |
| 104 | ## KISS - Keep It Simple |
| 105 | |
| 106 | > "The simplest solution is usually the best." |
| 107 | |
| 108 | ### Violation |
| 109 | |
| 110 | ```java |
| 111 | // ❌ BAD: Over-engineered for simple task |
| 112 | public class StringUtils { |
| 113 | |
| 114 | public boolean isEmpty(String str) { |
| 115 | return Optional.ofNullable(str) |
| 116 | .map(String::trim) |
| 117 | .map(String::isEmpty) |
| 118 | .orElseGet(() -> Boolean.TRUE); |
| 119 | } |
| 120 | } |
| 121 | ``` |
| 122 | |
| 123 | ### Refactored |
| 124 | |
| 125 | ```java |
| 126 | // ✅ GOOD: Simple and clear |
| 127 | public class StringUtils { |
| 128 | |
| 129 | public boolean isEmpty(String str) { |
| 130 | return str == null || str.trim().isEmpty(); |
| 131 | } |
| 132 | |
| 133 | // Or use existing library |
| 134 | // return StringUtils.isBlank(str); // Apache Commons |
| 135 | // return str == null || str.isBlank(); // Java 11+ |
| 136 | } |
| 137 | ``` |
| 138 | |
| 139 | ### KISS Checklist |
| 140 | |
| 141 | - Can a junior developer understand this in 30 seconds? |
| 142 | - Is there a simpler way using standard libraries? |
| 143 | - Am I adding complexity for edge cases that may never happen? |
| 144 | |
| 145 | --- |
| 146 | |
| 147 | ## YAGNI - You Aren't Gonna Need It |
| 148 | |
| 149 | > "Don't add functionality until it's necessary." |
| 150 | |
| 151 | ### Violation |
| 152 | |
| 153 | ```java |
| 154 | // ❌ BAD: Building for hypothetical future |
| 155 | public interface Repository<T, ID> { |
| 156 | T findById(ID id); |
| 157 | List<T> findAll(); |
| 158 | List<T> findAll(Pageable pageable); |
| 159 | List<T> findAll(Sort sort); |
| 160 | List<T> findAllById(Iterable<ID> ids); |
| 161 | T save(T entity); |
| 162 | List<T> saveAll(Iterable<T> entities); |
| 163 | void delete(T entity); |
| 164 | void deleteById(ID id); |
| 165 | void deleteAll(Iterable<T> entities); |
| 166 | void deleteAll(); |
| 167 | boolean existsById(ID id); |
| 168 | long count(); |
| 169 | // ... 20 more methods "just in case" |
| 170 | } |
| 171 | |
| 172 | // Current usage: only findById and save |
| 173 | ``` |
| 174 | |
| 175 | ### Refactored |
| 176 | |
| 177 | ```java |
| 178 | // ✅ GOOD: Only what's needed now |
| 179 | public interface UserRepository { |
| 180 | Optional<User> findById(Long id); |
| 181 | User save(User user); |
| 182 | } |
| 183 | |
| 184 | // Add methods when actually needed, not before |
| 185 | ``` |
| 186 | |
| 187 | ### YAGNI Signs |
| 188 | |
| 189 | - "We might need this later" |
| 190 | - "Let's make it configurable just in case" |
| 191 | - "What if we need to support X in the future?" |
| 192 | - Abstract classes with one implementation |
| 193 | |
| 194 | --- |
| 195 | |
| 196 | ## Naming Conventions |
| 197 | |
| 198 | ### Variables |
| 199 | |
| 200 | ```java |
| 201 | // ❌ BAD |
| 202 | int d; // What is d? |