Naming

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:

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:

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

with this:

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

Avoid Encodings

Ex. Type Encoding

PhoneNumber phoneString;

Ex. Member Prefixes

Don't:

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

Do:

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.

HolyHandGrenade(product);
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.

Last updated