Best Practices of Naming in Code

Rules of naming of variables and methods:

– Use meaningful names.

For example “value”, “equals”, “data”, etc are not valid names of variables for any case.

Variable name must define the exact explanation of its content.

Method name must specify the exact action of the method and for most cases must start with a verb. (e.g. createPasswordHash)

Use meaningful names for method parameters, so it can documentate itself in case of no documentation.

– Use short enough and long enough variable names in each scope of code.

Generally length may be 1 char for loop counters, one word for condition/loop variables, 1-2 words for methods, 2-3 words for classes, 3-4 words for globals.

– Don’t reuse same variable name in the same class in different contexts: e.g. in method, constructor, class.

– Don’t use same variable for different purposes in a method, conditional etc. Create a new and different named variable instead.

– Don’t use non-ASCII chars in variable names.

– Use one natural language for naming, e.g. using mixed English and German names will be inconsistent and unreadable.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>