Choose names for variables and functions that convey their purpose and meaning clearly.

🚨 Problems

Consider the confusion that arises when fetch, retrieve, and get are used interchangeably as methods across different classes. It becomes a challenge to recall which method belongs to which class, often necessitating a reliance on the library or class author's naming choices. This results in wasted time searching through headers and past code samples.

A consistent and intention-revealing naming convention is a valuable asset for programmers using your code.


public List<int[]> getThem() {
    List<int[]> list1 = new ArrayList<int[]>(); 
    for (int[] x : theList)
        if (x[0] == 4) list1.add(x);
    return list1;
}

✅ Solution

In the solution, instead of using generic names like getThem() and list1, opt for more descriptive names such as getFlaggedCells() and flaggedCells. This enhances code readability and makes the purpose of the method and variables explicit, thus facilitating easier code maintenance.


public List<int[]> getFlaggedCells() {
    List<int[]> flaggedCells = new ArrayList<int[]>();
    for (int[] cell : gameBoard)
        if (cell[STATUS_VALUE] == FLAGGED) flaggedCells.add(cell);
    return flaggedCells; 
}

💪🏽 Benefits

  • Enhancing code readability.
  • Facilitating code maintenance.
  • Clean Code - Pages 18:19 - Use Intention-Revealing Names