Defining Programming Standards   
for Professional Programmers 
  

         

Home

Contents

1: Standards

2: Psychological Factors

3: General Principles

4: Commenting

5: Naming

6: Code Layout

7: File Layout

8: Language Usage

9: Data Usage

10: Programming Usage

11: Implementing Standards

A: Example Standard

B: References

C: Glossary

Syque

About

Share this page:

Google
C Style
syque.com
Web

 

 

Books and
more at:

USA:

In association with amazon.com

UK:

In Association with Amazon.co.uk

Canada:

In Association with amazon.ca

 

 

CHAPTER 5 : Naming

PART 2 : COMMENTING AND NAMING

CHAPTER 5 : Naming
5.1 Constraints upon naming
5.2 Abbreviations
5.3 Short names
5.4 Separating words
5.5 Spelling of names
5.6 Naming functions
5.7 Indicating functional group
5.8 Naming variables
5.9 Indicating type
5.10 Naming replacement items
5.11 Naming Files and Directories
5.12 Summary

<--Prev page | Next page -->

 

5.3  Short names

When can really short names, like 'i', 'c', etc. be used? They certainly can not be used anywhere, but there are recognized common situations where their usage is not only acceptable, but may also help the readability of the code.

Generally speaking, single (two, three..) letter names help where their brevity makes the code more readable, but where their usage is so obvious that a descriptive name is not needed.

 

Typically, i, j, k are used for array indices:

 

for ( i = 0; i < MAX_EMP; i++ )
    EmpNo[i] = 0;

 

Similarly, c is often used for single characters, s, t for strings, p, q for pointers, x, y, z for coordinates. There are, though, hidden dangers, even amongst the most common names:

 

EmpNo[1] = EmpNo[i] - 1;           /* 1 and i can easily be confused */

#define C_DIFF(a, c)  a = 'c' - c  /* may have unexpected results!   */

---------------------------------------------------------------------------

This type of hazard can be minimized, whilst retaining the benefit of a short name by using two or three characters, such as ix, ch and ptr instead. Searching for names in an editor is also easier using longer names.

For the code to remain readable, the scope of short-name variable usage must be kept strictly limited. The code loses readability when the reader has forgotten the meaning of 'i', and such is the nature of short-term memory that he will start to forget after a few lines or even a page break. There is also a danger when code is likely to 'grow', and an initial simple usage slowly becomes large and complex.

 

<--Prev page | Next page -->

 

 

  © Syque 1995-2010

Massive Content -- Maximum Speed