> For the complete documentation index, see [llms.txt](https://heronyang.gitbook.io/clean-up-your-spaghetti/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://heronyang.gitbook.io/clean-up-your-spaghetti/2_be_like_a_writer/naming.md).

# Naming

![](/files/-M4CTCRzk8OlG1V4NpFp)

<http://josephfitzsimmons.com/the-hardest-problem-in-programming-is-what-to-name-your-variables-and-functions/>

```
The hardest problem in programming is what to name your variables and functions
```

## Use Intention-Revealing Names

Why it exists, what it does, and how it is used.

Bad code:

```
int d; // elapse time in days
```

Clean code:

```
int elapsedTimeInDays;
int daysSinceCreation;
int daysSinceModification;
int fileAgeInDays;
```

## Avoid Disinformation

Programmers must avoid leaving false clues that obscure the meaning of code.

Ex. Don't refer to a grouping of accounts as an *accountList* unless it's actually a List. So, *accountGroup* , *bunchOfAccount*, or just *accounts*.

## Make Meaningful Distinctions

Programmers create problems for themselves when they write code solely to satisfy a compiler or interpreter.

Example:

```java
public static void copyChars(char a1[], char a2[]) {
    for (int i = 0; i < a1.length; i++) {
        a2[i] = a1[i];
    }
}
```

How can I know what is a1, and what is a2?

## Use Pronounceable Names

This matters because programming is a social activity.

Compare this:

```java
class DtaRcrd102 {
    private Date genymdhms; // generation day, year, moth, day, hour, ...
    private Date modymdhms;
    private final String pszqint = "102";
};
```

with this:

```java
class Customer {
    private Date generationTimestamp;
    private Date modificationTimestamp;
    private final String recordId = "102";
};
```

## Avoid Encodings

### Ex. Type Encoding

```java
PhoneNumber phoneString;
```

### Ex. Member Prefixes

Don't:

```java
public class Part {
    private String m_dsc;
    void setName(String name) {
        m_dsc = name;
    }
}
```

Do:

```java
public class Part {
    String description;
    void setDescription(String description) {
        this.description = description;
    }
}
```

## Class (Variables) Names

Use noun.

## Method (Function) Names

Use verb.

## Don't be Cute

Choose clarity over entertainment value. Say what you mean. Mean what you say.

```java
HolyHandGrenade(product);
```

```java
DeleteItems(product);
```

Which one is more understandable?

## Pick One Word per Concept

```
fetch(), retrieve(), get()
```

```
controller, manager, driver
```

Just pick the best one, don't mix them.
