Smell Detail

Mysterious Name

神秘命名

Change Preventers

Functions, modules, variables, or classes have names that don't clearly express what they do.

Define

Introduction

Good naming is one of the fundamental aspects of clean code. When names don't clearly express what they do, it makes the code harder to understand and maintain. Mysterious names force readers to spend extra mental effort to understand the code's purpose.

Key Identifiers

  • Variable, method, or class names that don't explain their purpose
  • Single-letter variables (except for loop counters)
  • Abbreviated names that save few characters but lose clarity
  • Names that are misleading or don't match the actual functionality
  • Generic names like data, info, manager, handler without context

Refactoring Suggestions

  1. Rename Variable: Use descriptive names that explain the variable's purpose
  2. Rename Method: Choose method names that clearly describe what the method does
  3. Rename Class: Use class names that reflect their responsibility
  4. Use Intention-Revealing Names: Make the code read like well-written prose

Explanation

Mysterious names create unnecessary cognitive load for anyone reading the code. By using clear, descriptive names, we make the code self-documenting and reduce the need for additional comments. Good names should answer three key questions: Why does it exist? What does it do? How is it used?

Code Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.util.*;
class DataProcessor {
// Mysterious variable names
private int x = 100;
private String s = "default";
private List<Object> lst = new ArrayList<>();
// Mysterious method names
public void proc(String inp) {
// What does 'proc' do? What is 'inp'?
String tmp = inp.toLowerCase();
lst.add(tmp);
}
public int calc(int a, int b) {
// What kind of calculation?
return a * b + x;
}
public boolean chk(Object obj) {
// Check what?
return obj != null && obj.toString().length() > 5;
}
// Mysterious class with unclear responsibility
static class Mgr {
private Map<String, Object> data = new HashMap<>();
public void handle(String k, Object v) {
// Handle what? What operation?
data.put(k, v);
}
public Object get(String k) {
return data.get(k);
}
public void doStuff() {
// What stuff?
data.clear();
}
}
// Method with misleading name
public void calculateTotal() {
// This method doesn't calculate anything!
System.out.println("Processing completed");
lst.clear();
}
// Abbreviated names that lose meaning
public void updateUsrPrf(String usrId, Map<String, String> prfData) {
// usr = user? prf = profile?
for (String k : prfData.keySet()) {
// More mysterious single letters
String v = prfData.get(k);
System.out.println(k + ": " + v);
}
}
// Generic unhelpful names
public void processInfo(List<String> items) {
// What kind of info? What processing?
for (String item : items) {
String result = item.trim();
if (result.length() > 0) {
lst.add(result);
}
}
}
}