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:

Clean code:

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:

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

Use Pronounceable Names

This matters because programming is a social activity.

Compare this:

with this:

Avoid Encodings

Ex. Type Encoding

Ex. Member Prefixes

Don't:

Do:

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.

Which one is more understandable?

Pick One Word per Concept

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

Last updated

Was this helpful?