Re: How to name variables in a program?



Phlip wrote:
Google for "intention revealing selector" for one topic. And here's a
summary of the rest:

 - pronounce identifiers and statements out loud, to ensure they make sense
 - methods are verb phrases

Personally i think e.g.

   name := person.Name()

makes more sense than

   name := person.GetName()

I use "get" only when naming functions/methods that take an out parameter (changes the value of its parameter), e.g. in C:

   int n;
   ...
   get_some_value(&n);

More specifically, following to the command/query principle: Use a noun describing the returned value for queries (methods returning a value with no side effects) and verb phrases for commands (methods with side effects that doesn't return a value).

 - classes are generic nouns
 - objects are specific nouns
 - use the same name for the same thing, spelled the same
 - don't use l, i, O, etc. for local variables.

You probably mean - don't use l, I, O, etc. for local variables.

 - Use spikey things like x, k, j, q, etc.
 - don't use long names for local variables
 - don't use short names for global things
 - prefix strings with s or str
 - prefix objects or references with a
 - prefix pointers with p
 - don't prefix anything else - rely on clarity
 - use CapitalizedNames for important things

- use camelCase for methods

This is standard in Java. Other languages has other conventions. .